Level 1 cache













The second level cache



<settings> <! <setting name="cacheEnabled" value="true"/> </ Settings >Copy the code
<cache/>Copy the code
public class User implements Serializable {}Copy the code


@Test
public void testCache2() throws Exception {
    SqlSession sqlSession1 = sqlSessionFactory.openSession();
    SqlSession sqlSession2 = sqlSessionFactory.openSession();
    UserMapper userMapper1 = sqlSession1.getMapper(UserMapper.class);
    User user1 = userMapper1.findUserById(1);
    System.out.println(user1);
    sqlSession1.close();
    UserMapper userMapper2 = sqlSession2.getMapper(UserMapper.class);
    User user2 = userMapper2.findUserById(1);
    System.out.println(user2);
    sqlSession2.close();
}Copy the code
DEBUG [main] - Cache Hit Ratio [com.iot.mybatis.mapper.UserMapper]: 0.0 DEBUG [main] -opening JDBC Connection DEBUG [main] -created Connection 103887628. DEBUG [main] -setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@631330c] DEBUG [main] - ==> Preparing: SELECT * FROM user WHERE id=? DEBUG [main] - ==> Parameters: 1(Integer) DEBUG [main] - <== Total: 1 User [id=1, username= 3, sex=1, birthday=null, address=null] DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@631330c] DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@631330c] DEBUG [main] - Returned connection 103887628 to pool. DEBUG [main] - Cache Hit Ratio [com.iot.mybatis.mapper.UserMapper]: 0.5 User [id=1, username= 3, sex=1, birthday=null, address=null]Copy the code