redis(四)redis与Mybatis的无缝整合让MyBatis透明的管理缓存
redis的安装 http://liuyieyer.iteye.com/blog/2078093
redis的主从高可用 http://liuyieyer.iteye.com/blog/2078095
Mybatis 的使用不多说。
Mybatis为了方便我们扩展缓存定义了一个Cache接口,看看ehcache-mybatis的源码就明白了。我们要使用自己的cache同样的实现Cache接口即可。直接上代码
public class RedisCache implements Cache {
private static Log logger = LogFactory.getLog(RedisCache.class);
private Jedis redisClient = createClient();
/** The ReadWriteLock. */
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); private String id;
public RedisCache(final String id) {
if (id == null) {
throw new IllegalArgumentException("Cache instances require an ID");
}
logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>MybatisRedisCache:id=" + id);
this.id = id;
} @Override
public String getId() {
return this.id;
} @Override
public int getSize() {
return Integer.valueOf(redisClient.dbSize().toString());
} @Override
public void putObject(Object key, Object value) {
logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>putObject:" + key + "=" + value);
redisClient.set(SerializeUtil.serialize(key.toString()), SerializeUtil.serialize(value));
} @Override
public Object getObject(Object key) {
Object value = SerializeUtil.unserialize(redisClient.get(SerializeUtil.serialize(key.toString())));
logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>getObject:" + key + "=" + value);
return value;
} @Override
public Object removeObject(Object key) {
return redisClient.expire(SerializeUtil.serialize(key.toString()), 0);
} @Override
public void clear() {
redisClient.flushDB();
} @Override
public ReadWriteLock getReadWriteLock() {
return readWriteLock;
} protected static Jedis createClient() {
try {
JedisPool pool = new JedisPool(new JedisPoolConfig(), "172.60.0.172");
return pool.getResource();
} catch (Exception e) {
e.printStackTrace();
}
throw new RuntimeException("初始化连接池错误");
} } class SerializeUtil {
public static byte[] serialize(Object object) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
// 序列化
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
byte[] bytes = baos.toByteArray();
return bytes;
} catch (Exception e) {
e.printStackTrace();
}
return null;
} public static Object unserialize(byte[] bytes) {
if(bytes == null)return null;
ByteArrayInputStream bais = null;
try {
// 反序列化
bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
在看ehcache-mybatis的源码 它真正使用cache的方式是通过集成org.apache.ibatis.cache.decorators.LoggingCache 这个类实现的,照猫画虎,直接我们也继承
public class LoggingRedisCache extends LoggingCache { public LoggingRedisCache(String id) {
super(new RedisCache(id));
} }
在mapper.xml中添加如下cache标签
<!-- 启用缓存 -->
<cache type="cn.seafood.cache.LoggingRedisCache" />
在mybatis的核心文件中开启缓存
<settings>
<!-- 这个配置使全局的映射器启用或禁用缓存 -->
<setting name="cacheEnabled" value="true" />
<!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->
<setting name="multipleResultSetsEnabled" value="true"/>
<!-- 配置默认的执行器。SIMPLE 执行器没有什么特别之处。REUSE 执行器重用预处理语句。BATCH 执行器重用语句和批量更新 -->
<setting name="defaultExecutorType" value="REUSE" />
<!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 -->
<setting name="lazyLoadingEnabled" value="false" />
<setting name="aggressiveLazyLoading" value="true" />
<!-- <setting name="enhancementEnabled" value="true"/> -->
<!-- 设置超时时间,它决定驱动等待一个数据库响应的时间。 -->
<setting name="defaultStatementTimeout" value="25000" />
</settings>
<setting name="lazyLoadingEnabled" value="false" />
<setting name="aggressiveLazyLoading" value="true" />
注意着两个属性,需要把属性延迟加载和关联对象加载给关闭了,不然放进redis中的cglib代理对象,在对数据发生更改的时候,会出错。
redis(四)redis与Mybatis的无缝整合让MyBatis透明的管理缓存的更多相关文章
- redis(五)redis与Mybatis的无缝整合让MyBatis透明的管理缓存二
在上一篇文中的Cahe类存在各种问题如:一直使用同一个连接,每次都创建新的Cache,项目中老是爆出connection timeout 的异常,存储的key过长等等一系列的问题,解决问题最好的办法就 ...
- Mybatis与Spring整合,使用了maven管理项目,作为初学者觉得不错,转载下来
转载自:http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype ...
- ssm框架(Spring Springmvc Mybatis框架)整合及案例增删改查
三大框架介绍 ssm框架是由Spring springmvc和Mybatis共同组成的框架.Spring和Springmvc都是spring公司开发的,因此他们之间不需要整合.也可以说是无缝整合.my ...
- Spring+SpringMVC+MyBatis深入学习及搭建(九)——MyBatis和Spring整合
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6964162.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(八)--My ...
- spring + Mybatis + pageHelper + druid 整合源码分享
springMvc + spring + Mybatis + pageHelper + druid 整合 spring 和druid整合,spring 整合druid spring 和Mybatis ...
- Mybatis和Spring整合&逆向工程
Mybatis和Spring整合&逆向工程Mybatis和Spring整合mybatis整合Spring的思路目的就是将在SqlMapConfig.xml中的配置移植到Spring的appli ...
- Spring+SpringMVC+MyBatis+easyUI整合进阶篇(九)Linux下安装redis及redis的常用命令和操作
redis简介 Redis是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis与其他key-value缓存产品有以下三个特点: Redis支持数据的持久化,可以将内存 ...
- 03.redis与ssm整合(mybatis二级缓存)
SSM+redis整合 ssm框架之前已经搭建过了,这里不再做代码复制工作. 这里主要是利用redis去做mybatis的二级缓存,mybaits映射文件中所有的select都会刷新已有缓存,如果不存 ...
- springboot与redis的无缝整合(分布式)
参考博客 : https://blog.csdn.net/csao204282/article/details/54092997 1 首先得引入依赖 <dependency> <gr ...
随机推荐
- Linux 动态库剖析
进程与 API 动态链接的共享库是 GNU/Linux® 的一个重要方面.该种库允许可执行文件在运行时动态访问外部函数,从而(通过在需要时才会引入函数的方式)减少它们对内存的总体占用.本文研究了创建和 ...
- jQuery.fn.extend与jQuery.extend 的区别
1 jquery.extend 是jquery 静态的方法 实例 jQuery.extend({ liu: function(){ alert('liu'); } }) ...
- 【剑指offer】第一个仅仅出现一次的字符
转载请注明出处:http://blog.csdn.net/ns_code/article/details/27106997 题目描写叙述: 在一个字符串(1<=字符串长度<=10000,所 ...
- 语句(语句分类及if语句)
目前为止,之前学过的程序只能一句一句向下执行.C#:选择控制:if,else,switch,case循环控制:while,do,for,foreach跳转语句:break,continue异常处理:t ...
- 【监控】使用probe对tomcat服务进行监控
1.运行环境(博主本地) JDK:jdk1.6 Tomcat:tomcat7 OS:Windows10 2.下载 点击下载 3.安装运行 1.解压,将probe文件夹复制放进tomcat里面的weba ...
- 一个简单链表的C++实现(二)
/* LList.cpp * Author: Qiang Xiao * Time: 2015-07-12 */ #include<iostream> using namespace std ...
- WCF跟踪分析 使用(SvcTraceViewer)
1.首先在WCF服务端配置文件中配置两处,用于记录WCF调用记录! A:<system.serviceModel>目录下: <diagnostics> <mes ...
- System.Web.Security 在winform中是什么命名空间呢
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStorin ...
- Identity-修改Error错误提示为中文
第一步:重写IdentityErrorDescriber public class CustomIdentityErrorDescriber : IdentityErrorDescriber ...
- js 选择器
a>b 获取a下面的直接子元素