最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot 與 Kotlin 使用Redis數(shù)據(jù)庫的配置方法

 更新時間:2018年01月24日 08:45:35   作者:quanke  
Redis是目前業(yè)界使用最廣泛的內(nèi)存數(shù)據(jù)存儲。下面通過本文給大家介紹Spring Boot 與 Kotlin 使用Redis數(shù)據(jù)庫的配置方法,感興趣的朋友一起看看吧

Spring Boot中除了對常用的關系型數(shù)據(jù)庫提供了優(yōu)秀的自動化支持之外,對于很多NoSQL數(shù)據(jù)庫一樣提供了自動化配置的支持,包括:Redis, MongoDB, Elasticsearch, Solr和Cassandra。

使用Redis

Redis是一個開源的使用 ANSI C 語言編寫、支持網(wǎng)絡、可基于內(nèi)存亦可持久化的日志型、 Key-Value 數(shù)據(jù)庫。

  • Redis官網(wǎng)
  • Redis中文社區(qū)

引入依賴

Spring Boot提供的數(shù)據(jù)訪問框架Spring Data Redis基于Jedis??梢酝ㄟ^引入 spring-boot-starter-data-redis 來配置依賴關系。

compile "org.springframework.boot:spring-boot-starter-data-redis:$spring_boot_version"

注意:spring boot 1.4 以后改名叫 spring-boot-starter-data-redis 1.4 之前使用 spring-boot-starter-redis

用kotlin,需要增加一個插件

apply plugin: "kotlin-jpa" //https://stackoverflow.com/questions/32038177/kotlin-with-jpa-default-constructor-hell

完整的 build.gradle 文件

group 'name.quanke.kotlin'
version '1.0-SNAPSHOT'
buildscript {
 ext.kotlin_version = '1.2.10'
 ext.spring_boot_version = '1.5.4.RELEASE'
 ext.springfox_swagger2_version = '2.7.0'
 ext.mysql_version = '5.1.21'
 repositories {
  mavenCentral()
 }
 dependencies {
  classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
  classpath("org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version")
//  Kotlin整合SpringBoot的默認無參構造函數(shù),默認把所有的類設置open類插件
  classpath("org.jetbrains.kotlin:kotlin-noarg:$kotlin_version")
  classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlin_version")
 }
}
apply plugin: 'kotlin'
apply plugin: "kotlin-spring" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin
apply plugin: 'org.springframework.boot'
apply plugin: "kotlin-jpa" //https://stackoverflow.com/questions/32038177/kotlin-with-jpa-default-constructor-hell
jar {
 baseName = 'chapter11-6-3-service'
 version = '0.1.0'
}
repositories {
 mavenCentral()
}
dependencies {
 compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
 compile("org.jetbrains.kotlin:kotlin-reflect:${kotlin_version}")
 compile "org.springframework.boot:spring-boot-starter-web:$spring_boot_version"
 compile "org.springframework.boot:spring-boot-starter-data-redis:$spring_boot_version"
 testCompile "org.springframework.boot:spring-boot-starter-test:$spring_boot_version"
 testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
}
compileKotlin {
 kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
 kotlinOptions.jvmTarget = "1.8"
}

參數(shù)配置

按照慣例在 application.yml 中加入Redis服務端的相關配置,具體說明如下:

spring:
 redis:
 database: 2
 host: 192.168.1.29
 port: 6379

其中spring.redis.database的配置通常使用0即可,Redis在配置的時候可以設置數(shù)據(jù)庫數(shù)量,默認為16,可以理解為數(shù)據(jù)庫的schema

測試使用上面的配置就可以了

spring:
 redis:
 database: 2 # Redis數(shù)據(jù)庫索引(默認為0)
 host: 192.168.1.29
 port: 6379 # Redis服務器連接端口
 password: 123456 # Redis服務器連接密碼(默認為空)
 pool:
  max-active: 8 # 連接池最大連接數(shù)(使用負值表示沒有限制)
  max-wait: -1 # 連接池最大阻塞等待時間(使用負值表示沒有限制)
  max-idle: 8 # 連接池中的最大空閑連接
  min-idle: 0 # 連接池中的最小空閑連接
 timeout: 0 # 連接超時時間(毫秒)

創(chuàng)建User實體類

import java.io.Serializable
data class User(val username: String, val age: Int?) : Serializable

測試訪問

通過編寫測試用例,舉例說明如何訪問Redis。

import name.quanke.kotlin.chaper11_6_3.entity.User
import org.apache.commons.logging.LogFactory
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.data.redis.core.RedisTemplate
import org.springframework.data.redis.core.StringRedisTemplate
import org.springframework.test.context.junit4.SpringRunner
import javax.annotation.Resource
/**
 * Created by http://quanke.name on 2018/1/9.
 */
