欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2018年01月24日 08:45:35   作者:quanke  
Redis是目前業(yè)界使用最廣泛的內(nèi)存數(shù)據(jù)存儲(chǔ)。下面通過本文給大家介紹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è)開源的使用 ANSI C 語言編寫、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、 Key-Value 數(shù)據(jù)庫(kù)。

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

引入依賴

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

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è)插件

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的默認(rèn)無參構(gòu)造函數(shù),默認(rèn)把所有的類設(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服務(wù)端的相關(guān)配置,具體說明如下:

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è)試使用上面的配置就可以了

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ù)值表示沒有限制)
  max-wait: -1 # 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒有限制)
  max-idle: 8 # 連接池中的最大空閑連接
  min-idle: 0 # 連接池中的最小空閑連接
 timeout: 0 # 連接超時(shí)時(shí)間(毫秒)

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

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

測(cè)試訪問

通過編寫測(cè)試用例,舉例說明如何訪問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"`() {
  // 保存對(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ì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Hadoop中的壓縮與解壓縮案例詳解

    Hadoop中的壓縮與解壓縮案例詳解

    壓縮就是通過某種技術(shù)(算法)把原始文件變下,相應(yīng)的解壓就是把壓縮后的文件變成原始文件,本文給大家分享Hadoop中的壓縮知識(shí),感興趣的朋友跟隨小編一起看看吧
    2021-12-12
  • Java數(shù)據(jù)結(jié)構(gòu)之順序表篇

    Java數(shù)據(jù)結(jié)構(gòu)之順序表篇

    順序表,全名順序存儲(chǔ)結(jié)構(gòu),是線性表的一種。線性表用于存儲(chǔ)邏輯關(guān)系為“一對(duì)一”的數(shù)據(jù),順序表自然也不例外,不僅如此,順序表對(duì)數(shù)據(jù)物理存儲(chǔ)結(jié)構(gòu)也有要求。順序表存儲(chǔ)數(shù)據(jù)時(shí),會(huì)提前申請(qǐng)一整塊足夠大小的物理空間,然后將數(shù)據(jù)依次存儲(chǔ)起來,存儲(chǔ)時(shí)數(shù)據(jù)元素間不留縫隙
    2022-01-01
  • java實(shí)現(xiàn)一個(gè)桌球小游戲

    java實(shí)現(xiàn)一個(gè)桌球小游戲

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)一個(gè)桌球小游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Maven發(fā)布封裝到中央倉(cāng)庫(kù)時(shí)候報(bào)錯(cuò):no default secret key

    Maven發(fā)布封裝到中央倉(cāng)庫(kù)時(shí)候報(bào)錯(cuò):no default secret key

    這篇文章主要介紹了Maven發(fā)布封裝到中央倉(cāng)庫(kù)時(shí)候報(bào)錯(cuò):no default secret key,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • 解讀@RequestBody與post請(qǐng)求的關(guān)系

    解讀@RequestBody與post請(qǐng)求的關(guān)系

    這篇文章主要介紹了解讀@RequestBody與post請(qǐng)求的關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Java中正則表達(dá)式去除html標(biāo)簽

    Java中正則表達(dá)式去除html標(biāo)簽

    Java中正則表達(dá)式去除html的標(biāo)簽,主要目的更精確的顯示內(nèi)容,接下來通過本文給大家介紹Java中正則表達(dá)式去除html標(biāo)簽的方法,需要的朋友參考下
    2017-02-02
  • 5分鐘教你使用java搞定網(wǎng)站登錄驗(yàn)證碼

    5分鐘教你使用java搞定網(wǎng)站登錄驗(yàn)證碼

    這篇文章主要為大家介紹了使用java搞定網(wǎng)站登錄驗(yàn)證碼的快速實(shí)現(xiàn)方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • springboot?整合sentinel的示例代碼

    springboot?整合sentinel的示例代碼

    本文主要介紹了springboot?整合sentinel的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • JSON序列化導(dǎo)致Long類型被搞成Integer的坑及解決

    JSON序列化導(dǎo)致Long類型被搞成Integer的坑及解決

    這篇文章主要介紹了JSON序列化導(dǎo)致Long類型被搞成Integer的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java壓縮之LZW算法字典壓縮與解壓講解

    Java壓縮之LZW算法字典壓縮與解壓講解

    今天小編就為大家分享一篇關(guān)于Java壓縮之LZW算法字典壓縮與解壓講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02

最新評(píng)論