In addition to providing excellent automation support for commonly used relational databases, Spring Boot also provides automation configuration support for many NoSQL databases, including: Redis, MongoDB, Elasticsearch, Solr and Cassandra.
Using Redis
Redis is an open source log-type, Key-Value database written in ANSI C language, supports network, memory-based and persistent.
Introduce dependencies
Spring Data Redis is a data access framework provided by Spring Boot. Dependencies can be configured by introducing spring-boot-starter-data-redis.
compile "org.springframework.boot:spring-boot-starter-data-redis:$spring_boot_version"
Note: Spring boot 1.4 was renamed spring-boot-starter-data-redis 1.4 before spring-boot-starter-redis
Using kotlin, you need to add a plugin
apply plugin: "kotlin-jpa" //https://stackoverflow.com/questions/32038177/kotlin-with-jpa-default-constructor-hell
Complete build.gradle file
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 integrates SpringBoot's default parameterless constructor, and sets all classes by default to open class plugin 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-pluginapply plugin: 'org.springframework.boot'apply plugin: "kotlin-jpa" //https://stackoverflow.com/questions/32038177/kotlin-with-jpa-default-constructor-helljar { 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"}Parameter configuration
According to convention, add the Redis server-side configuration in application.yml, and the specific description is as follows:
spring: redis: database: 2 host: 192.168.1.29 port: 6379
Among them, the configuration of spring.redis.database usually uses 0. Redis can set the number of databases when configuring, and the default is 16. It can be understood as the schema of the database.
Just use the above configuration
spring: redis: database: 2 # Redis database index (default is 0) host: 192.168.1.29 port: 6379 # Redis server connection port password: 123456 # Redis server connection password (default is empty) pool: max-active: 8 # Maximum number of connections in connection pool (using negative values means no limit) max-wait: -1 # Maximum blocking waiting time in connection pool (using negative values means no limit) max-idle: 8 # Maximum idle connection in connection pool min-idle: 0 # Minimum idle connection timeout in connection pool: 0 # Connection timeout in milliseconds)
Create User Entity Class
import java.io.Serializabledata class User(val username: String, val age: Int?) : Serializable
Test access
By writing test cases, give examples of how to access Redis.
import name.quanke.kotlin.chaper11_6_3.entity.Userimport org.apache.commons.logging.LogFactoryimport org.junit.Testimport org.junit.runner.RunWithimport org.springframework.boot.test.context.SpringBootTestimport org.springframework.data.redis.core.RedisTemplateimport org.springframework.data.redis.core.StringRedisTemplateimport org.springframework.test.context.junit4.SpringRunnerimport javax.annotation.Resource/** * Created by http://quanke.name on 2018/1/9. */@RunWith(SpringRunner::class)@SpringBootTestclass 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"`() { // Save the string stringRedisTemplate.opsForValue().set("url", "http://quanke.name") log.info("Blog address for general studies: ${stringRedisTemplate.opsForValue().get("url")}") } @Test fun `redis object test"`() { // Save the object val user = User("Superman", 20) redisTemplate.opsForValue().set(user.username, user) log.info("Superman's age: ${redisTemplate.opsForValue().get("Superman").age}") }}Summarize
The above is the configuration method of using Redis database for Spring Boot and Kotlin introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!