@RunWith(SpringRunner::class)
@SpringBootTest
class ApplicationTests {
 val log = LogFactory.getLog(ApplicationTests::class.java)!!
 @Resource
 lateinit var stringRedisTemplate: StringRedisTemplate
 @Resource
 lateinit var redisTemplate: RedisTemplate<String, User>
 @Test
 fun `redis string test"`() {
  // 保存字符串
  stringRedisTemplate.opsForValue().set("url", "http://quanke.name")
  log.info("全科的博客地址: ${stringRedisTemplate.opsForValue().get("url")}")
 }
 @Test
 fun `redis object test"`() {
  // 保存對象
  val user = User("超人", 20)
  redisTemplate.opsForValue().set(user.username, user)
  log.info("超人的年齡:${redisTemplate.opsForValue().get("超人").age}")
 }
}

總結(jié)

以上所述是小編給大家介紹的Spring Boot 與 Kotlin 使用Redis數(shù)據(jù)庫的配置方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關文章

  • 深入了解Java方法的重載與重寫

    深入了解Java方法的重載與重寫

    在最初學習java的時候,人們都知道,java這種面向?qū)ο蟮恼Z言,一共有三大特征,分別是:封裝、繼承、多態(tài)。多態(tài)的實現(xiàn)途徑有三種:重寫、重載、接口實現(xiàn)。本文就來為大家詳細講講Java方法的重載與重寫
    2022-07-07
  • Spring注解開發(fā)@Bean和@ComponentScan使用案例

    Spring注解開發(fā)@Bean和@ComponentScan使用案例

    這篇文章主要介紹了Spring注解開發(fā)@Bean和@ComponentScan使用案例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • javaCV開發(fā)詳解之推流器和錄制器的實現(xiàn)

    javaCV開發(fā)詳解之推流器和錄制器的實現(xiàn)

    這篇文章主要介紹了javaCV開發(fā)詳解之推流器和錄制器實現(xiàn),對JavaCV感興趣的同學,可以參考下
    2021-04-04
  • SpringCloud Zuul和Gateway的實例代碼(搭建方式)

    SpringCloud Zuul和Gateway的實例代碼(搭建方式)

    本文主要介紹了SpringCloudZuul和SpringCloudGateway的簡單示例,SpringCloudGateway是推薦使用的API網(wǎng)關解決方案,基于SpringFramework5和ProjectReactor構建,具有更高的性能和吞吐量
    2025-02-02
  • Spring security認證兩類用戶代碼實例

    Spring security認證兩類用戶代碼實例

    這篇文章主要介紹了Spring security認證兩類用戶代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06
  • SpringBoot+Vue+Axios+BootStrap實現(xiàn)圖書的增刪改查功能示例

    SpringBoot+Vue+Axios+BootStrap實現(xiàn)圖書的增刪改查功能示例

    本文主要介紹了SpringBoot+Vue+Axios+BootStrap實現(xiàn)圖書的增刪改查功能,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • SpringBoot中打war包需要注意事項

    SpringBoot中打war包需要注意事項

    這篇文章主要介紹了SpringBoot中打war包需要注意事項,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • SpringBoot實現(xiàn)RBAC權限校驗模型的示例

    SpringBoot實現(xiàn)RBAC權限校驗模型的示例

    本文主要介紹了SpringBoot實現(xiàn)RBAC權限校驗模型的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-04-04
  • Spring Boot 中的任務執(zhí)行器基本概念及使用方法

    Spring Boot 中的任務執(zhí)行器基本概念及使用方法

    務執(zhí)行器是 Spring Boot 中的一個非常實用的模塊,它可以簡化異步任務的開發(fā)和管理,在本文中,我們介紹了任務執(zhí)行器的基本概念和使用方法,以及一個完整的示例代碼,需要的朋友可以參考下
    2023-07-07
  • springmvc @RequestBody String類型參數(shù)的使用

    springmvc @RequestBody String類型參數(shù)的使用

    這篇文章主要介紹了springmvc @RequestBody String類型參數(shù)的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10

最新評論

建昌县| 富裕县| 怀柔区| 汕头市| 达州市| 青海省| 六安市| 新兴县| 汕头市| 开封县| 江源县| 黄梅县| 永吉县| 长白| 屯门区| 安庆市| 迭部县| 安阳县| 吕梁市| 丰顺县| 涿鹿县| 阳江市| 五峰| 垦利县| 甘孜| 章丘市| 深泽县| 于都县| 长治市| 上虞市| 淅川县| 会同县| 大邑县| 壤塘县| 柞水县| 靖远县| 南丰县| 宣恩县| 东乡县| 平阳县| 乌兰察布市|