核心关键在于定义一个RedisCache实现mytis实现的Cache接口

 **
* @author tele
* @Description RedisCache由于需要传入id, 由mybatis进行创建,所以如果需要为RedisCache注入RedisTemplateUtil,直接使用@Autowired无效
* @create 2019-09-07
*/
public class RedisCache implements Cache { private static RedisTemplateUtil templateUtil; public static void setTemplateUtil(RedisTemplateUtil templateUtil) {
RedisCache.templateUtil = templateUtil;
} //这个id实际是传入的mapper全限定名
private final String id; public RedisCache(final String id) {
if (id == null) {
throw new IllegalArgumentException("Cache instances require an ID");
}
this.id = id;
} public String getId() {
return id;
} public void putObject(Object key, Object value) {
templateUtil.set(key, value);
} public Object getObject(Object key) {
return templateUtil.get(key);
} public Object removeObject(Object key) {
return null;
} public void clear() {
templateUtil.clear();
} public int getSize() {
return templateUtil.getSize();
} public ReadWriteLock getReadWriteLock() {
return null;
}
}

定义中间类,注入工具类

 @Component
public class RedisCacheTransfer {
@Autowired
private RedisTemplateUtil redisTemplateUtil; @Autowired
public void setRedisTemplateUtil(){
RedisCache.setTemplateUtil(redisTemplateUtil);
}
}

工具类

 /**
* @author tele
* @Description
* @create 2019-09-09
*/
@Component
public class RedisTemplateUtil {
@Autowired
private RedisTemplate template; public Object get(Object key) {
return template.opsForValue().get(key);
} public void set(Object key,Object value){
template.opsForValue().set(key, value);
} public int getSize() {
int size = (int) template.execute((RedisCallback<Integer>) connection -> {
return connection.dbSize().intValue();
});
return size;
} public void clear() {
template.execute((RedisCallback) connection -> {
connection.flushDb();
return null;
});
}
}

在对应的mapper.xml中添加cache标签

测试的时候还是先加载ClassPathXmlContext,然后获得sqlSession,注意mybatis的增删改,flushCache=true,可如果你没有调用commit并不会清空缓存

applicationContext.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="cn.tele"/> <context:property-placeholder location="classpath:redis.properties,db.properties"/> <!--数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${db.url}"/>
<property name="driverClass" value="${db.driver}"/>
<property name="user" value="${db.username}"/>
<property name="password" value="${db.password}"/>
<property name="initialPoolSize" value="${db.initialPoolSize}"/>
<property name="maxPoolSize" value="${db.maxPoolSize}"/>
<property name="maxIdleTime" value="${db.maxIdleTime}"/>
<property name="idleConnectionTestPeriod" value="${db.idleConnectionTestPeriod}"/>
<property name="testConnectionOnCheckout" value="true"/>
</bean> <!--mybatis-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="mapperLocations" value="classpath*:cn/tele/bean/**/*.xml"/>
<property name="dataSource" ref="dataSource"/>
<property name="configurationProperties">
<props>
<prop key="cacheEnabled">true</prop>
<prop key="jdbcTypeForNull">NULL</prop>
<prop key="lazyLoadingEnabled">true</prop>
<prop key="aggressiveLazyLoading">false</prop>
</props>
</property>
</bean> <!--扫描mapper-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="cn.tele"/>
</bean> <!--redis连接池属性设置-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxTotal" value="${redis.maxTotal}"/>
</bean> <bean id="jedisConnFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:poolConfig-ref="poolConfig" p:port="${redis.port}" p:hostName="${redis.host}"/> <bean id="redisTemplate"
class="org.springframework.data.redis.core.StringRedisTemplate"
p:connection-factory-ref="jedisConnFactory">
<!--序列化策略-->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer">
<constructor-arg type="java.lang.Class" value="java.lang.String"/>
</bean>
</property> <property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"> </bean>
</property> <property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property> <property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
</bean>
</beans>

