核心关键在于定义一个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. JavaScript--轮播图_带计时器

    轮播图效果: 实现的功能: 1.鼠标移入,左右按钮显示 2.右下叫小圆点鼠标移入,进入下一张图 3.左右按钮点击,右下小圆点页跟随变更 4.自动开启计时器,鼠标移入右下叫小圆点区,计时器停止,鼠标移出 ...

  2. poj 2229 【完全背包dp】【递推dp】

    poj 2229 Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 21281   Accepted: 828 ...

  3. poj1637&&hdu1956 混合欧拉回图判断

    欧拉路:经过所有路有且仅有1次,可以路过所有的点. 无向图:  图连通,所有点都是偶数度,或者只有两个点是奇数度.当所有点是偶数度时欧拉路起点可以是任意点:当有两个奇数度点时起点必须是奇数度点. 有向 ...

  4. python 异常处理技巧

  5. EL表达式简单总结

    EL表达式 ## EL表达式的取值范围 JSP的四个作用域: pagecontext(生命周期用户离开或者跳转页面,作用域范围这个页面) request(生命周期用户离开页面,作用于这个页面) ses ...

  6. Kubernetes1.3新特性:新的资源回收控制器

    (一)  核心概念 在kubernetes1.3中新增了一个资源回收控制器GarbaseCollector,用这个控制器来替代kubernetes1.3中的资源回收控制器GC. 如下为kubernet ...

  7. 12-2 js基础

    一 数据类型 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  8. MySQL_连表查询

    连表查询 连表查询通常分为内连接和外连接.内连接就是使用INNER JOIN进行连表查询:而外连接又分为三种连接方式,分别是左连接(LEFT JOIN).右连接(RIGHT JOIN).全连接(FUL ...

  9. Vue点击事件失效

    在做项目时给button添加click事件,发现没反应,但另外写了一个button添加同样的事件,却能触发.原因是使用了better-scroll,默认它会阻止touch事件.所以在配置中需要加上cl ...

  10. Laravel实现定时任务的示例代码

    https://mp.weixin.qq.com/s/VUEqjwcHRb0ovhP0wup36A 最近在玩Laravel实现定时任务,这个是示例代码,可以参照这个实例.有需要的可以看看 定时任务是后 ...