温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Redis数据库如何在Spring Boot 与 Kotlin中使用

发布时间:2021-03-30 15:27:51 来源:亿速云 阅读:386 作者:Leah 栏目:编程语言

Redis数据库如何在Spring Boot 与 Kotlin中使用?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

使用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-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"
}

参数配置

按照惯例在 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.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}")
 }
}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI