Spring Boot 与 Kotlin 使用Redis数据库的配置方法
SpringBoot中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多NoSQL数据库一样提供了自动化配置的支持,包括:Redis,MongoDB,Elasticsearch,Solr和Cassandra。
使用Redis
Redis是一个开源的使用ANSIC语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库。
- Redis官网
- Redis中文社区
引入依赖
SpringBoot提供的数据访问框架SpringDataRedis基于Jedis。可以通过引入spring-boot-starter-data-redis来配置依赖关系。
compile"org.springframework.boot:spring-boot-starter-data-redis:$spring_boot_version"
注意:springboot1.4以后改名叫spring-boot-starter-data-redis1.4之前使用spring-boot-starter-redis
用kotlin,需要增加一个插件
applyplugin:"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")
}
}
applyplugin:'kotlin'
applyplugin:"kotlin-spring"//Seehttps://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin
applyplugin:'org.springframework.boot'
applyplugin:"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实体类
importjava.io.Serializable dataclassUser(valusername:String,valage:Int?):Serializable
测试访问
通过编写测试用例,举例说明如何访问Redis。
importname.quanke.kotlin.chaper11_6_3.entity.User
importorg.apache.commons.logging.LogFactory
importorg.junit.Test
importorg.junit.runner.RunWith
importorg.springframework.boot.test.context.SpringBootTest
importorg.springframework.data.redis.core.RedisTemplate
importorg.springframework.data.redis.core.StringRedisTemplate
importorg.springframework.test.context.junit4.SpringRunner
importjavax.annotation.Resource
/**
*Createdbyhttp://quanke.nameon2018/1/9.
*/
@RunWith(SpringRunner::class)
@SpringBootTest
classApplicationTests{
vallog=LogFactory.getLog(ApplicationTests::class.java)!!
@Resource
lateinitvarstringRedisTemplate:StringRedisTemplate
@Resource
lateinitvarredisTemplate:RedisTemplate
@Test
fun`redisstringtest"`(){
//保存字符串
stringRedisTemplate.opsForValue().set("url","http://quanke.name")
log.info("全科的博客地址:${stringRedisTemplate.opsForValue().get("url")}")
}
@Test
fun`redisobjecttest"`(){
//保存对象
valuser=User("超人",20)
redisTemplate.opsForValue().set(user.username,user)
log.info("超人的年龄:${redisTemplate.opsForValue().get("超人").age}")
}
}
总结
以上所述是小编给大家介绍的SpringBoot与Kotlin使用Redis数据库的配置方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!