您现在的位置是:首页 > 文章详情

spring data redis 使用之 spring boot 2.x

日期:2018-09-09点击:381

准备

本人写的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 @Bean public 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); } @Bean public 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; } @Bean public 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 { @Autowired private RedisTemplate<String, Serializable> redisTemplate; @Autowired private RedisTemplate<String, User> userRedisTemplate; @Test public void saveString() { // 保存字符串 redisTemplate.opsForValue().set("a:b:c", "111"); Serializable serializable = redisTemplate.opsForValue().get("a:b:c"); log.info("s={}", serializable); } @Test public 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); } } 
原文链接:https://yq.aliyun.com/articles/652129
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章