import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component; import javax.annotation.Resource; /**
* Created with IntelliJ IDEA.
* Description:
* User: mingyanliao
* Date: 2018-11-20
* Time: 下午7:52
*/
@Component
public class CacheConfig { //String json
@Resource
private RedisTemplate<String,String> jsonRedisTemplate; @Bean(name="redisWithSerial")
public RedisTemplate<Object, Object> getRedisTemplate(RedisTemplate<Object, Object> redisTemplate) {
RedisSerializer<String> stringSerializer = new StringRedisSerializer();
//json序列化方式
GenericJackson2JsonRedisSerializer jsonRedisSerializer =new GenericJackson2JsonRedisSerializer(); redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setValueSerializer(jsonRedisSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
redisTemplate.setHashValueSerializer(jsonRedisSerializer);
return redisTemplate;
}
}

键值使用StringRedisSerializer,value使用GenericJackson2JsonRedisSerializer

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable; /**
* <p>
* Mapper 接口
* </p>
*
* @author myliao
* @since 2018-11-20
*/
@Mapper
public interface MessageAutoanswerMapper extends BaseMapper<MessageAutoanswer> { @Cacheable(value = "panda_autoanswer",key="'m_'+#p0+'_'+#p1+'_'+#p2")
@Select("select * from panda_wechat_message_autoanswer where wechat_id=#{wechatId} and subscribe_msg_type=#{msg_type} and msg_rule=#{rule}")
public MessageAutoanswer getMessageAutoanswer(@Param("wechatId")Long wechatId, @Param("msg_type")String msg_type, @Param("rule")String rule); @CacheEvict(value = "panda_autoanswer",key="'m_'+#p0.wechatId+'_'+#p0.subscribeMsgType+'_'+#p0.msgRule")
@Override
public int insert(MessageAutoanswer messageAutoanswer);
}

  注意: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. JavaScript 获得客户端IP

    Below are all the free active IP lookup services I could find and the information they return. If yo ...

  2. 2018-2019-2 《网络对抗技术》Exp6 信息搜集与漏洞扫描 20165326

    信息搜集与漏洞扫描 实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 基础知识 间接:不接触目标,无直接连接访问,使用辅助模块进行收集分析 DNS:执行各种相关查询 搜索引擎 直接:建立逻辑连 ...

  3. java 开发环境配置 安装 MyEclipse

    一.下载MyEclipse开发工具 下载地址:http://www.myeclipsecn.com 需要注册帐号,登录后点击下载

  4. arcgis 添加经纬度坐标点

    ArcGIS 10.2导入X.Y坐标(经度.纬度),生成Shapefile点数据   参考博客很完整 基站-纬度-经度 1---- 2----X 经度  Y 纬度  Z高程(没有) 3---一开始只定 ...

  5. Angular/cli 安装(windows环境)。

    1.卸载先前安装的Angular/cli npm uninstall -g angular-clinpm uninstall --save-dev angular-clinpm uninstall - ...

  6. Java前后端依赖

    有时候我们的一个类需要依赖另外一个类,这种就是依赖关系,创建对象的工作一般由spring容器来完成然后注入给调用者,这种就是依赖注入. 代码可参考1227210565朋友空间 DispatcherSe ...

  7. 新增和编辑clob字段

    #region 新的数据新增和修改方法 /// <summary> /// 添加信息 /// </summary> /// <returns></return ...

  8. javaScript 深拷贝、浅拷贝

    在 JS 中有一些基本类型像是Number.String.Boolean,而对象就是像这样的东西{ name: 'Larry', skill: 'Node.js' },对象跟基本类型最大的不同就在于他 ...

  9. 前端跨域(一):CORS

    上周做了一个移动端表单提交的页面,其中涉及到了跨域问题,想来也是惭愧,因为之前一直都没有遇到过这个问题,因此都没有深入探索过,只是知道有哪几种方式,这次终于借这个机会可以把遗留的知识点补一补了. 1. ...

  10. Android KitKat Immersive Mode使用

    写了一个方法,在onCreate和onResume中调用即可,4.4以上可用. private void openImmersiveMode() { if (android.os.Build.VERS ...