zoukankan      html  css  js  c++  java
  • 第六章.MyBatis缓存结构

    一级缓存

    测试案例:

    MyBatisTest.java

            //缓存
            @Test  
            public void testFindCustomerCache1() throws Exception{  
                  
                SqlSession sqlSession=dataConn.getSqlSession();    
                  
                //调用userMapper的方法  
                Customer customer1=sqlSession.selectOne("test.findCustomerById",1);
                System.out.println("用户姓名:"+customer1.getUsername());
                
                
                Customer customer2=sqlSession.selectOne("test.findCustomerById",1);
                System.out.println("用户姓名:"+customer2.getUsername());
                sqlSession.close();
            }
            

    测试结果:

    只查询了一次

    DEBUG [main] - ==>  Preparing: SELECT * FROM CUSTOMER WHERE cus_id=? 
    DEBUG [main] - ==> Parameters: 1(Integer)
    DEBUG [main] - <==      Total: 1
    用户姓名:Mr
    用户姓名:Mr
    DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@42d8062c]

    两次查询之间出现增删该查等情况时,即执行commit()方法。

    在UserMapper.xml最后面加上

       <update id="updateCustomerAcNo" parameterType="cn.com.mybatis.po.Customer" >
            UPDATE CUSTOMER SET acno = #{acno} WHERE cus_id=#{cus_id}
        </update>

    在MyBatisTest.java中测试

            @Test  
            public void testFindCustomerCache2() throws Exception{  
                  
                SqlSession sqlSession=dataConn.getSqlSession();    
                  
                //调用userMapper的方法  
                Customer customer1=sqlSession.selectOne("test.findCustomerById",1);
                System.out.println("用户姓名:"+customer1.getUsername()+"|"
                        +"卡号:"+customer1.getAcno());
                
                String AcNo = "6228289999999";
                customer1.setAcno(AcNo);
                System.out.println("修改用户卡号为:"+AcNo);
                sqlSession.update("test.updateCustomerAcNo",customer1);
                sqlSession.commit();
                
                Customer customer2=sqlSession.selectOne("test.findCustomerById",1);
                System.out.println("用户姓名:"+customer2.getUsername()+"|"
                        +"卡号:"+customer2.getAcno());
                
                sqlSession.close();
            }

    观察结果:

    com.mysql.jdbc.JDBC4Connection@42d8062c]
    DEBUG [main] - ==>  Preparing: SELECT * FROM CUSTOMER WHERE cus_id=? 
    DEBUG [main] - ==> Parameters: 1(Integer)
    DEBUG [main] - <==      Total: 1
    用户姓名:Mr|卡号:622848
    修改用户卡号为:6228289999999
    DEBUG [main] - ==>  Preparing: UPDATE CUSTOMER SET acno = ? WHERE cus_id=? 
    DEBUG [main] - ==> Parameters: 6228289999999(String), 1(Integer)
    DEBUG [main] - <==    Updates: 1
    DEBUG [main] - Committing JDBC Connection [com.mysql.jdbc.JDBC4Connection@42d8062c]
    DEBUG [main] - ==>  Preparing: SELECT * FROM CUSTOMER WHERE cus_id=? 
    DEBUG [main] - ==> Parameters: 1(Integer)
    DEBUG [main] - <==      Total: 1
    用户姓名:Mr|卡号:6228289999999
    DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@42d8062c]
    DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@42d8062c]
    DEBUG [main] - Returned connection 1121453612 to pool.

    二级缓存

    检查Customer.java文件

    属性以及是否实现序列化接口

    public class Customer implements Serializable{
        private int cus_id;
        private String username;
        private String acno;
        private String gender;
        private String phone;
        private List<Batch> batchList;
    ....
    }

    在MyBatisTest.java中测试

        @Test  
            public void testFindCustomerOnMapper1() throws Exception{  
                SqlSession sqlSession=dataConn.getSqlSession();    
                  
                //获取Mapper代理  
                CustomerMapper customerMapper1=sqlSession.getMapper(CustomerMapper.class);
                //执行Mapper代理对象的查询方法
                Customer customer1=customerMapper1.findCustomerById(1);
                System.out.println("用户姓名:"+customer1.getUsername()+"|"
                        +"卡号:"+customer1.getAcno());
        
                
                //获取Mapper代理  
                CustomerMapper customerMapper2=sqlSession.getMapper(CustomerMapper.class);
                //执行Mapper代理对象的查询方法
                Customer customer2=customerMapper2.findCustomerById(1);
                System.out.println("用户姓名:"+customer2.getUsername()+"|"
                        +"卡号:"+customer2.getAcno());
                
                sqlSession.close();
            }
            

    得到结果:

    DEBUG [main] - ==>  Preparing: SELECT * FROM CUSTOMER WHERE cus_id=? 
    DEBUG [main] - ==> Parameters: 1(Integer)
    DEBUG [main] - <==      Total: 1
    用户姓名:Mr|卡号:622828999999
    用户姓名:Mr|卡号:622828999999

    若二级缓存中两个查询之间多出了commit()方法的执行?

    MyBatisTest.java继续测试

            @Test  
            public void testFindCustomerOnMapper2() throws Exception{  
                SqlSession sqlSession=dataConn.getSqlSession();    
                  
                //获取Mapper代理  
                CustomerMapper customerMapper1=sqlSession.getMapper(CustomerMapper.class);
                //执行Mapper代理对象的查询方法
                Customer customer1=customerMapper1.findCustomerById(1);
                System.out.println("用户姓名:"+customer1.getUsername()+"|"
                        +"卡号:"+customer1.getAcno());
                
                //获取Mapper代理  
                CustomerMapper customerMapper2=sqlSession.getMapper(CustomerMapper.class);
                String AcNo = "6228286666666";
                customer1.setAcno(AcNo);
                //执行Mapper代理对象的修改方法
                customerMapper2.updateCustomerAcNo(customer1);
                System.out.println("修改用户姓名:"+customer1.getUsername()+"|"
                        +"的卡号为:"+customer1.getAcno());
                sqlSession.commit();
                
                //获取Mapper代理  
                CustomerMapper customerMapper3=sqlSession.getMapper(CustomerMapper.class);
                //执行Mapper代理对象的查询方法
                Customer customer3=customerMapper3.findCustomerById(1);
                System.out.println("用户姓名:"+customer3.getUsername()+"|"
                        +"卡号:"+customer3.getAcno());
                
                sqlSession.close();
            }

    测试结果:

    DEBUG [main] - ==>  Preparing: SELECT * FROM CUSTOMER WHERE cus_id=? 
    DEBUG [main] - ==> Parameters: 1(Integer)
    DEBUG [main] - <==      Total: 1
    用户姓名:Mr|卡号:622828999999
    DEBUG [main] - ==>  Preparing: UPDATE CUSTOMER SET acno = ? WHERE cus_id=? 
    DEBUG [main] - ==> Parameters: 6228286666666(String), 1(Integer)
    DEBUG [main] - <==    Updates: 1
    修改用户姓名:Mr|的卡号为:6228286666666
    DEBUG [main] - Committing JDBC Connection [com.mysql.jdbc.JDBC4Connection@13c27452]
    DEBUG [main] - ==>  Preparing: SELECT * FROM CUSTOMER WHERE cus_id=? 
    DEBUG [main] - ==> Parameters: 1(Integer)
    DEBUG [main] - <==      Total: 1
    用户姓名:Mr|卡号:6228286666666
    DEBUG [main] - Resetting autocommit to true on JDBC Connection 
  • 相关阅读:
    【装机知识】内存条知识总结
    【装机知识】主板知识整理
    【装机知识】CPU知识整理
    SHELL 学历笔记
    tmux 会话恢复(no sessions)
    数据库客户端神器(mycli/pgcli/iredis)
    golang编写二叉树
    编译安装带lua 的 vim 编辑器
    linux 下vim 开发环境配置(通用所有编程语言)
    mac 下安装mysql
  • 原文地址:https://www.cnblogs.com/Mrchengs/p/9811002.html
Copyright © 2011-2022 走看看