mybatis学习之查询缓存
来源:互联网 发布:ssh传输文件命令 mac 编辑:程序博客网 时间:2024/05/22 07:06
转自 http://blog.csdn.net/fjnmbb12/article/details/73692364
什么是查询缓存?
mybatis提供查询缓存,用于减轻数据压力,提高数据库性能
mybatis提供一级缓存和二级缓存
一级缓存是SQLSession的缓存
在操作数据库时需要构造SQLSession对象,在对象中有一个数据结构(HashMap)用于存储缓存数据
不同的SQLSession之间的缓存数据区域(HashMap)是互相不影响的
二级缓存是mapper级别的缓存
多个SqlSession,操作同一个mapper的SQL语句,多个SqlSession可以共用二级缓存,二级缓存是跨SQLSession的
为什么要用缓存?
一级缓存
1、工作原理
2、一级缓存测试
- //一级缓存测试
- @Test
- public void testCache1() throws Exception{
- //创建SqlSession
- SqlSession sqlSession = sqlSessionFactory.openSession();
- UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
- //第一次发起请求,查询ID为29的用户
- User user1 = userMapper.findUserById(29);
- System.out.println(user1);
- //如果sqlSession执行commit操作,是否会清空一级缓存
- //更新user1的信息,清空缓存
- user1.setUsername("一级缓存已更新");
- userMapper.updateUser(user1);
- //执行commit才会清空缓存
- sqlSession.commit();
- //第二次发起请求,查询ID为29的用户
- User user2 = userMapper.findUserById(29);
- System.out.println(user2);
- sqlSession.close();
- }
- DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
- DEBUG [main] - Class not found: org.jboss.vfs.VFS
- DEBUG [main] - JBoss 6 VFS API is not available in this environment.
- DEBUG [main] - Class not found: org.jboss.vfs.VirtualFile
- DEBUG [main] - VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
- DEBUG [main] - Using VFS adapter org.apache.ibatis.io.DefaultVFS
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
- DEBUG [main] - Reader entry: Items.class
- DEBUG [main] - Reader entry: Order.class
- DEBUG [main] - Reader entry: OrderCustom.class
- DEBUG [main] - Reader entry: OrderDetail.class
- DEBUG [main] - Reader entry: User.class
- DEBUG [main] - Reader entry: UserCustom.class
- DEBUG [main] - Reader entry: UserQueryVo.class
- DEBUG [main] - Listing file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Items.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Items.class
- DEBUG [main] - Reader entry: ���� 4 W
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Order.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Order.class
- DEBUG [main] - Reader entry: ���� 4 f
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderCustom.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderCustom.class
- DEBUG [main] - Reader entry: ���� 4 "
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderDetail.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderDetail.class
- DEBUG [main] - Reader entry: ���� 4 J
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/User.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/User.class
- DEBUG [main] - Reader entry: ���� 4 [
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserCustom.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserCustom.class
- DEBUG [main] - Reader entry: ���� 4
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserQueryVo.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserQueryVo.class
- DEBUG [main] - Reader entry: ���� 4 %
- DEBUG [main] - Checking to see if class pojo.Items matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.Order matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.OrderCustom matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.OrderDetail matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.User matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.UserCustom matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.UserQueryVo matches criteria [is assignable to Object]
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
- DEBUG [main] - Reader entry: OrderMapperCustom.class
- DEBUG [main] - Reader entry: OrderMapperCustom.xml
- DEBUG [main] - Reader entry: UserMapper.class
- DEBUG [main] - Reader entry: UserMapper.xml
- DEBUG [main] - Listing file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.class
- DEBUG [main] - Reader entry: ���� 4
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.xml
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.xml
- DEBUG [main] - Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.class
- DEBUG [main] - Reader entry: ���� 4 findUserByIdResultMap (I)Lpojo/User;
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.xml
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.xml
- DEBUG [main] - Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
- DEBUG [main] - Checking to see if class mapper.OrderMapperCustom matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class mapper.UserMapper matches criteria [is assignable to Object]
- DEBUG [main] - Opening JDBC Connection
- DEBUG [main] - Created connection 439904756.
- DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1a3869f4]
- DEBUG [main] - ==> Preparing: SELECT * FROM USER WHERE id = ?
- DEBUG [main] - ==> Parameters: 29(Integer)
- DEBUG [main] - <== Total: 1
- User{id=29, username='测试测试1', sex='1', birthday=Mon Jun 19 00:00:00 CST 2017, address='湖南益阳', orderList=null}
- DEBUG [main] - ==> Preparing: UPDATE user SET username = ? , birthday = ? , sex = ? , address = ? where id = ?
- DEBUG [main] - ==> Parameters: 一级缓存已更新(String), 2017-06-19 00:00:00.0(Timestamp), 1(String), 湖南益阳(String), 29(Integer)
- DEBUG [main] - <== Updates: 1
- DEBUG [main] - Committing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1a3869f4]
- DEBUG [main] - ==> Preparing: SELECT * FROM USER WHERE id = ?
- DEBUG [main] - ==> Parameters: 29(Integer)
- DEBUG [main] - <== Total: 1
- User{id=29, username='一级缓存已更新', sex='1', birthday=Mon Jun 19 00:00:00 CST 2017, address='湖南益阳', orderList=null}
- DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1a3869f4]
- DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1a3869f4]
- DEBUG [main] - Returned connection 439904756 to pool.
- Process finished with exit code 0
- //一级缓存测试
- @Test
- public void testCache1() throws Exception{
- //创建SqlSession
- SqlSession sqlSession = sqlSessionFactory.openSession();
- UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
- //第一次发起请求,查询ID为29的用户
- User user1 = userMapper.findUserById(29);
- System.out.println(user1);
- //第二次发起请求,查询ID为29的用户
- User user2 = userMapper.findUserById(29);
- System.out.println(user2);
- sqlSession.close();
- }
- DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
- DEBUG [main] - Class not found: org.jboss.vfs.VFS
- DEBUG [main] - JBoss 6 VFS API is not available in this environment.
- DEBUG [main] - Class not found: org.jboss.vfs.VirtualFile
- DEBUG [main] - VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
- DEBUG [main] - Using VFS adapter org.apache.ibatis.io.DefaultVFS
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
- DEBUG [main] - Reader entry: Items.class
- DEBUG [main] - Reader entry: Order.class
- DEBUG [main] - Reader entry: OrderCustom.class
- DEBUG [main] - Reader entry: OrderDetail.class
- DEBUG [main] - Reader entry: User.class
- DEBUG [main] - Reader entry: UserCustom.class
- DEBUG [main] - Reader entry: UserQueryVo.class
- DEBUG [main] - Listing file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Items.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Items.class
- DEBUG [main] - Reader entry: ���� 4 W
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Order.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Order.class
- DEBUG [main] - Reader entry: ���� 4 f
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderCustom.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderCustom.class
- DEBUG [main] - Reader entry: ���� 4 "
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderDetail.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderDetail.class
- DEBUG [main] - Reader entry: ���� 4 J
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/User.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/User.class
- DEBUG [main] - Reader entry: ���� 4 [
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserCustom.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserCustom.class
- DEBUG [main] - Reader entry: ���� 4
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserQueryVo.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserQueryVo.class
- DEBUG [main] - Reader entry: ���� 4 %
- DEBUG [main] - Checking to see if class pojo.Items matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.Order matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.OrderCustom matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.OrderDetail matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.User matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.UserCustom matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.UserQueryVo matches criteria [is assignable to Object]
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
- DEBUG [main] - Reader entry: OrderMapperCustom.class
- DEBUG [main] - Reader entry: OrderMapperCustom.xml
- DEBUG [main] - Reader entry: UserMapper.class
- DEBUG [main] - Reader entry: UserMapper.xml
- DEBUG [main] - Listing file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.class
- DEBUG [main] - Reader entry: ���� 4
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.xml
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.xml
- DEBUG [main] - Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.class
- DEBUG [main] - Reader entry: ���� 4 findUserByIdResultMap (I)Lpojo/User;
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.xml
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.xml
- DEBUG [main] - Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
- DEBUG [main] - Checking to see if class mapper.OrderMapperCustom matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class mapper.UserMapper matches criteria [is assignable to Object]
- DEBUG [main] - Opening JDBC Connection
- DEBUG [main] - Created connection 439904756.
- DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1a3869f4]
- DEBUG [main] - ==> Preparing: SELECT * FROM USER WHERE id = ?
- DEBUG [main] - ==> Parameters: 29(Integer)
- DEBUG [main] - <== Total: 1
- User{id=29, username='一级缓存已更新', sex='1', birthday=Mon Jun 19 00:00:00 CST 2017, address='湖南益阳', orderList=null}
- User{id=29, username='一级缓存已更新', sex='1', birthday=Mon Jun 19 00:00:00 CST 2017, address='湖南益阳', orderList=null}
- DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1a3869f4]
- DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1a3869f4]
- DEBUG [main] - Returned connection 439904756 to pool.
- Process finished with exit code 0
3、一级缓存的应用
二级缓存
1、原理
UserMapper有一个二级缓存区域(按照namespace分),其他的Mapper也有自己的二级缓存区域(按照namespace分)。
每一个namespace的mapper,都有一个二级缓存区域。两个mapper的namespace如果相同,那么这两个mapper执行sql查询到的数据会存储在相同的二级缓存区域中。
2、开启二级缓存
在核心配置文件SqlMapConfig.xml中加入
<setting name="cacheEnabled" value="true"/>
描述
允许值
默认值
cacheEnabled
对在此配置文件下的所有cache 进行全局性开/关设置。
true false
true
- <!--开启本mapper的namespace下的二级缓存 -->
- <cache />
3、调用pojo类实现序列化接口
- public class User implements Serializable{
- //属性名和数据库表的字段一一对应
- private Integer id;
- private String username;// 用户姓名
- private String sex;// 性别
- private Date birthday;// 生日
- private String address;// 地址
4、测试方法
未执行提交操作,不同SQLSession查询同一个mapper的sql,二级缓存测试:- //二级缓存测试
- @Test
- public void testCache2() throws Exception{
- //创建SqlSession1与SqlSession2
- SqlSession sqlSession1 = sqlSessionFactory.openSession();
- SqlSession sqlSession2 = sqlSessionFactory.openSession();
- SqlSession sqlSession3 = sqlSessionFactory.openSession();
- UserMapper userMapper1 = sqlSession1.getMapper(UserMapper.class);
- UserMapper userMapper2 = sqlSession2.getMapper(UserMapper.class);
- UserMapper userMapper3 = sqlSession3.getMapper(UserMapper.class);
- //第一次发起请求,查询ID为29的用户
- User user1 = userMapper1.findUserById(29);
- System.out.println(user1);
- //执行关闭操作,将sqlSession的数据写到二级缓存区域
- sqlSession1.close();
- //第二次发起请求,查询ID为29的用户
- User user2 = userMapper2.findUserById(29);
- System.out.println(user2);
- //执行关闭操作,将sqlSession的数据写到二级缓存区域
- sqlSession2.close();
- //第三次发起请求,执行commit操作
- // User user3 = userMapper3.findUserById(29);
- // System.out.println(user3);
- // //执行关闭操作,将sqlSession的数据写到二级缓存区域
- // sqlSession3.close();
- }
- DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
- DEBUG [main] - Class not found: org.jboss.vfs.VFS
- DEBUG [main] - JBoss 6 VFS API is not available in this environment.
- DEBUG [main] - Class not found: org.jboss.vfs.VirtualFile
- DEBUG [main] - VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
- DEBUG [main] - Using VFS adapter org.apache.ibatis.io.DefaultVFS
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
- DEBUG [main] - Reader entry: Items.class
- DEBUG [main] - Reader entry: Order.class
- DEBUG [main] - Reader entry: OrderCustom.class
- DEBUG [main] - Reader entry: OrderDetail.class
- DEBUG [main] - Reader entry: User.class
- DEBUG [main] - Reader entry: UserCustom.class
- DEBUG [main] - Reader entry: UserQueryVo.class
- DEBUG [main] - Listing file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Items.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Items.class
- DEBUG [main] - Reader entry: ���� 4 W
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Order.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Order.class
- DEBUG [main] - Reader entry: ���� 4 f
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderCustom.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderCustom.class
- DEBUG [main] - Reader entry: ���� 4 "
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderDetail.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderDetail.class
- DEBUG [main] - Reader entry: ���� 4 J
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/User.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/User.class
- DEBUG [main] - Reader entry: ���� 4 ]
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserCustom.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserCustom.class
- DEBUG [main] - Reader entry: ���� 4
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserQueryVo.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserQueryVo.class
- DEBUG [main] - Reader entry: ���� 4 %
- DEBUG [main] - Checking to see if class pojo.Items matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.Order matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.OrderCustom matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.OrderDetail matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.User matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.UserCustom matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.UserQueryVo matches criteria [is assignable to Object]
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
- DEBUG [main] - Reader entry: OrderMapperCustom.class
- DEBUG [main] - Reader entry: OrderMapperCustom.xml
- DEBUG [main] - Reader entry: UserMapper.class
- DEBUG [main] - Reader entry: UserMapper.xml
- DEBUG [main] - Listing file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.class
- DEBUG [main] - Reader entry: ���� 4
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.xml
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.xml
- DEBUG [main] - Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.class
- DEBUG [main] - Reader entry: ���� 4 findUserByIdResultMap (I)Lpojo/User;
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.xml
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.xml
- DEBUG [main] - Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
- DEBUG [main] - Checking to see if class mapper.OrderMapperCustom matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class mapper.UserMapper matches criteria [is assignable to Object]
- DEBUG [main] - Cache Hit Ratio [mapper.UserMapper]: 0.0
- DEBUG [main] - Opening JDBC Connection
- DEBUG [main] - Created connection 1780132728.
- DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6a1aab78]
- DEBUG [main] - ==> Preparing: SELECT * FROM USER WHERE id = ?
- DEBUG [main] - ==> Parameters: 29(Integer)
- DEBUG [main] - <== Total: 1
- User{id=29, username='一级缓存已更新', sex='1', birthday=Mon Jun 19 00:00:00 CST 2017, address='湖南益阳', orderList=null}
- DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6a1aab78]
- DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@6a1aab78]
- DEBUG [main] - Returned connection 1780132728 to pool.
- DEBUG [main] - Cache Hit Ratio [mapper.UserMapper]: 0.5
- User{id=29, username='一级缓存已更新', sex='1', birthday=Mon Jun 19 00:00:00 CST 2017, address='湖南益阳', orderList=null}
- Process finished with exit code 0
可以看到这里第一次执行查询了数据库,第二次执行时,没有查询,Cache Hit Ratio 命中率由0 变为了0.5,含义为,查询了两次,命中一次,为百分之50
- //二级缓存测试
- @Test
- public void testCache2() throws Exception{
- //创建SqlSession1与SqlSession2
- SqlSession sqlSession1 = sqlSessionFactory.openSession();
- SqlSession sqlSession2 = sqlSessionFactory.openSession();
- SqlSession sqlSession3 = sqlSessionFactory.openSession();
- UserMapper userMapper1 = sqlSession1.getMapper(UserMapper.class);
- UserMapper userMapper2 = sqlSession2.getMapper(UserMapper.class);
- UserMapper userMapper3 = sqlSession3.getMapper(UserMapper.class);
- //第一次发起请求,查询ID为29的用户
- User user1 = userMapper1.findUserById(29);
- System.out.println(user1);
- //执行关闭操作,将sqlSession的数据写到二级缓存区域
- sqlSession1.close();
- //第三次发起请求,执行commit操作
- User user3 = userMapper3.findUserById(29);
- user3.setUsername("二级缓存已更新");
- userMapper3.updateUser(user3);
- //执行提交操作,清空UserMapper下的二级缓存
- sqlSession3.commit();
- //执行关闭操作,将sqlSession的数据写到二级缓存区域
- sqlSession3.close();
- //第二次发起请求,查询ID为29的用户
- User user2 = userMapper2.findUserById(29);
- System.out.println(user2);
- //执行关闭操作,将sqlSession的数据写到二级缓存区域
- sqlSession2.close();
- }
结果:
- DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
- DEBUG [main] - Class not found: org.jboss.vfs.VFS
- DEBUG [main] - JBoss 6 VFS API is not available in this environment.
- DEBUG [main] - Class not found: org.jboss.vfs.VirtualFile
- DEBUG [main] - VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
- DEBUG [main] - Using VFS adapter org.apache.ibatis.io.DefaultVFS
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
- DEBUG [main] - Reader entry: Items.class
- DEBUG [main] - Reader entry: Order.class
- DEBUG [main] - Reader entry: OrderCustom.class
- DEBUG [main] - Reader entry: OrderDetail.class
- DEBUG [main] - Reader entry: User.class
- DEBUG [main] - Reader entry: UserCustom.class
- DEBUG [main] - Reader entry: UserQueryVo.class
- DEBUG [main] - Listing file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Items.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Items.class
- DEBUG [main] - Reader entry: ���� 4 W
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Order.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Order.class
- DEBUG [main] - Reader entry: ���� 4 f
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderCustom.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderCustom.class
- DEBUG [main] - Reader entry: ���� 4 "
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderDetail.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderDetail.class
- DEBUG [main] - Reader entry: ���� 4 J
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/User.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/User.class
- DEBUG [main] - Reader entry: ���� 4 ]
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserCustom.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserCustom.class
- DEBUG [main] - Reader entry: ���� 4
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserQueryVo.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserQueryVo.class
- DEBUG [main] - Reader entry: ���� 4 %
- DEBUG [main] - Checking to see if class pojo.Items matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.Order matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.OrderCustom matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.OrderDetail matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.User matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.UserCustom matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.UserQueryVo matches criteria [is assignable to Object]
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
- DEBUG [main] - Reader entry: OrderMapperCustom.class
- DEBUG [main] - Reader entry: OrderMapperCustom.xml
- DEBUG [main] - Reader entry: UserMapper.class
- DEBUG [main] - Reader entry: UserMapper.xml
- DEBUG [main] - Listing file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.class
- DEBUG [main] - Reader entry: ���� 4
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.xml
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.xml
- DEBUG [main] - Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.class
- DEBUG [main] - Reader entry: ���� 4 findUserByIdResultMap (I)Lpojo/User;
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.xml
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.xml
- DEBUG [main] - Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
- DEBUG [main] - Checking to see if class mapper.OrderMapperCustom matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class mapper.UserMapper matches criteria [is assignable to Object]
- DEBUG [main] - Cache Hit Ratio [mapper.UserMapper]: 0.0
- DEBUG [main] - Opening JDBC Connection
- DEBUG [main] - Created connection 1780132728.
- DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6a1aab78]
- DEBUG [main] - ==> Preparing: SELECT * FROM USER WHERE id = ?
- DEBUG [main] - ==> Parameters: 29(Integer)
- DEBUG [main] - <== Total: 1
- User{id=29, username='一级缓存已更新', sex='1', birthday=Mon Jun 19 00:00:00 CST 2017, address='湖南益阳', orderList=null}
- DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6a1aab78]
- DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@6a1aab78]
- DEBUG [main] - Returned connection 1780132728 to pool.
- DEBUG [main] - Cache Hit Ratio [mapper.UserMapper]: 0.5
- DEBUG [main] - Opening JDBC Connection
- DEBUG [main] - Checked out connection 1780132728 from pool.
- DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6a1aab78]
- DEBUG [main] - ==> Preparing: UPDATE user SET username = ? , birthday = ? , sex = ? , address = ? where id = ?
- DEBUG [main] - ==> Parameters: 二级缓存已更新(String), 2017-06-19 00:00:00.0(Timestamp), 1(String), 湖南益阳(String), 29(Integer)
- DEBUG [main] - <== Updates: 1
- DEBUG [main] - Committing JDBC Connection [com.mysql.jdbc.JDBC4Connection@6a1aab78]
- DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6a1aab78]
- DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@6a1aab78]
- DEBUG [main] - Returned connection 1780132728 to pool.
- DEBUG [main] - Cache Hit Ratio [mapper.UserMapper]: 0.3333333333333333
- DEBUG [main] - Opening JDBC Connection
- DEBUG [main] - Checked out connection 1780132728 from pool.
- DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6a1aab78]
- DEBUG [main] - ==> Preparing: SELECT * FROM USER WHERE id = ?
- DEBUG [main] - ==> Parameters: 29(Integer)
- DEBUG [main] - <== Total: 1
- User{id=29, username='二级缓存已更新', sex='1', birthday=Mon Jun 19 00:00:00 CST 2017, address='湖南益阳', orderList=null}
- DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6a1aab78]
- DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@6a1aab78]
- DEBUG [main] - Returned connection 1780132728 to pool.
- Process finished with exit code 0
可以看到此处,在我们执行了SQLSession3的commit操作后,该mapper的二级缓存就被清空了,当SQLSession2再次执行查询操作时,请求了数据库。
5、userCache配置
在statement中设置useCache=false可以禁用当前select语句的二级缓存,即每次查询都会发出sql去查询,默认情况是true,即该sql使用二级缓存。
- <select id="findOrderListResultMap" resultMap="ordersUserMap" useCache="false">
总结:针对每次查询都需要最新的数据sql,要设置成useCache=false,禁用二级缓存。
6、刷新缓存(就是清空缓存)
在mapper的同一个namespace中,如果有其它insert、update、delete操作数据后需要刷新缓存,如果不执行刷新缓存会出现脏读。
设置statement配置中的flushCache="true" 属性,默认情况下为true即刷新缓存,如果改成false则不会刷新。使用缓存时如果手动修改数据库表中的查询数据会出现脏读。
如下:
- <insert id="insertUser" parameterType="cn.itcast.mybatis.po.User" flushCache="true">
总结:一般下执行完commit操作都需要刷新缓存,flushCache=true表示刷新缓存,这样可以避免数据库脏读。不需要更改该设置
mybatis整合ehcache
1、分布式缓存
2、整合方法(掌握)
3、加入ehcache包
4、整合ehcache(掌握)
- <!--
- 开启本mapper的namespace下的二级缓存
- type:指定cache接口的实现类的类型,默认使用PerpetualCache,要和EhCache整合,需要配置type为ehcache实现cache接口的类型
- -->
- <cache type="org.mybatis.caches.ehcache.EhcacheCache" />
5、加入EhCache配置文件
classpath下添加:ehcache.xml
内容如下:
- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
- <diskStore path="F:\develop\ehcache" />
- <defaultCache
- maxElementsInMemory="1000"
- maxElementsOnDisk="10000000"
- eternal="false"
- overflowToDisk="false"
- timeToIdleSeconds="120"
- timeToLiveSeconds="120"
- diskExpiryThreadIntervalSeconds="120"
- memoryStoreEvictionPolicy="LRU">
- </defaultCache>
- </ehcache>
6、测试代码
- //二级缓存测试
- @Test
- public void testCache2() throws Exception{
- //创建SqlSession1与SqlSession2
- SqlSession sqlSession1 = sqlSessionFactory.openSession();
- SqlSession sqlSession2 = sqlSessionFactory.openSession();
- SqlSession sqlSession3 = sqlSessionFactory.openSession();
- UserMapper userMapper1 = sqlSession1.getMapper(UserMapper.class);
- UserMapper userMapper2 = sqlSession2.getMapper(UserMapper.class);
- UserMapper userMapper3 = sqlSession3.getMapper(UserMapper.class);
- //第一次发起请求,查询ID为29的用户
- User user1 = userMapper1.findUserById(29);
- System.out.println(user1);
- //执行关闭操作,将sqlSession的数据写到二级缓存区域
- sqlSession1.close();
- //第三次发起请求,执行commit操作
- User user3 = userMapper3.findUserById(29);
- user3.setUsername("二级缓存已更新");
- userMapper3.updateUser(user3);
- //执行提交操作,清空UserMapper下的二级缓存
- sqlSession3.commit();
- //执行关闭操作,将sqlSession的数据写到二级缓存区域
- sqlSession3.close();
- //第二次发起请求,查询ID为29的用户
- User user2 = userMapper2.findUserById(29);
- System.out.println(user2);
- //执行关闭操作,将sqlSession的数据写到二级缓存区域
- sqlSession2.close();
- }
7、运行结果
- DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
- DEBUG [main] - Class not found: org.jboss.vfs.VFS
- DEBUG [main] - JBoss 6 VFS API is not available in this environment.
- DEBUG [main] - Class not found: org.jboss.vfs.VirtualFile
- DEBUG [main] - VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
- DEBUG [main] - Using VFS adapter org.apache.ibatis.io.DefaultVFS
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
- DEBUG [main] - Reader entry: Items.class
- DEBUG [main] - Reader entry: Order.class
- DEBUG [main] - Reader entry: OrderCustom.class
- DEBUG [main] - Reader entry: OrderDetail.class
- DEBUG [main] - Reader entry: User.class
- DEBUG [main] - Reader entry: UserCustom.class
- DEBUG [main] - Reader entry: UserQueryVo.class
- DEBUG [main] - Listing file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Items.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Items.class
- DEBUG [main] - Reader entry: ���� 4 W
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Order.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Order.class
- DEBUG [main] - Reader entry: ���� 4 f
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderCustom.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderCustom.class
- DEBUG [main] - Reader entry: ���� 4 "
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderDetail.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderDetail.class
- DEBUG [main] - Reader entry: ���� 4 J
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/User.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/User.class
- DEBUG [main] - Reader entry: ���� 4 ]
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserCustom.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserCustom.class
- DEBUG [main] - Reader entry: ���� 4
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserQueryVo.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserQueryVo.class
- DEBUG [main] - Reader entry: ���� 4 %
- DEBUG [main] - Checking to see if class pojo.Items matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.Order matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.OrderCustom matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.OrderDetail matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.User matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.UserCustom matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class pojo.UserQueryVo matches criteria [is assignable to Object]
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
- DEBUG [main] - Reader entry: OrderMapperCustom.class
- DEBUG [main] - Reader entry: OrderMapperCustom.xml
- DEBUG [main] - Reader entry: UserMapper.class
- DEBUG [main] - Reader entry: UserMapper.xml
- DEBUG [main] - Listing file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.class
- DEBUG [main] - Reader entry: ���� 4
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.xml
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.xml
- DEBUG [main] - Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.class
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.class
- DEBUG [main] - Reader entry: ���� 4 findUserByIdResultMap (I)Lpojo/User;
- DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.xml
- DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.xml
- DEBUG [main] - Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
- DEBUG [main] - Checking to see if class mapper.OrderMapperCustom matches criteria [is assignable to Object]
- DEBUG [main] - Checking to see if class mapper.UserMapper matches criteria [is assignable to Object]
- DEBUG [main] - Configuring ehcache from ehcache.xml found in the classpath: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/ehcache.xml
- DEBUG [main] - Configuring ehcache from URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/ehcache.xml
- DEBUG [main] - Configuring ehcache from InputStream
- DEBUG [main] - Ignoring ehcache attribute xmlns:xsi
- DEBUG [main] - Ignoring ehcache attribute xsi:noNamespaceSchemaLocation
- DEBUG [main] - Disk Store Path: F:\develop\ehcache
- DEBUG [main] - Creating new CacheManager with default config
- DEBUG [main] - propertiesString is null.
- DEBUG [main] - No CacheManagerEventListenerFactory class specified. Skipping...
- DEBUG [main] - No BootstrapCacheLoaderFactory class specified. Skipping...
- DEBUG [main] - CacheWriter factory not configured. Skipping...
- DEBUG [main] - No CacheExceptionHandlerFactory class specified. Skipping...
- DEBUG [main] - Initialized net.sf.ehcache.store.NotifyingMemoryStore for mapper.UserMapper
- DEBUG [main] - Initialised cache: mapper.UserMapper
- DEBUG [main] - CacheDecoratorFactory not configured for defaultCache. Skipping for 'mapper.UserMapper'.
- DEBUG [main] - Cache Hit Ratio [mapper.UserMapper]: 0.0
- DEBUG [main] - Opening JDBC Connection
- DEBUG [main] - Created connection 631659383.
- DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@25a65b77]
- DEBUG [main] - ==> Preparing: SELECT * FROM USER WHERE id = ?
- DEBUG [main] - ==> Parameters: 29(Integer)
- DEBUG [main] - <== Total: 1
- User{id=29, username='二级缓存已更新', sex='1', birthday=Mon Jun 19 00:00:00 CST 2017, address='湖南益阳', orderList=null}
- DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@25a65b77]
- DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@25a65b77]
- DEBUG [main] - Returned connection 631659383 to pool.
- DEBUG [main] - Cache Hit Ratio [mapper.UserMapper]: 0.5
- DEBUG [main] - Opening JDBC Connection
- DEBUG [main] - Checked out connection 631659383 from pool.
- DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@25a65b77]
- DEBUG [main] - ==> Preparing: UPDATE user SET username = ? , birthday = ? , sex = ? , address = ? where id = ?
- DEBUG [main] - ==> Parameters: 二级缓存已更新(String), 2017-06-19 00:00:00.0(Timestamp), 1(String), 湖南益阳(String), 29(Integer)
- DEBUG [main] - <== Updates: 1
- DEBUG [main] - Committing JDBC Connection [com.mysql.jdbc.JDBC4Connection@25a65b77]
- DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@25a65b77]
- DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@25a65b77]
- DEBUG [main] - Returned connection 631659383 to pool.
- DEBUG [main] - Cache Hit Ratio [mapper.UserMapper]: 0.3333333333333333
- DEBUG [main] - Opening JDBC Connection
- DEBUG [main] - Checked out connection 631659383 from pool.
- DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@25a65b77]
- DEBUG [main] - ==> Preparing: SELECT * FROM USER WHERE id = ?
- DEBUG [main] - ==> Parameters: 29(Integer)
- DEBUG [main] - <== Total: 1
- User{id=29, username='二级缓存已更新', sex='1', birthday=Mon Jun 19 00:00:00 CST 2017, address='湖南益阳', orderList=null}
- DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@25a65b77]
- DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@25a65b77]
- DEBUG [main] - Returned connection 631659383 to pool.
- Process finished with exit code 0
二级缓存的应用场景
对于访问多的查询请求且用户对查询结果实时性要求不高,此时可采用mybatis二级缓存技术降低数据库访问量,提高访问速度,业务场景比如:耗时较高的统计分析sql、电话账单查询sql等。
实现方法如下:通过设置刷新间隔时间,由mybatis每隔一段时间自动清空缓存,根据数据变化频率设置缓存刷新间隔flushInterval,比如设置为30分钟、60分钟、24小时等,根据需求而定。
二级缓存的局限性
mybatis二级缓存对细粒度的数据级别的缓存实现不好,比如如下需求:对商品信息进行缓存,由于商品信息查询访问量大,但是要求用户每次都能查询最新的商品信息,此
时如果使用mybatis的二级缓存就无法实现当一个商品变化时只刷新该商品的缓存信息而不刷新其它商品的信息,因为mybaits的二级缓存区域以mapper为单位划分,当一个商品信
息变化会将所有商品信息的缓存数据全部清空。解决此类问题需要在业务层根据需求对数据有针对性缓存。
- mybatis学习之查询缓存
- mybatis学习之查询缓存
- MyBatis之查询缓存
- mybatis学习笔记(14)-查询缓存之一级缓存
- mybatis学习笔记(13)-查询缓存之一级缓存
- Mybatis学习笔记(十五)【查询缓存之一级缓存】
- MyBatis学习之Mybatis缓存
- mybatis学习笔记(15)-查询缓存之二级缓存
- Mybatis学习笔记(十六)【查询缓存之二级缓存】
- Mybatis学习之查询
- mybatis学习六之缓存
- 查询缓存---Mybatis学习笔记(十)
- 查询缓存---Mybatis学习笔记(十)
- 【JAVA - SSM】之MyBatis查询缓存
- Mybatis学习(十三)mybatis查询缓存理解
- mybatis进阶(7)--查询缓存之一级缓存
- MyBatis--查询缓存--一级缓存
- 【Mybatis学习】Mybatis缓存
- 数据库考试之-数据结构
- WAF安全应用防火墙(nginx+lua)
- [Leetcode] 225. Implement Stack using Queues 解题报告
- 高德地图 一 SHA 1 值的获取和sdk的集成
- linux系统操作指令速查速记
- mybatis学习之查询缓存
- Co-Fusion:多目标实时分割、跟踪与融合
- centos设置文件夹打开方式
- Sum It Up
- Servlet的生命周期
- 金融如何在游戏行业分一杯羹?
- 【JavaWeb笔记】Servlet和JSP的区别
- 03. 抓包过滤器 ❀ 数据包分析工具 Wireshark
- Android平板开发