mybatis 整合redis作为二级缓存的更多相关文章

  1. Mybatis整合Redis实现二级缓存

    Mybatis集成ehcache . 为什么需要缓存 拉高程序的性能 . 什么样的数据需要缓存 很少被修改或根本不改的数据 业务场景比如:耗时较高的统计分析sql.电话账单查询sql等 . ehcac ...

  2. SSM+redis整合(mybatis整合redis做二级缓存)

    SSM:是Spring+Struts+Mybatis ,另外还使用了PageHelper 前言: 这里主要是利用redis去做mybatis的二级缓存,mybaits映射文件中所有的select都会刷 ...

  3. mybatis 使用redis实现二级缓存(spring boot)

    mybatis 自定义redis做二级缓存 前言 如果关注功能实现,可以直接看功能实现部分 何时使用二级缓存 一个宗旨---不常变的稳定而常用的 一级是默认开启的sqlsession级别的. 只在单表 ...

  4. SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置

    2016年03月03日 10:37:47 标签: mysql / redis / mybatis / spring mvc / spring 33805 项目环境: 在SpringMVC + MyBa ...

  5. SpringMVC +Spring + MyBatis + Mysql + Redis(作为二级缓存) 配置

    转载:http://blog.csdn.net/xiadi934/article/details/50786293 项目环境: 在SpringMVC +Spring + MyBatis + MySQL ...

  6. SpringBank 开发日志 Mybatis 使用redis 作为二级缓存时,无法通过cacheEnabled=false 将其关闭

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC ...

  7. mybatis结合redis实战二级缓存(六)

    之前的文章中我们意见分析了一级缓存.二级缓存的相关源码和基本原理,今天我们来分享下了mybatis二级缓存和redis的结合,当然mybatis二级缓存也可以和ehcache.memcache.OSC ...

  8. mybatis结合redis实战二级缓存

    之前的文章中我们意见分析了一级缓存.二级缓存的相关源码和基本原理,今天我们来分享下了mybatis二级缓存和redis的结合,当然mybatis二级缓存也可以和ehcache.memcache.OSC ...

  9. springboot+mybatis 用redis作二级缓存

    1.加入相关依赖包: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...

随机推荐

  1. 通过 FastAdmin 理解开源软件

    通过 FastAdmin 理解开源软件 开源软件 ≠ 免费软件,免费是遵循其开源协议下的一个特性. 开源软件虽然免费,但服务是可以收费的,因为房子要钱. 开源的目的是为了用户更自由. 做开源每天会遇到 ...

  2. [linux]vmware中linux虚拟机扩容 标签: vmware虚拟机linux 2016-09-05 08:03 315人阅读 评

    扩容原因 现阶段,虚拟机的标配都是1G内存和20G硬盘,大部分时候是够用的,但是也会出现虚拟机里面东西放多了硬盘不够用的情况,这种情况下,除了清理垃圾,另外就只能给虚拟机扩容了.因为window扩容相 ...

  3. Java练习 SDUT-2561_九九乘法表

    九九乘法表 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 九九乘法表是数学学习的基础,今天我们就来看看乘法表的相关问题 ...

  4. Java练习 SDUT-1255_小明A+B

    小明A+B Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 小明今年3岁了, 现在他已经能够认识100以内的非负整数, ...

  5. SQL注入原理讲解,很不错!

    SQL注入原理讲解,很不错! 原文地址:http://www.cnblogs.com/rush/archive/2011/12/31/2309203.html 1.1.1 摘要 日前,国内最大的程序员 ...

  6. HDOJ1016 Prime Ring Problem(DFS深层理解)

      Prime Ring Problem                                                                       时间限制: 200 ...

  7. Oracle中的Union、Union All、Intersect、Minus[转]

    众所周知的几个结果集集合操作命令,今天详细地测试了一下,发现一些问题,记录备考. 假设我们有一个表Student,包括以下字段与数据: drop table student; create table ...

  8. HDFS概念名称节点和数据节点-名称节点-文件系统元数据的持久状态

  9. [***]HZOJ 跳房子

    一道非常神仙的题. 算法一:对于20%的数据: 模拟,直接走K步,时间复杂度O(K) 算法二:对于40%的数据:走M*N步内必有一个循环节.直接走,找循环节,时间复杂度O(M*N) 正解大概有两种做法 ...

  10. CSS属性Display(显示)和Visibility(可见性)

    隐藏一个元素可以通过把display属性设置为“none”,或把visibility属性设置为“hidden”.但是请注意,这两种方法会产生不同的效果. Visibility:hidden可以隐藏某个 ...