一级缓存

一级缓存,在开启sqlSession后会自动开启,关闭sqlSession,就可以关闭一级缓存

1.查询相同的数据,不会刷新缓存

例子

 public void getUserById (){
        SqlSession sqlSession = sqlSessionFactory.getsqlSession();
        userMapper mapper = sqlSession.getMapper(userMapper.class);
        user user1 = mapper.getUserById(2);
        System.out.println(user1);
        System.out.println("=========");
        user user2 = mapper.getUserById(2);
        System.out.println(user2);
        sqlSession.close();
    }

结果

Opening JDBC Connection
Created connection 454884231.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1b1cfb87]
==>  Preparing: select * from user where id=? 
==> Parameters: 2(Integer)
<==    Columns: id, name, pwd
<==        Row: 2, 小落, 12341234
<==      Total: 1
com.Google.pojo.user@19b89d4
=========
com.Google.pojo.user@19b89d4
Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1b1cfb87]
Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1b1cfb87]
Returned connection 454884231 to pool.

2.当执行 增 删 改 命令时,会刷新缓存

例子

    public void getUserById (){
        SqlSession sqlSession = sqlSessionFactory.getsqlSession();
        userMapper mapper = sqlSession.getMapper(userMapper.class);
        user user1 = mapper.getUserById(2);//查找用户
        System.out.println(user1);
        int i = mapper.updateUser(new user(2, "aaa", "bbb"));//修改用户
        if(i>0){
            sqlSession.commit();
        }else{
            sqlSession.rollback();
        } 
        System.out.println("=========");
        user user2 = mapper.getUserById(2);
        System.out.println(user2);
        sqlSession.close();
    }

运行结果

Opening JDBC Connection
Created connection 136393487.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@821330f]
==>  Preparing: select * from user where id=? 
==> Parameters: 2(Integer)
<==    Columns: id, name, pwd
<==        Row: 2, 小落, 12341234
<==      Total: 1
user(id=2, name=小落, pwd=12341234)
==>  Preparing: update user set name = ?,pwd = ? where id=? 
==> Parameters: aaa(String), bbb(String), 2(Integer)
<==    Updates: 1
=========
==>  Preparing: select * from user where id=? 
==> Parameters: 2(Integer)
<==    Columns: id, name, pwd
<==        Row: 2, aaa, bbb
<==      Total: 1
user(id=2, name=aaa, pwd=bbb)
Rolling back JDBC Connection [com.mysql.jdbc.JDBC4Connection@821330f]
Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@821330f]
Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@821330f]
Returned connection 136393487 to pool.

这里我们可以清楚的看到,即使是查询两次相同的结果,缓存还是刷新了。这是因为更新了数据库,所有得强制刷新缓存,要不然,得到的数据就是旧数据

3.手动刷新缓存

  public void getUserById (){
        SqlSession sqlSession = sqlSessionFactory.getsqlSession();
        userMapper mapper = sqlSession.getMapper(userMapper.class);
        user user1 = mapper.getUserById(2);//查找用户
        System.out.println(user1);
        sqlSession.clearCache();//刷新缓存
        System.out.println("=========");
        user user2 = mapper.getUserById(2);
        System.out.println(user2);
        sqlSession.close();
    }

运行结果

Opening JDBC Connection
Created connection 136393487.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@821330f]
==>  Preparing: select * from user where id=? 
==> Parameters: 2(Integer)
<==    Columns: id, name, pwd
<==        Row: 2, aaa, bbb
<==      Total: 1
user(id=2, name=aaa, pwd=bbb)
=========
==>  Preparing: select * from user where id=? 
==> Parameters: 2(Integer)
<==    Columns: id, name, pwd
<==        Row: 2, aaa, bbb
<==      Total: 1
user(id=2, name=aaa, pwd=bbb)
Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@821330f]
Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@821330f]
Returned connection 136393487 to pool.

这里用于手动刷新了缓存,所以出现了两个sql

4.查询不同的Mapper.xml

一级缓存,用处不大,但如果一个用户一直刷新一个页面,那最好用一级缓存

上一篇:MyBatis缓存机制


下一篇:模糊查询防止sql注入