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. Java中的静态方法和实例方法的调用的理解(不同的类下的方法调用)

    public class MethodCall { public static void main(String[] args) { Test.sayStatic(); Test test = new ...

  2. Log4net 配置文件组成

    Example: <?xml version="1.0" encoding="utf-8" ?><configuration><l ...

  3. C语言关键字分类整理

    C语言总览: 强类型,面向过程 简洁.灵活:32个关键字(C99标准新增5个,C11新增7个),9种控制语句,34种运算符 数据类型丰富,运算符丰富 结构化(控制语句).模块化(函数) 灵魂.特色:指 ...

  4. VmwareTools以及搜狗拼音的安装

    已经那么多年工作下来了,结果装linux还是那么 的费劲! 装的是纯净版Ubuntu16.04版本,17.04怕不稳定就没装, 装了发现VmwareTools是暗的,以前也遇到过这个问题,但是真的忘记 ...

  5. There is no getter for property named 'user' in 'class com.jyr.wh.domain.User' 异常

    今天在使用mybatis时,出现了一个问题:There is no getter for property named 'user' in 'class com.jyr.wh.domain.User, ...

  6. QWaitConditioin的思考1

    16:50:392017-03-31 本文章为原创,如若引用请指明出处,否则追责. 以 生产.消费者的例子说明 #include <QtCore/QCoreApplication>#inc ...

  7. Failed to load ApplicationContext ,Error creating bean with name 'adminUserService': Injection of autowired dependencies failed;

    Druid配置的时候出现这个问题: "C:\Program Files\Java\jdk1.8.0_191\bin\java" -ea -Didea.test.cyclic.buf ...

  8. 使用spark访问hive错误记录

    在spark集群中执行./spark-shell时报以下错误: 18/07/23 10:02:39 WARN DataNucleus.Connection: BoneCP specified but ...

  9. QT | 一些学习心得

    1. 如何使控件随着窗口大小变化而自动填充? 选中控件中最外层的那个控件,如centralWidget(不要去选中内部的小控件,这样能够保证内部的相对位置) 然后对centralWidget选择布局方 ...

  10. ubuntu18.04LTS配置apache虚拟目录(基于端口)

    假设在用户目录下要新建两个虚拟目录web1,web2,分别对应端口8081,8082 $ cd ~ #切换到用户目录 $ mkdir www #新建www文件夹,以后新建的虚拟目录都放在该文件夹下 $ ...