核心关键在于定义一个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. 【时光回溯】【JZOJ3571】【GDKOI2014】内存分配

    题目描述 输入 输出 输出m行,每行一个整数,代表输入中每次程序变化后系统所需要的空闲内存单位数. 样例输入 2 3 1 4 1 4 2 2 1 2 1 1 1 1 1 样例输出 2 3 1 数据范围 ...

  2. 2019.9.19登陆注册猜数字给奖品combo

    #注册函数 def register(): registor_count = 0 while registor_count < 3: username_inp = input('user nam ...

  3. Directx11学习笔记【二十一】 封装键盘鼠标响应类

    原文:Directx11学习笔记[二十一] 封装键盘鼠标响应类 摘要: 本文由zhangbaochong原创,转载请注明出处:http://www.cnblogs.com/zhangbaochong/ ...

  4. UIView 判断是否visible

    if(self.view == [(MyAppDelegate *)[[UIApplication sharedApplication] delegate].window.subviews objec ...

  5. 亲测的orabbix监控Oracle过程

    网上教程很多,但普遍较老,担心新版本的变化,于是亲自测试了一下,记录过程如下: 一.环境 Centos7.3 192.168.56.104 Oracle安装在windows下,192.168.56.1 ...

  6. python 数据的写入

  7. Struts2整合Spring方法及原理

    一.   Struts 2框架整合Spring步骤 1. 复制文件.复制struts2-spring-plugin-x-x-x.jar和spring.jar到WEB-INF/lib目录下.其中的x对应 ...

  8. Data Flow-File Read-详细过程

  9. 11 session 使用

    #session 使用app.secret_key = "dsada12212132dsad1232113"app.config['PERMANENT_SESSION_LIFETI ...

  10. Rikka with Mista 线段树求交点个数

    由于上下线段是不可能有交点的 可以先看左右线段树,按照y递增的顺序,对点进行排序. 升序构造,那么对于从某一点往下的射线,对于L,R进行区间覆盖,线段交点个数就是单点的被覆盖的次数. 降序构造,那么对 ...