通过自定义RedisTemplate解决
package com.springboot.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.StringRedisSerializer;@ConfigurationpublicclassRedisConfig{@Beanpublic RedisTemplate<String, Object>redisTemplate(RedisConnectionFactory factory){
RedisTemplate<String, Object> template=newRedisTemplate();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer=newJackson2JsonRedisSerializer(Object.class);
ObjectMapper om=newObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer=newStringRedisSerializer();
template.setKeySerializer(stringRedisSerializer);
template.setHashKeySerializer(stringRedisSerializer);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();return template;}}
Redis工具类
package com.springboot.utils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Component;import org.springframework.util.CollectionUtils;import java.util.Collection;import java.util.List;import java.util.Map;import java.util.Set;import java.util.concurrent.TimeUnit;@ComponentpublicclassRedisUtil{@Autowiredprivate RedisTemplate<String, Object> redisTemplate;publicbooleanexpire(String key,long time){try{if(time>0){
redisTemplate.expire(key, time, TimeUnit.SECONDS);}returntrue;}catch(Exception e){
e.printStackTrace();returnfalse;}}publiclonggetExpire(String key){return redisTemplate.getExpire(key, TimeUnit.SECONDS);}publicbooleanhasKey(String key){try{return redisTemplate.hasKey(key);}catch(Exception e){
e.printStackTrace();returnfalse;}}@SuppressWarnings("unchecked")publicvoiddel(String... key){if(key!= null&& key.length>0){if(key.length==1){
redisTemplate.delete(key[0]);}else{
redisTemplate.delete((Collection<String>) CollectionUtils.arrayToList(key));}}}public Objectget(String key){return key== null? null: redisTemplate.opsForValue().get(key);}publicbooleanset(String key, Object value){try{
redisTemplate.opsForValue().set(key, value);returntrue;}catch(Exception e){
e.printStackTrace();returnfalse;}}publicbooleanset(String key, Object value,long time){try{if(time>0){
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);}else{set(key, value);}returntrue;}catch(Exception e){
e.printStackTrace();returnfalse;}}publiclongincr(String key,long delta){if(delta<0){thrownewRuntimeException("递增因子必须大于0");}return redisTemplate.opsForValue().increment(key, delta);}publiclongdecr(String key,long delta){if(delta<0){thrownewRuntimeException("递减因子必须大于0");}return redisTemplate.opsForValue().increment(key,-delta);}public Objecthget(String key, String item){return redisTemplate.opsForHash().get(key, item);}public Map<Object, Object>hmget(String key){return redisTemplate.opsForHash().entries(key);}publicbooleanhmset(String key, Map<String, Object> map){try{
redisTemplate.opsForHash().putAll(key, map);returntrue;}catch(Exception e){
e.printStackTrace();returnfalse;}}publicbooleanhmset(String key, Map<String, Object> map,long time){try{
redisTemplate.opsForHash().putAll(key, map);if(time>0){expire(key, time);}returntrue;}catch(Exception e){
e.printStackTrace();returnfalse;}}publicbooleanhset(String key, String item, Object value){try{
redisTemplate.opsForHash().put(key, item, value);returntrue;}catch(Exception e){
e.printStackTrace();returnfalse;}}publicbooleanhset(String key, String item, Object value,long time){try{
redisTemplate.opsForHash().put(key, item, value);if(time>0){expire(key, time);}returntrue;}catch(Exception e){
e.printStackTrace();returnfalse;}}publicvoidhdel(String key, Object... item){
redisTemplate.opsForHash().delete(key, item);}publicbooleanhHasKey(String key, String item){return redisTemplate.opsForHash().hasKey(key, item);}publicdoublehincr(String key, String item,double by){return redisTemplate.opsForHash().increment(key, item, by);}publicdoublehdecr(String key, String item,double by){return redisTemplate.opsForHash().increment(key, item,-by);}public Set<Object>sGet(String key){try{return redisTemplate.opsForSet().members(key);}catch(Exception e){
e.printStackTrace();return null;}}publicbooleansHasKey(String key, Object value){try{return redisTemplate.opsForSet().isMember(key, value);}catch(Exception e){
e.printStackTrace();returnfalse;}}publiclongsSet(String key, Object... values){try{return redisTemplate.opsForSet().add(key, values);}catch(Exception e){
e.printStackTrace();return0;}}publiclongsSetAndTime(String key,long time, Object... values){try{
Long count= redisTemplate.opsForSet().add(key, values);if(time>0)expire(key, time);return count;}catch(Exception e){
e.printStackTrace();return0;}}publiclongsGetSetSize(String key){try{return redisTemplate.opsForSet().size(key);}catch(Exception e){
e.printStackTrace();return0;}}publiclongsetRemove(String key, Object... values){try{
Long count= redisTemplate.opsForSet().remove(key, values);return count;}catch(Exception e){
e.printStackTrace();return0;}}public List<Object>lGet(String key,long start,long end){try{return redisTemplate.opsForList().range(key, start, end);}catch(Exception e){
e.printStackTrace();return null;}}publiclonglGetListSize(String key){try{return redisTemplate.opsForList().size(key);}catch(Exception e){
e.printStackTrace();return0;}}public ObjectlGetIndex(String key,long index){try{return redisTemplate.opsForList().index(key, index);}catch(Exception e){
e.printStackTrace();return null;}}publicbooleanlSet(String key, Object value){try{
redisTemplate.opsForList().rightPush(key, value);returntrue;}catch(Exception e){
e.printStackTrace();returnfalse;}}publicbooleanlSet(String key, Object value,long time){try{
redisTemplate.opsForList().rightPush(key, value);if(time>0)expire(key, time);returntrue;}catch(Exception e