1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.data.redis.core.RedisTemplate;
  3. import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
  4. import org.springframework.data.redis.serializer.RedisSerializer;
  5. import org.springframework.data.redis.serializer.StringRedisSerializer;
  6. import org.springframework.stereotype.Component;
  7.  
  8. import javax.annotation.Resource;
  9.  
  10. /**
  11. * Created with IntelliJ IDEA.
  12. * Description:
  13. * User: mingyanliao
  14. * Date: 2018-11-20
  15. * Time: 下午7:52
  16. */
  17. @Component
  18. public class CacheConfig {
  19.  
  20. //String json
  21. @Resource
  22. private RedisTemplate<String,String> jsonRedisTemplate;
  23.  
  24. @Bean(name="redisWithSerial")
  25. public RedisTemplate<Object, Object> getRedisTemplate(RedisTemplate<Object, Object> redisTemplate) {
  26. RedisSerializer<String> stringSerializer = new StringRedisSerializer();
  27. //json序列化方式
  28. GenericJackson2JsonRedisSerializer jsonRedisSerializer =new GenericJackson2JsonRedisSerializer();
  29.  
  30. redisTemplate.setKeySerializer(stringSerializer);
  31. redisTemplate.setValueSerializer(jsonRedisSerializer);
  32. redisTemplate.setHashKeySerializer(stringSerializer);
  33. redisTemplate.setHashValueSerializer(jsonRedisSerializer);
  34. return redisTemplate;
  35. }
  36. }

键值使用StringRedisSerializer,value使用GenericJackson2JsonRedisSerializer

  1. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  2. import org.apache.ibatis.annotations.Mapper;
  3. import org.apache.ibatis.annotations.Param;
  4. import org.apache.ibatis.annotations.Select;
  5. import org.springframework.cache.annotation.CacheConfig;
  6. import org.springframework.cache.annotation.CacheEvict;
  7. import org.springframework.cache.annotation.Cacheable;
  8.  
  9. /**
  10. * <p>
  11. * Mapper 接口
  12. * </p>
  13. *
  14. * @author myliao
  15. * @since 2018-11-20
  16. */
  17. @Mapper
  18. public interface MessageAutoanswerMapper extends BaseMapper<MessageAutoanswer> {
  19.  
  20. @Cacheable(value = "panda_autoanswer",key="'m_'+#p0+'_'+#p1+'_'+#p2")
  21. @Select("select * from panda_wechat_message_autoanswer where wechat_id=#{wechatId} and subscribe_msg_type=#{msg_type} and msg_rule=#{rule}")
  22. public MessageAutoanswer getMessageAutoanswer(@Param("wechatId")Long wechatId, @Param("msg_type")String msg_type, @Param("rule")String rule);
  23.  
  24. @CacheEvict(value = "panda_autoanswer",key="'m_'+#p0.wechatId+'_'+#p0.subscribeMsgType+'_'+#p0.msgRule")
  25. @Override
  26. public int insert(MessageAutoanswer messageAutoanswer);
  27. }

  注意:Cacheable的value是指缓存名称,即redis里面的分组名称

springboot Cacheable(redis),解决key乱码问题的更多相关文章

  1. 【springBoot】springBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  2. SpringBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  3. SpringBoot2.X + SpringCache + redis解决乱码问题

    环境:SpringBoot2.X + SpringCache + Redis Spring boot默认使用的是SimpleCacheConfiguration,使用ConcurrentMapCach ...

  4. SpringBoot整合Redis在可视化工具乱码问题,以及常用的api

     pom依赖: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spr ...

  5. SpringBoot整合redis实现过期key监听事件

    Spring整合redis实现key过期事件监听:https://www.cnblogs.com/pxblog/p/13969375.html 可以用于简单的过期订单取消支付.7天自动收货场景中 1. ...

  6. 带着新人学springboot的应用04(springboot+mybatis+redis 完)

    对于缓存也说了比较多了,大家对下图这一堆配置类现在应该有些很粗略的认识了(因为我也就很粗略的认识了一下,哈哈!),咳,那么我们怎么切换这个缓存呢?(就是不用springboot提供的默认的Simple ...

  7. SpringBoot整合Redis及Redis工具类撰写

            SpringBoot整合Redis的博客很多,但是很多都不是我想要的结果.因为我只需要整合完成后,可以操作Redis就可以了,并不需要配合缓存相关的注解使用(如@Cacheable). ...

  8. 28. SpringBoot 集成Redis

    1.引入依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  9. SpringBoot集成Redis分布式锁以及Redis缓存

    https://blog.csdn.net/qq_26525215/article/details/79182687 集成Redis 首先在pom.xml中加入需要的redis依赖和缓存依赖 < ...

随机推荐

  1. Hyperledger Fabric 建立一个简单网络

    Building you first network 网络结构: 2个Orgnizations(每个Org包含2个peer节点)+1个solo ordering service 打开fabric-sa ...

  2. iOS Build Apps for the World WWDC

    Programming Resources https://developer.apple.com/internationalization/ WWDC Session Videos Internat ...

  3. python3 进行字符串、日期、时间、时间戳相关转换

    1.字符串转换成时间戳 2. 日期转换成时间戳

  4. 设置eclipse联想功能

    当我们在用eclipse的时候,怎么能够让自己的编码速度加快?我想利用eclipse的提示功能是其中的方法之一.下面就利出配置eclipse联想功能(代码的提示功能)的步骤: 1. 打开Eclipse ...

  5. 【原创】Arduino入门基础知识总结

     一.概述   Arduino是源自意大利的一个开放源代码的硬件项目平台,该平台包括一块具备简单I/O功能的电路板以及一套程序开发环境软件. Arduino可以用来开发交互产品,比如它可以读取大量的开 ...

  6. c++单链表冒泡排序(交换结点),链表增删改查,运算符重载

    #include <iostream> #include <stdlib.h> #include <time.h> #include <fstream> ...

  7. 【温故知新】HTTP请求GET和POST的用法和区别

    转自http://www.w3school.com.cn GET的使用 请注意,查询字符串(名称/值对)是在 GET 请求的 URL 中发送的: /test/demo_form.asp?name1=v ...

  8. WPF-在Grid中编程添加控件

    摘要: 在Grid中编程添加控件,布局控件的位置用到的代码是: gridLayout.Children.Add(textblock); Grid.SetRow(textblock, ); Grid.S ...

  9. 固态硬盘Ghost安装Windows 10无法引导的问题

    机器配置如下: 电脑型号 技嘉 B360M POWER 台式电脑 操作系统 Windows 10 64位 ( DirectX 12 ) 处理器 英特尔 Core i7-8700 @ 3.20GHz 六 ...

  10. JSP中重定向页面没有全屏显示的问题解决

    <script type="text/javascript"> window.onload=function(){ if(window.parent != window ...