springboot项目使用redis注解存入缓存乱码
第一次接触redis,自己碰了好多回壁。下面是乱码图
下面是配置成功了的redis缓存图
一、重要的redis配置类
package com.tt.springboot.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.cache.CacheProperties; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext; 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缓存配置类 * */ @Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport{ @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.password}") private String password; @Bean public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) { CacheProperties.Redis redisProperties = cacheProperties.getRedis(); RedisCacheConfiguration config = RedisCacheConfiguration .defaultCacheConfig(); config = config.serializeValuesWith(RedisSerializationContext.SerializationPair .fromSerializer(new GenericJackson2JsonRedisSerializer())); if (redisProperties.getTimeToLive() != null) { config = config.entryTtl(redisProperties.getTimeToLive()); } if (redisProperties.getKeyPrefix() != null) { config = config.prefixKeysWith(redisProperties.getKeyPrefix()); } if (!redisProperties.isCacheNullValues()) { config = config.disableCachingNullValues(); } if (!redisProperties.isUseKeyPrefix()) { config = config.disableKeyPrefix(); } return config; } }
网上又很多这个的配置类,但是我试了很多都依旧是乱码,只有这个好用。虽然我也看不懂!
二、serviceimpl类的注解使用。
package com.tt.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import com.tt.mapper.UserMapper; import com.tt.model.User; @Service public class UserserviceImpl implements UserService{ @Autowired private UserMapper userMapper; @Cacheable(cacheNames="test",key="#user.id") public User selectByUsername(String username) { User user = userMapper.selectByUsername(username); System.out.println("select"); return user; } @CachePut(cacheNames="test",key="#user.username") public User insertSelective(User user) { userMapper.insertSelective(user); return user; } }
需要注意的是:
Cacheable注解方法的返回值需要与CachePut注解方法的返回值一致。否则即使Cacheable存入不乱码,但CachePut依旧乱码,而且还不会更新数据。