Spring Boot 与 Kotlin 使用Redis数据库

Spring Boot中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多NoSQL数据库一样提供了自动化配置的支持,包括:Redis, MongoDB, Elasticsearch, Solr和Cassandra。

使用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文件

1group 'name.quanke.kotlin' 2version '1.0-SNAPSHOT' 3 4buildscript { 5 ext.kotlin_version = '1.2.10' 6 ext.spring_boot_version = '1.5.4.RELEASE' 7 ext.springfox_swagger2_version = '2.7.0' 8 ext.mysql_version = '5.1.21' 9 repositories { 10 mavenCentral() 11 } 12 dependencies { 13 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 14 classpath("org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version") 15 16// Kotlin整合SpringBoot的默认无参构造函数,默认把所有的类设置open类插件 17 classpath("org.jetbrains.kotlin:kotlin-noarg:$kotlin_version") 18 classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlin_version") 19 } 20} 21 22apply plugin: 'kotlin' 23apply plugin: "kotlin-spring" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin 24apply plugin: 'org.springframework.boot' 25apply plugin: "kotlin-jpa" //https://stackoverflow.com/questions/32038177/kotlin-with-jpa-default-constructor-hell 26jar { 27 baseName = 'chapter11-6-3-service' 28 version = '0.1.0' 29} 30repositories { 31 mavenCentral() 32} 33 34 35dependencies { 36 compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version" 37 compile("org.jetbrains.kotlin:kotlin-reflect:${kotlin_version}") 38 39 40 compile "org.springframework.boot:spring-boot-starter-web:$spring_boot_version" 41 compile "org.springframework.boot:spring-boot-starter-data-redis:$spring_boot_version" 42 43 testCompile "org.springframework.boot:spring-boot-starter-test:$spring_boot_version" 44 testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version" 45 46} 47 48compileKotlin { 49 kotlinOptions.jvmTarget = "1.8" 50} 51compileTestKotlin { 52 kotlinOptions.jvmTarget = "1.8" 53}

参数配置

按照惯例在application.yml中加入Redis服务端的相关配置,具体说明如下:

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

其中spring.redis.database的配置通常使用0即可,Redis在配置的时候可以设置数据库数量,默认为16,可以理解为数据库的schema

测试使用上面的配置就可以了

1spring: 2 redis: 3 database: 2 # Redis数据库索引(默认为04 host: 192.168.1.29 5 port: 6379 # Redis服务器连接端口 6 password: 123456 # Redis服务器连接密码(默认为空) 7 pool: 8 max-active: 8 # 连接池最大连接数(使用负值表示没有限制) 9 max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制) 10 max-idle: 8 # 连接池中的最大空闲连接 11 min-idle: 0 # 连接池中的最小空闲连接 12 timeout: 0 # 连接超时时间(毫秒) 13

创建User实体类

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

测试访问

通过编写测试用例,举例说明如何访问Redis。

1import name.quanke.kotlin.chaper11_6_3.entity.User 2import org.apache.commons.logging.LogFactory 3import org.junit.Test 4import org.junit.runner.RunWith 5import org.springframework.boot.test.context.SpringBootTest 6import org.springframework.data.redis.core.RedisTemplate 7import org.springframework.data.redis.core.StringRedisTemplate 8import org.springframework.test.context.junit4.SpringRunner 9import javax.annotation.Resource 10 11 12/** 13 * Created by http://quanke.name on 2018/1/9. 14 */ 15@RunWith(SpringRunner::class) 16@SpringBootTest 17class ApplicationTests { 18 19 val log = LogFactory.getLog(ApplicationTests::class.java)!! 20 21 @Resource 22 lateinit var stringRedisTemplate: StringRedisTemplate 23 24 @Resource 25 lateinit var redisTemplate: RedisTemplate<String, User> 26 27 @Test 28 fun `redis string test"`() { 29 30 // 保存字符串 31 stringRedisTemplate.opsForValue().set("url", "http://quanke.name") 32 log.info("全科的博客地址: ${stringRedisTemplate.opsForValue().get("url")}") 33 34 } 35 @Test 36 fun `redis object test"`() { 37 38 // 保存对象 39 val user = User("超人", 20) 40 redisTemplate.opsForValue().set(user.username, user) 41 42 log.info("超人的年龄:${redisTemplate.opsForValue().get("超人").age}") 43 } 44 45}

当然spring-boot-starter-data-redis中提供的数据操作远不止这些,本文仅作为在Spring Boot中使用redis时的配置参考,更多对于redis的操作使用,请参考 Spring Data Redis Reference

更多Spring Boot 和 kotlin相关内容

欢迎关注《Spring Boot 与 kotlin 实战》

全科龙婷

参考

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )