SpringBoot整合SpringDataRedis、基本操作,以及创建配置类指定序列化策略
官网
https://spring.io/projects/spring-boot#learn
https://docs.spring.io/spring-boot/docs/2.4.13/reference/html/
点击上面 Spring Data Redis
可以在README中查看需要的内容,比如:
SpringBoot整合Spring Data Redis
添加pom依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
redis自动配置类:RedisAutoConfiguration
属性配置类RedisProperties
@ConfigurationProperties(prefix = "spring.redis") public class RedisProperties { /** * Database index used by the connection factory. */ private int database = 0; /** * Connection URL. Overrides host, port, and password. User is ignored. Example: * redis://user:password@example.com:6379 */ private String url; /** * Redis server host. */ private String host = "localhost"; /** * Login username of the redis server. */ private String username; /** * Login password of the redis server. */ private String password; /** * Redis server port. */ private int port = 6379; /** * Whether to enable SSL support. */ private boolean ssl; /** * Read timeout. */ private Duration timeout; /** * Connection timeout. */ private Duration connectTimeout; /** * Client name to be set on connections with CLIENT SETNAME. */ private String clientName; /** * Type of client to use. By default, auto-detected according to the classpath. */ private ClientType clientType; private Sentinel sentinel; private Cluster cluster; private final Jedis jedis = new Jedis(); private final Lettuce lettuce = new Lettuce(); ......
我们的配置文件application.properties中可以配置redis,以spring.redis开头
# 服务器ip spring.redis.host=116.10.53.66 # 端口 spring.redis.port=6379 # 密码 spring.redis.password=tester # 第一个数据库存储 spring.redis.database=0 # 最大活跃数 spring.redis.jedis.pool.max-active=20 # 最大空闲数 spring.redis.jedis.pool.max-idle=10 # 最小空闲数 spring.redis.jedis.pool.min-idle=5
使用redisTemplate
操作字符串
key和value都不设置序列化器
package com.qzcsbj.demo; import org.junit.jupiter.api.Test; 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.StringRedisTemplate; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ @SpringBootTest public class MyTest { @Autowired private StringRedisTemplate stringRedisTemplate; @Autowired private RedisTemplate redisTemplate; @Test public void test1(){ redisTemplate.opsForValue().set("name","jack"); System.out.println("set字符串完成。"); Object name = redisTemplate.opsForValue().get("name"); System.out.println("获取到的name: " + name); } }
代码中正常获取
但是redis客户端不行
给key和value设置序列化器
package com.qzcsbj.demo; import org.junit.jupiter.api.Test; 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.StringRedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ @SpringBootTest public class MyTest { @Autowired private StringRedisTemplate stringRedisTemplate; @Autowired private RedisTemplate redisTemplate; @Test public void test1(){ // 为key设置序列化器 redisTemplate.setKeySerializer(new StringRedisSerializer()); // 为value设置序列化器 redisTemplate.setValueSerializer(new StringRedisSerializer()); redisTemplate.opsForValue().set("name","jack"); System.out.println("set字符串完成。"); Object name = redisTemplate.opsForValue().get("name"); System.out.println("获取到的name: " + name); } }
操作对象
只为value设置了序列化器
package com.qzcsbj.demo; import com.qzcsbj.demo.pojo.User; import org.junit.jupiter.api.Test; 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.StringRedisTemplate; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ @SpringBootTest public class MyTest { @Autowired private StringRedisTemplate stringRedisTemplate; @Autowired private RedisTemplate redisTemplate; @Test public void test1(){ User user = new User(); user.setUsername("jack"); user.setPassword("123456"); // 设置value序列化器 redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.opsForValue().set("user",user); System.out.println("set对象完成。"); System.out.println("user: " + redisTemplate.opsForValue().get("user")); } }
为key和value设置序列化器
package com.qzcsbj.demo; import com.qzcsbj.demo.pojo.User; import org.junit.jupiter.api.Test; 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.StringRedisTemplate; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ @SpringBootTest public class MyTest { @Autowired private StringRedisTemplate stringRedisTemplate; @Autowired private RedisTemplate redisTemplate; @Test public void test1(){ User user = new User(); user.setUsername("jack"); user.setPassword("123456"); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.opsForValue().set("user",user); System.out.println("set对象完成。"); System.out.println("user: " + redisTemplate.opsForValue().get("user")); } }
value序列化方式采用jackson
package com.qzcsbj.demo; import com.qzcsbj.demo.pojo.User; import org.junit.jupiter.api.Test; 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.StringRedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ @SpringBootTest public class MyTest { @Autowired private StringRedisTemplate stringRedisTemplate; @Autowired private RedisTemplate redisTemplate; @Test public void test1(){ User user = new User(); user.setUsername("jack"); user.setPassword("123456"); redisTemplate.setKeySerializer(new StringRedisSerializer()); // 设置value序列化器 // redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); // value序列化方式采用jackson redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class)); redisTemplate.opsForValue().set("user",user); System.out.println("set对象完成。"); System.out.println("user: " + redisTemplate.opsForValue().get("user")); } }
操作list
package com.qzcsbj.demo; import com.qzcsbj.demo.pojo.User; import org.junit.jupiter.api.Test; 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.StringRedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.util.ArrayList; import java.util.List; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ @SpringBootTest public class MyTest { @Autowired private StringRedisTemplate stringRedisTemplate; @Autowired private RedisTemplate redisTemplate; @Test public void test1(){ List<User> users = new ArrayList<>(); User user = new User(); user.setUsername("jack"); user.setPassword("123456"); users.add(user); User user2 = new User(); user2.setUsername("tom"); user2.setPassword("654321"); users.add(user2); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.opsForValue().set("users",users); System.out.println("set集合完成。"); System.out.println("users: " + redisTemplate.opsForValue().get("users")); } }
value序列化方式采用jackson
package com.qzcsbj.demo; import com.qzcsbj.demo.pojo.User; import org.junit.jupiter.api.Test; 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.StringRedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.util.ArrayList; import java.util.List; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ @SpringBootTest public class MyTest { @Autowired private StringRedisTemplate stringRedisTemplate; @Autowired private RedisTemplate redisTemplate; @Test public void test1(){ List<User> users = new ArrayList<>(); User user = new User(); user.setUsername("jack"); user.setPassword("123456"); users.add(user); User user2 = new User(); user2.setUsername("tom"); user2.setPassword("654321"); users.add(user2); redisTemplate.setKeySerializer(new StringRedisSerializer()); // 设置value序列化器 // redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); // value序列化方式采用jackson redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(List.class)); redisTemplate.opsForValue().set("users",users); System.out.println("set集合完成。"); System.out.println("users: " + redisTemplate.opsForValue().get("users")); } }
使用stringRedisTemplate(更简单)
操作字符串
package com.qzcsbj.demo; import com.qzcsbj.demo.pojo.User; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.util.ArrayList; import java.util.List; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ @SpringBootTest public class MyTest { @Autowired private StringRedisTemplate stringRedisTemplate; @Test public void test2(){ stringRedisTemplate.opsForValue().set("name","jack"); System.out.println("set字符串完成。"); Object name = stringRedisTemplate.opsForValue().get("name"); System.out.println("获取到的name: " + name); } }
操作对象
package com.qzcsbj.demo; import com.qzcsbj.demo.pojo.User; import com.qzcsbj.demo.utils.JsonUtil; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.util.ArrayList; import java.util.List; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ @SpringBootTest public class MyTest { @Autowired private StringRedisTemplate stringRedisTemplate; @Test public void test2(){ User user = new User(); user.setUsername("jack"); user.setPassword("123456"); stringRedisTemplate.opsForValue().set("user",JsonUtil.objectToJson(user)); System.out.println("set对象完成。"); System.out.println("user: " + stringRedisTemplate.opsForValue().get("user")); } }
操作list
package com.qzcsbj.demo; import com.qzcsbj.demo.pojo.User; import com.qzcsbj.demo.utils.JsonUtil; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import springfox.documentation.spring.web.json.Json; import java.util.ArrayList; import java.util.List; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ @SpringBootTest public class MyTest { @Autowired private StringRedisTemplate stringRedisTemplate; @Test public void test2(){ List<User> users = new ArrayList<>(); User user = new User(); user.setUsername("jack"); user.setPassword("123456"); users.add(user); User user2 = new User(); user2.setUsername("tom"); user2.setPassword("654321"); users.add(user2); // 对象转为json字符串 stringRedisTemplate.opsForValue().set("users",JsonUtil.objectToJson(users)); System.out.println("set集合完成。"); System.out.println("users: " + stringRedisTemplate.opsForValue().get("users")); } }
使用总结
如果用redisTemplate,一般是创建配置类自定义RedisTemplate来设置序列化器
package com.qzcsbj.demo.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ @Configuration public class RedisConfig { @Autowired private RedisConnectionFactory connectionFactory; @Bean public RedisTemplate redisTemplate(){ RedisTemplate<String,Object> redisTemplate=new RedisTemplate<>(); redisTemplate.setConnectionFactory(connectionFactory); // 指定key、value的序列化策略 redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; } @Bean public StringRedisTemplate stringRedisTemplate(){ StringRedisTemplate stringRedisTemplate=new StringRedisTemplate(); stringRedisTemplate.setConnectionFactory(connectionFactory); return stringRedisTemplate; } }
__EOF__
本文作者:持之以恒(韧)
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!