第一步:自己实现缓存类,实现Cache接口,重写方法,导入jedis.jar
JedisCache类
public class JedisCache implements Cache {
/**
* 序列化和反序列化的操作类
* @author Administrator
*
*/
static class SeqUtils{
/**
* 反序列化为对象
* @param bt
* @return
* @throws ClassNotFoundException
* @throws IOException
*/
public static Object deSer(byte[] bt) throws ClassNotFoundException, IOException{
ByteArrayInputStream bais=new ByteArrayInputStream(bt);
ObjectInputStream ois=new ObjectInputStream(bais);
return ois.readObject();
}
/**
* 对象序列化成字节数组
* @param obj
* @return
* @throws IOException
*/
public static byte[] ser(Object obj) throws IOException{
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ObjectOutputStream obos=new ObjectOutputStream(baos);
obos.writeObject(obj);
return baos.toByteArray();
}
}
static JedisPool jp=null;
private String id;
/**
* 当查询时 自动实例化JedisCache
* id 其实mapper查询的id
* @param id
*/
public JedisCache(String id) {
if(jp==null){
try {
GenericObjectPoolConfig gopc=new GenericObjectPoolConfig();
gopc.setMaxTotal(100);
gopc.setMaxIdle(10);
jp=new JedisPool(gopc, "localhost");
jp.getResource();
} catch (Exception e) {
//e.printStackTrace();
jp=null;
}
}
this.id=id;
}
/**
* 清空所有缓存
*/
public void clear() {
Jedis jedis=jp.getResource();
jedis.flushAll();
jp.returnResourceObject(jedis);
}
public String getId() {
// TODO Auto-generated method stub
return id;
}
/**
* mybatis自动调用该方法 判断返回值是否为null
* 如果为空 自动查询数据库
* 不为空 直接使用 返回的对象
* key 才是 传入当前查询的oid 主键
*/
public Object getObject(Object key) {
if(jp==null){
return null;
}
Jedis jedis=jp.getResource();
try {
byte[] bt=jedis.get(SeqUtils.ser(key));
if(bt!=null){
Object obj=SeqUtils.deSer(bt);
//将redis对象 回收到连接池
jp.returnResourceObject(jedis);
return obj;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* newio non-blocking io
*/
public ReadWriteLock getReadWriteLock() {
return new ReentrantReadWriteLock();
}
/**
* 用于读取 redis中缓存了多少元素
*/
public int getSize() {
Jedis jedis=jp.getResource();
Set<String> alllElemebts=jedis.keys("*");
return alllElemebts.size();
}
/**
* 第一次查询数据库后 mybatis会自动调用 该方法将数据写入缓存
*/
public void putObject(Object key, Object value) {
if(jp==null){
return;
}
Jedis jedis=jp.getResource();
try {
jedis.set(SeqUtils.ser(key),SeqUtils.ser(value));
jedis.save();
jp.returnResourceObject(jedis);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 调用了mybatis flush方法自动清空缓存
* 自动调用removeObject 需要从redis中删除该元素
*/
public Object removeObject(Object key) {
Jedis jedis=jp.getResource();
try {
jedis.del(SeqUtils.ser(key));
jp.returnResourceObject(jedis);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
第二步:接口映射文件中加入<cache type="自定义的缓存类"/>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
接口映射关注点
namespace必须和接口的类全路径一致
-->
<mapper namespace="cn.et.mybatis.lesson05.EmpMapper">
<!-- 表示使用二级缓存 -->
<cache type="cn.et.mybatis.lesson05.JedisCache"></cache>
<select id="selectByWhere" resultType="cn.et.mybatis.lesson04.genernator.entity.Emp">
select * from emp where empNo=#{0}
</select>
</mapper>