mybatis提供查询缓存,用于减轻数据压力,提高数据库性能
mybatis提供一级缓存和二级缓存
一级缓存是SQLSession的缓存
在操作数据库时需要构造SQLSession对象,在对象中有一个数据结构(HashMap)用于存储缓存数据
不同的SQLSession之间的缓存数据区域(HashMap)是互相不影响的
二级缓存是mapper级别的缓存
多个SqlSession,操作同一个mapper的SQL语句,多个SqlSession可以共用二级缓存,二级缓存是跨SQLSession的
//一级缓存测试
@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:
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:
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:
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:
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
UserMapper有一个二级缓存区域(按照namespace分),其他的Mapper也有自己的二级缓存区域(按照namespace分)。
每一个namespace的mapper,都有一个二级缓存区域。两个mapper的namespace如果相同,那么这两个mapper执行sql查询到的数据会存储在相同的二级缓存区域中。
在核心配置文件SqlMapConfig.xml中加入
<setting name="cacheEnabled" value="true"/>
|
描述 |
允许值 |
默认值 |
cacheEnabled |
对在此配置文件下的所有cache 进行全局性开/关设置。 |
true false |
true |
public class User implements Serializable{
//属性名和数据库表的字段一一对应
private Integer id;
private String username;// 用户姓名
private String sex;// 性别
private Date birthday;// 生日
private String address;// 地址
为了将缓存数据取出,执行反序列化操作,因为二级缓存数据存储介质多种多样,不一定在内存中。
//二级缓存测试
@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:
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:
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
//二级缓存测试
@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:
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:
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
在statement中设置useCache=false可以禁用当前select语句的二级缓存,即每次查询都会发出sql去查询,默认情况是true,即该sql使用二级缓存。
总结:针对每次查询都需要最新的数据sql,要设置成useCache=false,禁用二级缓存。
在mapper的同一个namespace中,如果有其它insert、update、delete操作数据后需要刷新缓存,如果不执行刷新缓存会出现脏读。
设置statement配置中的flushCache="true" 属性,默认情况下为true即刷新缓存,如果改成false则不会刷新。使用缓存时如果手动修改数据库表中的查询数据会出现脏读。
如下:
总结:一般下执行完commit操作都需要刷新缓存,flushCache=true表示刷新缓存,这样可以避免数据库脏读。不需要更改该设置
classpath下添加:ehcache.xml
内容如下:
//二级缓存测试
@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:
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:
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为单位划分,当一个商品信
息变化会将所有商品信息的缓存数据全部清空。解决此类问题需要在业务层根据需求对数据有针对性缓存。