一、springboot自带的
org.springframework.bootspring-boot-starter-data-redis
spring:redis:host: 127.0.0.1database: 0port: 6379password: 1234jedis:pool:# 连接池最大连接数(使用负值表示没有限制)max-active: 8# 连接池最大阻塞等待时间(使用负值表示没有限制)max-wait: 0# 连接池中的最大空闲连接max-idle: 500# 连接池中的最小空闲连接min-idle: 0
@AutowiredRedisTemplate redisTemplate;// 方式一:用springboot自带的 redisTemplate@Testpublic void test() throws SQLException {System.out.println("111111111111111");redisTemplate.opsForValue().set("name","dgx22");System.out.println(redisTemplate.opsForValue().get("name"));}
二、方式二,新建两个bean
package com.hy.oa.config;
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.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
/*** redis配置类
bluewelkin**/
@Configuration
public class RedisConfig {@Beanpublic RedisTemplate redisTemplate(RedisConnectionFactory factory) {RedisTemplate template = new RedisTemplate();template.setConnectionFactory(factory);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);StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();// key采用String的序列化方式template.setKeySerializer(stringRedisSerializer);// hash的key也采用String的序列化方式template.setHashKeySerializer(stringRedisSerializer);// value序列化方式采用jacksontemplate.setValueSerializer(jackson2JsonRedisSerializer);// hash的value序列化方式采用jacksontemplate.setHashValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();return template;}
}
package com.hy.oa.config;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
/**
bluewelkin直接用RedisTemplate操作Redis,需要很多行代码,因此直接封装好一个RedisUtils,这样写代码更方便点。这个RedisUtils交给Spring容器实例化,使用时直接注解注入。*/
@Component
public final class RedisUtil {&#64;Autowiredprivate RedisTemplate redisTemplate;// &#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;common&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;/*** 指定缓存失效时间* &#64;param key 键* &#64;param time 时间(秒)* &#64;return*/public boolean expire(String key, long time) {try {if (time > 0) {redisTemplate.expire(key, time, TimeUnit.SECONDS);}return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 根据key 获取过期时间* &#64;param key 键 不能为null* &#64;return 时间(秒) 返回0代表为永久有效*/public long getExpire(String key) {return redisTemplate.getExpire(key, TimeUnit.SECONDS);}/*** 判断key是否存在* &#64;param key 键* &#64;return true 存在 false不存在*/public boolean hasKey(String key) {try {return redisTemplate.hasKey(key);} catch (Exception e) {e.printStackTrace();return false;}}/*** 删除缓存* &#64;param key 可以传一个值 或多个*/&#64;SuppressWarnings("unchecked")public void del(String... key) {if (key !&#61; null && key.length > 0) {if (key.length &#61;&#61; 1) {redisTemplate.delete(key[0]);} else {redisTemplate.delete(CollectionUtils.arrayToList(key));}}}// &#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;String&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;/*** 普通缓存获取* &#64;param key 键* &#64;return 值*/public Object get(String key) {String value &#61; "";System.out.println(redisTemplate.opsForValue().get(key));return value &#61;&#61; null ? null : redisTemplate.opsForValue().get(key);}/*** 普通缓存放入* &#64;param key 键* &#64;param value 值* &#64;return true成功 false失败*/public boolean set(String key, Object value) {try {redisTemplate.opsForValue().set(key, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 普通缓存放入并设置时间* &#64;param key 键* &#64;param value 值* &#64;param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期* &#64;return true成功 false 失败*/public boolean set(String key, Object value, long time) {try {if (time > 0) {redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);} else {set(key, value);}return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 递增* &#64;param key 键* &#64;param delta 要增加几(大于0)* &#64;return*/public long incr(String key, long delta) {if (delta <0) {throw new RuntimeException("递增因子必须大于0");}return redisTemplate.opsForValue().increment(key, delta);}/*** 递减* &#64;param key 键* &#64;param delta 要减少几(小于0)* &#64;return*/public long decr(String key, long delta) {if (delta <0) {throw new RuntimeException("递减因子必须大于0");}return redisTemplate.opsForValue().increment(key, -delta);}// &#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;Map&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;/*** HashGet* &#64;param key 键 不能为null* &#64;param item 项 不能为null* &#64;return 值*/public Object hget(String key, String item) {return redisTemplate.opsForHash().get(key, item);}/*** 获取hashKey对应的所有键值* &#64;param key 键* &#64;return 对应的多个键值*/public Map hmget(String key) {return redisTemplate.opsForHash().entries(key);}/*** HashSet* &#64;param key 键* &#64;param map 对应多个键值* &#64;return true 成功 false 失败*/public boolean hmset(String key, Map map) {try {redisTemplate.opsForHash().putAll(key, map);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** HashSet 并设置时间* &#64;param key 键* &#64;param map 对应多个键值* &#64;param time 时间(秒)* &#64;return true成功 false失败*/public boolean hmset(String key, Map map, long time) {try {redisTemplate.opsForHash().putAll(key, map);if (time > 0) {expire(key, time);}return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 向一张hash表中放入数据,如果不存在将创建* &#64;param key 键* &#64;param item 项* &#64;param value 值* &#64;return true 成功 false失败*/public boolean hset(String key, String item, Object value) {try {redisTemplate.opsForHash().put(key, item, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 向一张hash表中放入数据,如果不存在将创建* &#64;param key 键* &#64;param item 项* &#64;param value 值* &#64;param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间* &#64;return true 成功 false失败*/public boolean hset(String key, String item, Object value, long time) {try {redisTemplate.opsForHash().put(key, item, value);if (time > 0) {expire(key, time);}return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 删除hash表中的值* &#64;param key 键 不能为null* &#64;param item 项 可以使多个 不能为null*/public void hdel(String key, Object... item) {redisTemplate.opsForHash().delete(key, item);}/*** 判断hash表中是否有该项的值* &#64;param key 键 不能为null* &#64;param item 项 不能为null* &#64;return true 存在 false不存在*/public boolean hHasKey(String key, String item) {return redisTemplate.opsForHash().hasKey(key, item);}/*** hash递增 如果不存在,就会创建一个 并把新增后的值返回* &#64;param key 键* &#64;param item 项* &#64;param by 要增加几(大于0)* &#64;return*/public double hincr(String key, String item, double by) {return redisTemplate.opsForHash().increment(key, item, by);}/*** hash递减* &#64;param key 键* &#64;param item 项* &#64;param by 要减少记(小于0)* &#64;return*/public double hdecr(String key, String item, double by) {return redisTemplate.opsForHash().increment(key, item, -by);}// &#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;set&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;/*** 根据key获取Set中的所有值* &#64;param key 键* &#64;return*/public Set sGet(String key) {try {return redisTemplate.opsForSet().members(key);} catch (Exception e) {e.printStackTrace();return null;}}/*** 根据value从一个set中查询,是否存在* &#64;param key 键* &#64;param value 值* &#64;return true 存在 false不存在*/public boolean sHasKey(String key, Object value) {try {return redisTemplate.opsForSet().isMember(key, value);} catch (Exception e) {e.printStackTrace();return false;}}/*** 将数据放入set缓存* &#64;param key 键* &#64;param values 值 可以是多个* &#64;return 成功个数*/public long sSet(String key, Object... values) {try {return redisTemplate.opsForSet().add(key, values);} catch (Exception e) {e.printStackTrace();return 0;}}/*** 将set数据放入缓存* &#64;param key 键* &#64;param time 时间(秒)* &#64;param values 值 可以是多个* &#64;return 成功个数*/public long sSetAndTime(String key, long time, Object... values) {try {Long count &#61; redisTemplate.opsForSet().add(key, values);if (time > 0)expire(key, time);return count;} catch (Exception e) {e.printStackTrace();return 0;}}/*** 获取set缓存的长度* &#64;param key 键* &#64;return*/public long sGetSetSize(String key) {try {return redisTemplate.opsForSet().size(key);} catch (Exception e) {e.printStackTrace();return 0;}}/*** 移除值为value的* &#64;param key 键* &#64;param values 值 可以是多个* &#64;return 移除的个数*/public long setRemove(String key, Object... values) {try {Long count &#61; redisTemplate.opsForSet().remove(key, values);return count;} catch (Exception e) {e.printStackTrace();return 0;}}// &#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;list&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;/*** 获取list缓存的内容* &#64;param key 键* &#64;param start 开始* &#64;param end 结束 0 到 -1代表所有值* &#64;return*/public List lGet(String key, long start, long end) {try {return redisTemplate.opsForList().range(key, start, end);} catch (Exception e) {e.printStackTrace();return null;}}/*** 获取list缓存的长度* &#64;param key 键* &#64;return*/public long lGetListSize(String key) {try {return redisTemplate.opsForList().size(key);} catch (Exception e) {e.printStackTrace();return 0;}}/*** 通过索引 获取list中的值* &#64;param key 键* &#64;param index 索引 index>&#61;0时&#xff0c; 0 表头&#xff0c;1 第二个元素&#xff0c;依次类推&#xff1b;index<0时&#xff0c;-1&#xff0c;表尾&#xff0c;-2倒数第二个元素&#xff0c;依次类推* &#64;return*/public Object lGetIndex(String key, long index) {try {return redisTemplate.opsForList().index(key, index);} catch (Exception e) {e.printStackTrace();return null;}}/*** 将list放入缓存* &#64;param key 键* &#64;param value 值* &#64;param time 时间(秒)* &#64;return*/public boolean lSet(String key, Object value) {try {redisTemplate.opsForList().rightPush(key, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 将list放入缓存* &#64;param key 键* &#64;param value 值* &#64;param time 时间(秒)* &#64;return*/public boolean lSet(String key, Object value, long time) {try {redisTemplate.opsForList().rightPush(key, value);if (time > 0)expire(key, time);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 将list放入缓存* &#64;param key 键* &#64;param value 值* &#64;param time 时间(秒)* &#64;return*/public boolean lSet(String key, List value) {try {redisTemplate.opsForList().rightPushAll(key, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 将list放入缓存** &#64;param key 键* &#64;param value 值* &#64;param time 时间(秒)* &#64;return*/public boolean lSet(String key, List value, long time) {try {redisTemplate.opsForList().rightPushAll(key, value);if (time > 0)expire(key, time);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 根据索引修改list中的某条数据* &#64;param key 键* &#64;param index 索引* &#64;param value 值* &#64;return*/public boolean lUpdateIndex(String key, long index, Object value) {try {redisTemplate.opsForList().set(key, index, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 移除N个值为value* &#64;param key 键* &#64;param count 移除多少个* &#64;param value 值* &#64;return 移除的个数*/public long lRemove(String key, long count, Object value) {try {Long remove &#61; redisTemplate.opsForList().remove(key, count, value);return remove;} catch (Exception e) {e.printStackTrace();return 0;}}
}
测试&#xff1a;
// 方式二&#xff1a;在需要操作Redis的类中通过&#64;Autowired注解引人RedisUtil Bean进行使用。 new一个对象还不行&#xff0c;报错。&#64;Autowiredprivate RedisUtil redisUtil;&#64;Testpublic void test2() {redisUtil.get("name");}&#64;Te
代码参考&#xff1a;https://github.com/bluewelkin/springboot_redis
另外 springboot本身整合redis也有两种方式
首先我们要知道&#xff0c;Springboot整合Redis有两种方式&#xff0c;分别是Jedis和RedisTemplate&#xff0c;这两者有何区别&#xff1f;
Jedis是Redis官方推荐的面向Java的操作Redis的客户端&#xff0c;而RedisTemplate是SpringDataRedis中对JedisApi的高度封装。其实在Springboot的官网上我们也能看到&#xff0c;官方现在推荐的是SpringDataRedis形式&#xff0c;相对于Jedis来说可以方便地更换Redis的Java客户端&#xff0c;其比Jedis多了自动管理连接池的特性&#xff0c;方便与其他Spring框架进行搭配使用如&#xff1a;SpringCache。
在这之前我在低版本的Springboot项目中一直使用Jedis操作Redis&#xff0c;但是我也意识到这种做法已经过时了&#xff0c;所以在升级了Springboot版本后特地在此篇中详细梳理一下&#xff0c;以后项目都会使用这个版本的整合方案&#xff0c;不再使用Jedis方式&#xff0c;不过我还是会再写一篇使用Jedis操作Redis的文章&#xff0c;因为从中能很清楚的看到其基本的工作方式&#xff0c;对Redis连接池的手动管理都能更清晰地体现出来。
参考文章&#xff1a;https://blog.csdn.net/zhulier1124/article/details/82154937