spring data redis 使用之 spring boot 2.x
生活随笔
收集整理的這篇文章主要介紹了
spring data redis 使用之 spring boot 2.x
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
準(zhǔn)備
本人寫的spring data是通過maven子父工程管理
parent項目的 : pom.xml
pom
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>spring-boot-data</artifactId><groupId>com.ronnie</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>spring-data-redis</artifactId><dependencies><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- fix start java.lang.ClassNotFoundException: io.lettuce.core.AbstractRedisClient --><dependency><groupId>io.lettuce</groupId><artifactId>lettuce-core</artifactId><version>5.0.3.RELEASE</version><optional>true</optional></dependency><!-- fix end java.lang.ClassNotFoundException: io.lettuce.core.AbstractRedisClient --></dependencies></project>使用的是lettuce redis連接客戶端
配置
package com.ronnie.data.config;import com.ronnie.data.entity.User; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisPassword; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer;import java.io.Serializable;/*** @Description:* @Author: rongyu* @CreateDate: 2018/9/8$ 16:08$* @Remark:*/ @Configuration public class ApplicationConfig {// TODO remove config of redis to application.yml@Beanpublic RedisConnectionFactory redisConnectionFactory() {RedisStandaloneConfiguration configuration =new RedisStandaloneConfiguration("192.168.1.12", 6379);RedisPassword redisPassword = RedisPassword.of("lcex123");configuration.setDatabase(5);configuration.setPassword(redisPassword);return new LettuceConnectionFactory(configuration);}@Beanpublic RedisTemplate<String, Serializable> redisCacheTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, Serializable> template = new RedisTemplate<>();template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new GenericJackson2JsonRedisSerializer());template.setConnectionFactory(redisConnectionFactory);return template;}@Beanpublic RedisTemplate<String, User> redisUserTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, User> stringUserRedisTemplate = new RedisTemplate<>();stringUserRedisTemplate.setKeySerializer(new StringRedisSerializer());stringUserRedisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());stringUserRedisTemplate.setConnectionFactory(redisConnectionFactory);return stringUserRedisTemplate;}}測試
package com.ronnie.data;import com.ronnie.data.entity.User; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.test.context.junit4.SpringRunner;import java.io.Serializable;@Slf4j @RunWith(SpringRunner.class) @SpringBootTest public class DemoApplicationTests {@Autowiredprivate RedisTemplate<String, Serializable> redisTemplate;@Autowiredprivate RedisTemplate<String, User> userRedisTemplate;@Testpublic void saveString() {// 保存字符串redisTemplate.opsForValue().set("a:b:c", "111");Serializable serializable = redisTemplate.opsForValue().get("a:b:c");log.info("s={}", serializable);}@Testpublic void saveUser() {// 保存字符串User user = new User();user.setUsername("root");user.setPassword("root");ValueOperations<String, User> opsForValue = userRedisTemplate.opsForValue();opsForValue.set("1", user);User user1 = opsForValue.get("1");log.info("user={}", user1);}}總結(jié)
以上是生活随笔為你收集整理的spring data redis 使用之 spring boot 2.x的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: P3758 [TJOI2017]可乐
- 下一篇: 安装JDK时提示 IllegalArgu