Redis數(shù)據(jù)庫如何在SpringBoot與Kotlin中使用

redis數(shù)據(jù)庫如何在Spring Boot 與 Kotlin中使用?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

廣平ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!

使用Redis

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

  • 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,需要增加一個插件

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ù)據(jù)庫數(shù)量,默認(rèn)為16,可以理解為數(shù)據(jù)庫的schema

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

spring:
 redis:
 database: 2 # Redis數(shù)據(jù)庫索引(默認(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 # 連接池最大阻塞等待時間(使用負(fù)值表示沒有限制)
  max-idle: 8 # 連接池中的最大空閑連接
  min-idle: 0 # 連接池中的最小空閑連接
 timeout: 0 # 連接超時時間(毫秒)

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

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}")
 }
}

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

分享文章:Redis數(shù)據(jù)庫如何在SpringBoot與Kotlin中使用
路徑分享:http://muchs.cn/article48/ppheep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、網(wǎng)站排名、軟件開發(fā)、電子商務(wù)自適應(yīng)網(wǎng)站、微信公眾號

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管