springboot整合redis

1.关键依赖


    org.springframework.boot
    spring-boot-starter-data-redis



    org.apache.commons
    commons-pool2


    com.alibaba
    fastjson
    1.2.67

2.配置文件

server:
  port: 9999
  servlet:
    context-path: /kgc

spring:
  redis:
    port: 6379
    host: localhost
  cache:
    type: redis                 # 缓存类型
    cache-names: xxx,yyy
    redis:
      cache-null-values: true  # 允许Redis缓存空值
      key-prefix: dacai_
      time-to-live: 0ms         # 缓存超时时间戳,配置为0则永不过期
      use-key-prefix: true      # 启用Redis的键前缀


  thymeleaf:
    cache: false
  datasource:
    username: root
    password: 1234
    url: jdbc:mysql:///db2109
    #type第三方连接池的主类
  mvc:
    format:
      date: yyyy-MM-dd
    hiddenmethod:
      filter:
        enabled: true

mybatis:
  mapper-locations: classpath:/mybatis/mapper/*.xml
  type-aliases-package: cn.kgc.*.entity
  configuration:
    map-underscore-to-camel-case: true

logging:
  level:
    cn.kgc: debug

3.config.RedisConfig类

@Configuration
@EnableCaching //启用缓存注解
public class RedisConfig extends CachingConfigurerSupport {

    /*@Autowired
    private RedisConnectionFactory connectionFactory;*/
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory){
        RedisTemplate redisTemplate = new RedisTemplate<>();

        redisTemplate.setConnectionFactory(connectionFactory);

        // 指定k/v的序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer<>(Object.class);
        redisTemplate.setValueSerializer(serializer);

        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        om.setTimeZone(TimeZone.getDefault());
        om.configure(MapperFeature.USE_ANNOTATIONS, false);
        om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        // 此项必须配置,否则会报java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to XXX
        //om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);//过时方法,用activateDefaultTyping替代
        om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance , ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        om.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        serializer.setObjectMapper(om);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(serializer);

        return  redisTemplate;
    }

    @Bean
    public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate) {
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory());
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer()));
        return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
    }
}
 
  

4. service.xx类impl

@Autowired
private RedisTemplate redisTemplate;

@Cacheable(cacheNames = "xxx",key = "'getEmpList'")
@Cacheable(cacheNames = "xxx",key = "'emp:id'+#id")

增改

@CachePut(cacheNames = "xxx",key = "'emp:id'+#shopping.id")

@CacheEvict(cacheNames = "xxx",key = "'emp:id'+#id")

你可能感兴趣的:(数据库开发)