在【Redis Jedis实战(含Spring Boot)】基础上做如下修改:
1、子pom.xml
添加redisson依赖
<!-- <dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.10.0</version>
</dependency> -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.10.0</version>
</dependency>
注意:redisson-spring-boot-starter与spring-boot-starter-data-redis是有冲突的,redisson-spring-boot-starter中RedissonAutoConfiguration自动配置Bean: redisTemplate、stringRedisTemplate、redisConnectionFactory,spring-boot-starter-data-redis也自动配置了。
这里选择单独引入redisson。
2、Redisson单独实战
RedisUtil.java
package com.java.ashare.redis.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;
/**
* 一、redis自动配置类:RedisRepositoriesAutoConfiguration,可以查看到能配置的属性以及可以使用的bean
* RedisRepositoriesAutoConfiguration源码可知:
* 提供了RedisTemplate<Object, Object>和StringRedisTemplate两种RedisTemplate的bean
* 区别:
* RedisTemplate<Object, Object>
* keySerializer、valueSerializer、hashKeySerializer、hashValueSerializer
* 使用JdkSerializationRedisSerializer做序列化,用redis客户端查看key-value时显示一串十六进制的字符串,可读性很差
* 泛型是<Object, Object>,写代码不方便,需要写好多类型转换的代码,不推荐
*
* StringRedisTemplate
* 使用StringRedisSerializer做序列化,用redis客户端查看key-value时显示正常的字符串,缺点是key、value必须都是字符串
* 如果是对象的话,需要在程序中手动把对象数据转化为json字符串(fastjson)
*
* 综上:我们可以自定义bean---RedisTemplate<String, Object>,查看config/RedisConfig
*
*
* 二、RedisConnectionFactory实现LettuceConnectionFactory、JedisConnectionFactory
* springboot1.x默认使用JedisConnectionFactory
* springboot2.x默认使用LettuceConnectionFactory,可以通过debug调试查看变量可知
*/
@Component
public class RedisUtil {
// 自定义redisTemplate
// private static RedisTemplate&l