package com.sunmap.dao; import static org.junit.Assert.*; import java.io.IOException; import java.io.Reader; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import com.sunmap.model.Article; import com.sunmap.model.Person; public class TestIPersonOperation_2 { @Test public void test() { // fail("Not yet implemented"); try { Reader reader = Resources.getResourceAsReader("config/Configuration.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sqlSessionFactory.openSession(); SqlSession session2 = sqlSessionFactory.openSession(); String select = "com.sunmap.dao.IPersonOperation.selectPersonByID"; String insert = "com.sunmap.dao.IPersonOperation.addPerson"; /** * 测试mybatis一级缓存 * */ // Person p = session.selectOne(select, 19); /**如果session提交就说明对数据库做过增删改等操作,为了防止一级缓存读取到脏数据,所以一级缓存会清空*/ // session.commit(); // Person p2 = session.selectOne(select, 19); // Person p3 = session2.selectOne(select, 19); // System.out.println(p == p2); // System.out.println(p == p3); //一旦母库进行增删改,session一级缓存就会自动clean // Person p4 = new Person(21,"qwsdfds", 21); // session.update(insert, p4); // session.commit(); // Person p5 = session.selectOne(select, 19); // Person p6 = session.selectOne(select, 19); // // System.out.println(p == p5); // System.out.println(p5 == p6); /** * 测试mybatis二级缓存 * * 二级缓存不建议使用,因为多表查询时会造成脏数据 * */ Person p = session.selectOne(select, 22); /**二级缓存是从cache中取得,只有提交才会进入到cache*/ session.commit(); Person p2 = session2.selectOne(select, 22); System.out.println(p == p2); } catch (IOException e) { e.printStackTrace(); } } }