目录
概述
问题再现
1.加入依赖
2.编写application.yml(或application.properties)配置文件
3.添加redis配置类
4.写测试类
5.运行后报错
问题解决
1.注释掉上面的redis配置类(注释掉,或者删除)
2.注释掉侯我们再运行我们的测试类
查看redis中的key
解决key乱码问题
再测试
概述
公司项目使用的为springboot框架,而且里面的redis也都是公司集成好了的。自己对redis使用的也还是可以的,哈哈哈。今天有刚入职的同事自己在用springboot整合redis的时候出现了问题,感觉很有实际意义就写了这篇文章,大家可以一起看一看。注意,我这里的springboot版本2.2.5.RELEASE。而且我这里用到redis也是windows版本的,正式开发一定要在Linux服务器,使用Linux版本的redis,这里这是演示,所以就是用了windows版本的。
问题再现
1.加入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.9.0</version>
</dependency>2.编写application.yml(或application.properties)配置文件
这是.properties格式下的

这是yml格式下的

3.添加redis配置类
package com.mybatis.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @author summer
* @date 2022-04-25 16:50
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setConnectionFactory(factory);
//key序列化方式
template.setKeySerializer(redisSerializer);
//value序列化
template.setValueSerializer(jackson2JsonRedisSerializer);
//value hashmap序列化
template.setHashValueSerializer(jackson2JsonRedisSerializer);
return template;
}
}4.写测试类
这里就简单一写
package com.mybatis;
import com.mybatis.entity.User;
import com.mybatis.service.IUserService;
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 java.util.List;
@SpringBootTest
class Mybatisplus02ApplicationTests {
@Autowired
IUserService userService;
@Autowired
private RedisTemplate redisTemplate;
@Test
public void test(){
Object users = redisTemplate.opsForValue().get("users");
if(users==null){
System.out.println("缓存没有需要查询数据库");
List<User> userList = userService.list();
System.out.println("将数据库查询的数据缓存到Redis");
redisTemplate.opsForValue().set("users",userList);
}else{
System.out.println("从redis缓存中查到的数据哦");
System.out.println(users);
}
}
}5.运行后报错
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisTemplate' defined in class path resource [com/csdn/mybatis/config/RedisConfig.class]: Unsatisfied dependency expressed through method 'redisTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisConnectionFactory' defined in class path resource问题解决
1.注释掉上面的redis配置类(注释掉,或者删除)
因为我们已入依赖后其就已经有了RedisTemplate,不需要我们在写。
2.注释掉侯我们再运行我们的测试类
我们会看到第一次查询会访问数据库查询语句


我们在运行第二次看看结果,其为从redis中查询的数据

其实到这里侯,我们完全可以这样使用redis了。不过下面的介绍也是有实际意义的哦。
查看redis中的key
使用keys *查询所有下的key值,发现竟然是乱码

解决key乱码问题
这就需要我们编写redis的配置类了,也就是之前我们写的那个配置类RedisConfig,如果你还是那样写的话,它还是会报错的,我们这里是想接管springboot自己的redisTemplate对象,因此我们需要写我们自己的,这样springboot的就会失效了,这个配置类的目的也是为了解决这个乱码。我们可以直接复制上面的RedisConfig配置类,然后按照红框内的的内容进行修改即可。

再测试
先将redis中的库清空以免影响结果

再去运行测试类,因为已经情况了redis库,所以这次会查询数据库并将结果写入redis库。
我们从redis里查看所有的keys *,发现这次不是乱码了

















