SpringBoot下使用SpringDataRedis

it2024-03-25  78

SpringBoot下使用SpringDataRedis

1.依赖导入

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--spring-boot-starter-data-redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>

2.配置

application.properties

spring.redis.host=172.16.2.134 spring.redis.port=6379 spring.redis.password=123456 spring.redis.database=1

3.创建一个对象

用于数据序列化

@Data @AllArgsConstructor public class Student implements Serializable { private String name; private Integer age; }

4.解决序列化的问题

4.1 如果只是为了在数据库中可读性:

@SpringBootApplication public class DemoApplication { @Autowired private RedisTemplate redisTemplate; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } // //在程序启动后自动运行这段程序 @Bean CommandLineRunner commandLineRunner(){ //java8Stream新特性 return (args ->{ //序列化key redisTemplate.setKeySerializer(new StringRedisSerializer()); ListOperations listOperations = redisTemplate.opsForList(); //序列化对象 redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Student.class)); listOperations.leftPush("student.1",new Student("张三",30)); } ); } }

存在的问题是进行删除时可能会因为key的序列化机制不一样,造成删除失败 解决办法是始终使用同一个 redisTemplate对象,或者不进行key的序列化设置

4.2 或者自定义一个配置类,类中定义好序列化后的 redisTemplate对象,并注入到容器中,当然这两个 redisTemplate可以共存

RedisConfig配置类

@Configuration public class RedisConfig { @Bean public RedisTemplate myRedisTemplate(RedisConnectionFactory jedisConnectionFactory){ RedisTemplate redisTemplate = new RedisTemplate(); redisTemplate.setConnectionFactory(jedisConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); //设置的是key redisTemplate.setHashKeySerializer(new StringRedisSerializer()); //设置的是hash中字段 redisTemplate.setHashValueSerializer(new StringRedisSerializer()); //设置的是hash中的value return redisTemplate; } }

DemoApplication主配置类,共存

@SpringBootApplication public class DemoApplication { @Autowired private RedisTemplate redisTemplate; //注入自定义的redisTemplate @Autowired private RedisTemplate myRedisTemplate; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } //在程序启动后自动运行这段程序 @Bean CommandLineRunner commandLineRunner(){ //java8Stream新特性 return (args ->{ redisTemplate.setKeySerializer(new StringRedisSerializer()); ListOperations listOperations = redisTemplate.opsForList(); redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Student.class)); listOperations.leftPush("student.1",new Student("张三",30)); } ); } }

共存插入结果:

换myRedisTemplate

@SpringBootApplication public class DemoApplication { @Autowired private RedisTemplate redisTemplate; //注入自定义的redisTemplate @Autowired private RedisTemplate myRedisTemplate; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } //在程序启动后自动运行这段程序 @Bean CommandLineRunner commandLineRunner(){ //java8Stream新特性 return (args ->{ ListOperations listOperations = myRedisTemplate.opsForList(); myRedisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Student.class)); listOperations.leftPush("student.1",new Student("张三",30)); } ); } }

显示结果:

最新回复(0)