博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第六章.MyBatis缓存结构
阅读量:6342 次
发布时间:2019-06-22

本文共 6217 字,大约阅读时间需要 20 分钟。

一级缓存

测试案例:

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用户姓名:MrDEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@42d8062c]

 

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

 

在UserMapper.xml最后面加上

UPDATE CUSTOMER SET acno = #{acno} WHERE cus_id=#{cus_id}

在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修改用户卡号为:6228289999999DEBUG [main] - ==>  Preparing: UPDATE CUSTOMER SET acno = ? WHERE cus_id=? DEBUG [main] - ==> Parameters: 6228289999999(String), 1(Integer)DEBUG [main] - <==    Updates: 1DEBUG [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|卡号:6228289999999DEBUG [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
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|卡号:622828999999DEBUG [main] - ==>  Preparing: UPDATE CUSTOMER SET acno = ? WHERE cus_id=? DEBUG [main] - ==> Parameters: 6228286666666(String), 1(Integer)DEBUG [main] - <==    Updates: 1修改用户姓名:Mr|的卡号为:6228286666666DEBUG [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|卡号:6228286666666DEBUG [main] - Resetting autocommit to true on JDBC Connection

 

转载于:https://www.cnblogs.com/Mrchengs/p/9811002.html

你可能感兴趣的文章
面试必问之JVM原理
查看>>
mysql主主同步+Keepalived
查看>>
研究音频编解码要看什么书
查看>>
tomcat远程调试配置
查看>>
QuartZ Cron表达式
查看>>
性能测试工具VTune的功能和用法介绍
查看>>
音频视频组件Audio DJ Studio for .NET更新至v10.0.0.0丨附下载
查看>>
RMAN Complete Recovery
查看>>
[ CodeForces 1064 B ] Equations of Mathematical Magic
查看>>
NYOJ-15:括号匹配(二)
查看>>
首次记录在案的
查看>>
C#进阶系列——WebApi 跨域问题解决方案:CORS
查看>>
错误:“产品订单的调度参数没有被定义”
查看>>
机器视觉在带钢针孔检测中的应用
查看>>
ASP.NET WEB API 调试
查看>>
使用wget命令进行整站下载
查看>>
解读volatile
查看>>
zookeeper安装部署
查看>>
centos6——初始化脚本
查看>>
linux I/O优化 磁盘读写参数设置
查看>>