激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Spring Boot 與 Kotlin 使用Redis數(shù)據(jù)庫(kù)的配置方法

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

2021-03-26 10:49quanke Java教程

Redis是目前業(yè)界使用最廣泛的內(nèi)存數(shù)據(jù)存儲(chǔ)。下面通過(guò)本文給大家介紹Spring Boot 與 Kotlin 使用Redis數(shù)據(jù)庫(kù)的配置方法,感興趣的朋友一起看看吧

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

使用Redis

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

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

引入依賴(lài)

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

?
1
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,需要增加一個(gè)插件

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

完整的 build.gradle 文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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的默認(rèn)無(wú)參構(gòu)造函數(shù),默認(rèn)把所有的類(lèi)設(shè)置open類(lèi)插件
  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服務(wù)端的相關(guān)配置,具體說(shuō)明如下:

?
1
2
3
4
5
spring:
 redis:
 database: 2
 host: 192.168.1.29
 port: 6379

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

測(cè)試使用上面的配置就可以了

?
1
2
3
4
5
6
7
8
9
10
11
12
spring:
 redis:
 database: 2 # Redis數(shù)據(jù)庫(kù)索引(默認(rèn)為0
 host: 192.168.1.29
 port: 6379 # Redis服務(wù)器連接端口
 password: 123456 # Redis服務(wù)器連接密碼(默認(rèn)為空)
 pool:
  max-active: 8 # 連接池最大連接數(shù)(使用負(fù)值表示沒(méi)有限制)
  max-wait: -1 # 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制)
  max-idle: 8 # 連接池中的最大空閑連接
  min-idle: 0 # 連接池中的最小空閑連接
 timeout: 0 # 連接超時(shí)時(shí)間(毫秒)

創(chuàng)建User實(shí)體類(lèi)

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

測(cè)試訪問(wèn)

通過(guò)編寫(xiě)測(cè)試用例,舉例說(shuō)明如何訪問(wèn)Redis。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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"`() {
  // 保存對(duì)象
  val user = User("超人", 20)
  redisTemplate.opsForValue().set(user.username, user)
  log.info("超人的年齡:${redisTemplate.opsForValue().get("超人").age}")
 }
}

總結(jié)

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

原文鏈接:https://www.jianshu.com/p/9e654ab4b3c5

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产成人在线视频 | 欧美成年性h版影视中文字幕 | 国产一区免费 | 青青草成人影视 | 欧美在线成人影院 | 片在线观看 | 91成人在线免费观看 | 欧美伦理一区二区 | 最新影院 | 成年免费视频黄网站在线观看 | 黄色影院在线观看视频 | 性欧美久久 | 成年人免费视频大全 | 久久久久久久久成人 | 亚洲国产成人久久成人52 | 久久99国产精品久久99 | 日韩欧美激情视频 | 久久91亚洲人成电影网站 | 国产91免费看| 欧美jizzhd极品欧美 | 爱逼av| 欧美精品一区二区视频 | 在线a视频 | 国产一区二区三区四区五区精品 | av色偷偷 | 羞羞视频免费视频欧美 | 中文字幕综合在线观看 | 日日狠狠久久偷偷四色综合免费 | 一级国产精品一级国产精品片 | ⅴideo裸体秀hd | 黄色免费高清网站 | 国产免费永久在线观看 | 一级一片免费看 | 欧美成人一区二区三区电影 | 欧美视频一区二区 | 爽成人777777婷婷 | 国产精品视频导航 | 亚洲影院在线观看 | 国产九色在线观看 | 欧美视频不卡 | 久久成人免费观看 |