package com.cy.test;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.cy.pj.goods.dao.GoodsDao;
import com.cy.pj.goods.pojo.Goods;
public class MyTestCache01 extends TestBaseWithJava{
/**
* 测试一级缓存,过程断点跟踪一定要明白
*/
@Test
//对于同一session来说,开二级缓存
public void testFirstLevelCache() {
//1.获取SqlSession对象
SqlSession session=sqlSessionFactory.openSession();
//2.执行删除业务
GoodsDao dao=session.getMapper(GoodsDao.class);
List<Goods> list=dao.findPageObjects(0, 3);//第一次去数据库取数据
//3.提交事务
session.commit();
list=dao.findPageObjects(0,3);//第二次走一级cache,从中取数据
System.out.println("list.size="+list.size());
for(Goods g:list) {
System.out.println(g);
}
// //3.提交事务
// session.commit();//这里边要是开二级缓存的话session.commit()应放在22-25之间因为不提交或者关闭数据部会进入二级缓存中
//4.释放资源
session.close();
}
@Test
//对于同一session来说,不开二级缓存
public void testFirstLevelCache1() {
/**--对于一级缓存来说,session.commit();或者session.close();
*--一级缓存都会失效因为其作用范围仅仅在一次会话中,注意session.commit()和session.close();的位置--*/
//1.获取SqlSession对象
SqlSession session=sqlSessionFactory.openSession();
//2.执行删除业务
GoodsDao dao=session.getMapper(GoodsDao.class);
List<Goods> list=dao.findPageObjects(0, 3);//第一次去数据库取数据
list=dao.findPageObjects(0,3);//第二次走一级cache,从中取数据
System.out.println("list.size="+list.size());
for(Goods g:list) {
System.out.println(g);
}
// //3.提交事务
session.commit();
/**这里边要是开二级缓存的话session.commit()
*应放在22-25之间因为不提交或者关闭数据部会进入二级缓存中*/
//4.释放资源
session.close();
}
@Test
//对于不同session来说
public void testSecondLevelCache() {
//1.获取SqlSession对象
SqlSession session1=sqlSessionFactory.openSession();
SqlSession session2=sqlSessionFactory.openSession();
//2.执行删除业务
GoodsDao dao1=session1.getMapper(GoodsDao.class);
GoodsDao dao2=session2.getMapper(GoodsDao.class);
List<Goods> list1=dao1.findPageObjects(0, 3);
session1.commit();//一级缓存失效
List<Goods> list2=dao2.findPageObjects(0, 3);//第二次走cache
//3.提交事务
session2.commit();//先从二级拿拿不到再从一级拿,还是拿不到就从数据库中查询
//4.释放资源
session1.close();
session2.close();
}
}
断点跟踪可以清晰的显示出cache缓存的运行顺序
配置文件
<?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">
<mapper namespace="com.cy.pj.goods.dao.GoodsDao">
<cache eviction="fifo" readOnly="true" > </cache>
<!-- readonly是设置是否要存储序列化的对象,因为cache也是内存所以也可以存非序列化的对象-->
<!--eviction可设置算法选项为FIFO和LRU-->
<!--flushInterval=""设置刷新时间-->
<!--size=""设置可以存的引用(对象)的数量-->
<!-- 基于多个id进行删除操作 方法1 -->
<!--
<delete id="deleteObjects">
delete from tb_goods
where id in
<foreach collection="array"
open="("
close=")"
item="id"
separator=",">
#{id}
</foreach>
</delete>
-->
<!-- 基于多个id进行删除操作:方法2 -->
<delete id="deleteObjects">
delete from tb_goods
<where>
<foreach collection="array"
item="id">
or id=#{id}
</foreach>
</where>
</delete>
<select id="findPageObjects"
resultType="com.cy.pj.goods.pojo.Goods">
select id,name,remark,createdTime
from tb_goods
limit #{startIndex},#{pageSize}
</select>
<select id="selectById"
resultType="com.cy.pj.goods.pojo.Goods">
select id,name,remark,createdTime
from tb_goods
where id=#{id}
</select>
<delete id="deleteObject">
delete from tb_goods
where id=#{id}
</delete>
<!-- 映射文件中每个元素都会封装一个MappedStatement对象 -->
<insert id="insertObject"
parameterType="com.cy.pj.goods.pojo.Goods">
insert into tb_goods
(id,name,remark,createdTime)
values
(#{id},#{name},#{remark},now())
</insert>
<!-- 映射文件中每个元素都会封装一个MappedStatement对象 -->
<update id="updateObject">
update tb_goods
set name=#{name},
remark=#{remark}
where id=#{id}
</update>
</mapper>
转载:https://blog.csdn.net/qq_43584965/article/details/101716953
查看评论