RedisTemplate是在Jedis的基础上进行了封装
<!-- springboot redis整合-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 连接池 redis依赖commons-pool 这个依赖一定要加-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
配置文件:
application.yml
server:
port: 8080
spring:
application:
name: redisdemo02
redis:
host: 192.168.145.128
password: 123456
port: 6379
database: 0
测试链接:
@SpringBootTest
class Redisdemo02ApplicationTests {
@Resource
private RedisTemplate<Object,Object> redisTemplate;
@Test
void contextLoads() {
System.out.println(redisTemplate);
//向Redis存入键值对
redisTemplate.opsForValue().set("javasss","helloworld");
//从Redis中获取键值对
System.out.println(redisTemplate.opsForValue().get("javasss"));
}
}
查看数据库 数据之前还有一些东西
以上存在一些问题:
1.redis的key都是string类型,在模板的泛型中使用Object不太好 string类型value无法存对象
2.redis模板进行存取时:使用客户端窗口常看时 会发现数据之前还会多一些字符,这会导致无法删除
解决方案:
创建RedisConfig类,添加以下代码:
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
StringRedisSerializer stringRedisSerializer=new StringRedisSerializer();
template.setKeySerializer(stringRedisSerializer);
template.setValueSerializer(stringRedisSerializer);
return template;
}
}
UserServiceImpl:
@Service
@Slf4j
public class UserServiceImpl {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
/*
* RedisTemplate操作String类型
* */
/*
* 1.判断Redis是否存在key
* 若存在直接查询Redis
* 若不存在查询MYSQL讲查询结果存入Redis
* */
public String getUserName(String id){
//1.生成Redis key
String key="user:name:"+id;
if(redisTemplate.hasKey(key)){
log.info("查询的是redis");
Object o = redisTemplate.opsForValue().get(key);
return (String)o;
}else {
log.info("查询mysql");
String result="cxk";
redisTemplate.opsForValue().set(key,result);
return result;
}
}
/*
* RedisTemplate hash
*根据用户ID查询用户信息
* 1.根据用户ID生成Redis KEY
* 2.判断Redis中是否存在key
* 3.如果key不存在,查询MYSQL,讲查询的对象结果返回并再次存入Redis hash类型
* 4.如果key存在 直接查询Redis返回
* */
public User selectUserById(String id){
String key="user:"+id;
if(!redisTemplate.opsForHash().hasKey("user",id)){
log.info("mysql");
User user=new User();
user.setId(id);
user.setName("cxk");
user.setRemark(" like basketball");
redisTemplate.opsForHash().put("user",id,user);
return user;
}else{
log.info("redis");
User user =(User) redisTemplate.opsForHash().get("user", id);
return user;
}
}
}
测试:
@Autowired
private UserServiceImpl userService;
@Test
public void userservice(){
System.out.println(userService.getUserName("1001"));
System.out.println(userService.selectUserById("1001"));
}
查看Redis 发现Hash类型的数据前又出现没必要的东西
在Config中添加内容,修改Serializer配置
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
StringRedisSerializer stringRedisSerializer=new StringRedisSerializer();
template.setKeySerializer(stringRedisSerializer);
template.setValueSerializer(stringRedisSerializer);
// --------------------添加的内容
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer=new GenericJackson2JsonRedisSerializer();
template.setHashKeySerializer(stringRedisSerializer);
//此处的value是对象 不应是string 而是json格式
template.setHashValueSerializer(genericJackson2JsonRedisSerializer);
// ---------------------------------------
return template;
}
}
再次执行Hash的方法 数据正常