国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Spring Boot 與 Kotlin 使用Redis數據庫的配置方法

2024-07-13 10:17:11
字體:
來源:轉載
供稿:網友

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

使用Redis

Redis是一個開源的使用 ANSI C 語言編寫、支持網絡、可基于內存亦可持久化的日志型、 Key-Value 數據庫。

  • Redis官網
  • Redis中文社區

引入依賴

Spring Boot提供的數據訪問框架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的默認無參構造函數,默認把所有的類設置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-pluginapply plugin: 'org.springframework.boot'apply plugin: "kotlin-jpa" //https://stackoverflow.com/questions/32038177/kotlin-with-jpa-default-constructor-helljar { 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"}

參數配置

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

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

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

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

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

創建User實體類

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

測試訪問

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

import name.quanke.kotlin.chaper11_6_3.entity.Userimport org.apache.commons.logging.LogFactoryimport org.junit.Testimport org.junit.runner.RunWithimport org.springframework.boot.test.context.SpringBootTestimport org.springframework.data.redis.core.RedisTemplateimport org.springframework.data.redis.core.StringRedisTemplateimport org.springframework.test.context.junit4.SpringRunnerimport javax.annotation.Resource/** * Created by http://quanke.name on 2018/1/9. */@RunWith(SpringRunner::class)@SpringBootTestclass 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}") }}

總結

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


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武城县| 金阳县| 宁夏| 昭苏县| 阆中市| 阳春市| 芜湖市| 自贡市| 信丰县| 辉县市| 宁波市| 卓尼县| 临泉县| 襄樊市| 阿鲁科尔沁旗| 大洼县| 华池县| 达孜县| 沾益县| 汕头市| 惠安县| 盐城市| 牟定县| 鄂托克前旗| 桐庐县| 新宁县| 盖州市| 祁连县| 乐陵市| 巨鹿县| 广汉市| 沙河市| 拉萨市| 灵山县| 滨海县| 石楼县| 安徽省| 屏东县| 通渭县| 湟源县| 澄江县|