mybatis 整合redis作为二级缓存
核心关键在于定义一个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作为二级缓存的更多相关文章
- Mybatis整合Redis实现二级缓存
Mybatis集成ehcache . 为什么需要缓存 拉高程序的性能 . 什么样的数据需要缓存 很少被修改或根本不改的数据 业务场景比如:耗时较高的统计分析sql.电话账单查询sql等 . ehcac ...
- SSM+redis整合(mybatis整合redis做二级缓存)
SSM:是Spring+Struts+Mybatis ,另外还使用了PageHelper 前言: 这里主要是利用redis去做mybatis的二级缓存,mybaits映射文件中所有的select都会刷 ...
- mybatis 使用redis实现二级缓存(spring boot)
mybatis 自定义redis做二级缓存 前言 如果关注功能实现,可以直接看功能实现部分 何时使用二级缓存 一个宗旨---不常变的稳定而常用的 一级是默认开启的sqlsession级别的. 只在单表 ...
- SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置
2016年03月03日 10:37:47 标签: mysql / redis / mybatis / spring mvc / spring 33805 项目环境: 在SpringMVC + MyBa ...
- SpringMVC +Spring + MyBatis + Mysql + Redis(作为二级缓存) 配置
转载:http://blog.csdn.net/xiadi934/article/details/50786293 项目环境: 在SpringMVC +Spring + MyBatis + MySQL ...
- SpringBank 开发日志 Mybatis 使用redis 作为二级缓存时,无法通过cacheEnabled=false 将其关闭
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC ...
- mybatis结合redis实战二级缓存(六)
之前的文章中我们意见分析了一级缓存.二级缓存的相关源码和基本原理,今天我们来分享下了mybatis二级缓存和redis的结合,当然mybatis二级缓存也可以和ehcache.memcache.OSC ...
- mybatis结合redis实战二级缓存
之前的文章中我们意见分析了一级缓存.二级缓存的相关源码和基本原理,今天我们来分享下了mybatis二级缓存和redis的结合,当然mybatis二级缓存也可以和ehcache.memcache.OSC ...
- springboot+mybatis 用redis作二级缓存
1.加入相关依赖包: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...
随机推荐
- kendo grid 使用小结
需要注意的: 1. id,如果没有指定id则会导致create.update等操作无法正常使用. 头疼事项: 1. 服务端失败返回error数据.如果是编辑状态,还不能友好提示错误.当然可以使用大量代 ...
- 【数据库】sql2008卸载和默认实例的删除 标签: 数据库 2014-11-16 15:15 5878人阅读 评论(30)
在安装sql2008的时候,会碰到这一步,要求创建实例,可以选择默认实例和命名实例,如果是第一次安装的话,可以选择默认实例,但是如果是第二次甚至更多次安装的 话,很多时候会出现不能用默认实例,只能自己 ...
- Java练习 SDUT-2240_织女的红线
织女的红线 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 好久不见牛郎哥哥了,织女非常想他,但是她想考验一下牛郎在她不 ...
- 洛谷P2820 局域网
#include<bits/stdc++.h> using namespace std; ; ; int n,k,sum,tot; struct node{ int cnt,fa; }f[ ...
- Python 2.X 版本 600行入门基础
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- 解析P2P金融的业务安全
看了很多乙方同学们写的业务安全,总结下来,其出发点主要是在技术层面风险问题.另外捎带一些业务风险.今天我要谈的是甲方眼里的业务安全问题,甲方和乙方在业务安全的视野上会有一些区别和一些重合.在同一个问题 ...
- colab找不到模块 no name
https://www.jianshu.com/p/2cf00bb9db34?utm_source=oschina-app 穷学生学习神经网络一定体会过“等待”的痛苦... 循环一次epoch急死人, ...
- poj 3384 Feng Shui (Half Plane Intersection)
3384 -- Feng Shui 构造半平面交,然后求凸包上最远点对. 这题的题意是给出一个凸多边形区域,要求在其中放置两个半径为r的圆(不能超出凸多边形区域),要求求出两个圆心,使得多边形中没有被 ...
- Java反射机制(五):使用反射增强简单工厂设计模式
关于简单工厂设计模式的讲解,可参考博文<设计模式: 简单工厂模式>,此处不再介绍: 我们先观察之前介绍的关于简单工厂: public class OperateFactory { pub ...
- 2018-8-10-WPF-调试-获得追踪输出
title author date CreateTime categories WPF 调试 获得追踪输出 lindexi 2018-08-10 19:16:51 +0800 2018-05-16 1 ...