• 初识mybatis_03 一级缓存和二级缓存


    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();
            }
            
        }
        
    }
    想的都是好
  • 相关阅读:
    UVaLive 7362 Farey (数学,欧拉函数)
    UVaLive 7361 Immortal Porpoises (矩阵快速幂)
    UVaLive 7359 Sum Kind Of Problem (数学,水题)
    CodeForces 706D Vasiliy's Multiset (字典树查询+贪心)
    负载均衡服务器
    集群-如何理解集群?
    架构规划
    领域模型
    状态图
    E-R图
  • 原文地址:https://www.cnblogs.com/freezone/p/5166716.html
Copyright © 2020-2023  润新知