Mybatis-一级缓存

  1. 会话级的缓存,一旦会话关闭缓存就会消失。一级缓存用map实现。
  2. Mybatis默认开启一级缓存。
  3. 执行流程:当同一个会话中执行查询时,会先去缓存查找以该查询条件为键的值,如存在则缓存命中并返回结果,不存在则发送SQL语句给数据库服务器查询。
  4. 增删改操作默认清空一级缓存和二级缓存。

以下案例不开启二级缓存
(1)Mapper文件


DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wsh.mapper.UserMapper">
    <resultMap id="EmployeeResult" type="Employee">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="deptId" column="dept_id"/>
    resultMap>
    
    <select id="selectEmployeeList" resultMap="EmployeeResult">
        select
            *
        from
            employee
        where
            id = #{id}
    select>

mapper>

(2)接口文件

public interface UserMapper {
    public List<Employee> selectEmployeeList(Long id);
}

(3)JAVA程序

    public void test() throws IOException {
        //读取配置文件创建SqlSession工厂
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //利用SqlSession工厂创建SqlSession实例
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try {
            //利用SqlSession创建代理对象
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            mapper.selectEmployeeList(1L);
            mapper.selectEmployeeList(1L);
        }finally {
            sqlSession.close();
        }
    }

输出(同一会话中,两次查询的条件相同,中间缓存没有被清空,缓存命中)

2022-04-05 16:57:18,187 [main] DEBUG [com.wsh.mapper.UserMapper.selectEmployeeList] - ==>  Preparing: select * from employee where id = ?
2022-04-05 16:57:18,214 [main] DEBUG [com.wsh.mapper.UserMapper.selectEmployeeList] - ==> Parameters: 1(Long)
2022-04-05 16:57:18,233 [main] DEBUG [com.wsh.mapper.UserMapper.selectEmployeeList] - <==      Total: 1

当在两个会话中同时执行相同条件的查询时

    public void test() throws IOException {
        //读取配置文件创建SqlSession工厂
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //利用SqlSession工厂创建SqlSession实例
        SqlSession sqlSession1 = sqlSessionFactory.openSession();
        SqlSession sqlSession2 = sqlSessionFactory.openSession();
        try {
            //利用SqlSession创建代理对象
            UserMapper mapper1 = sqlSession1.getMapper(UserMapper.class);
            mapper1.selectEmployeeList(1L);
            UserMapper mapper2 = sqlSession2.getMapper(UserMapper.class);
            mapper2.selectEmployeeList(1L);
        }finally {
            sqlSession1.close();
            sqlSession2.close();
        }
    }

输出

2022-04-05 17:00:58,149 [main] DEBUG [com.wsh.mapper.UserMapper.selectEmployeeList] - ==>  Preparing: select * from employee where id = ?
2022-04-05 17:00:58,176 [main] DEBUG [com.wsh.mapper.UserMapper.selectEmployeeList] - ==> Parameters: 1(Long)
2022-04-05 17:00:58,195 [main] DEBUG [com.wsh.mapper.UserMapper.selectEmployeeList] - <==      Total: 1
2022-04-05 17:00:58,224 [main] DEBUG [com.wsh.mapper.UserMapper.selectEmployeeList] - ==>  Preparing: select * from employee where id = ?
2022-04-05 17:00:58,224 [main] DEBUG [com.wsh.mapper.UserMapper.selectEmployeeList] - ==> Parameters: 1(Long)
2022-04-05 17:00:58,225 [main] DEBUG [com.wsh.mapper.UserMapper.selectEmployeeList] - <==      Total: 1

当会话中执行增删改操作时

    public void test() throws IOException {
        //读取配置文件创建SqlSession工厂
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //利用SqlSession工厂创建SqlSession实例
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try {
            //利用SqlSession创建代理对象
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            mapper.selectEmployeeList(1L);
            mapper.updateEmployee();
            mapper.selectEmployeeList(1L);
        }finally {
            sqlSession.close();
        }
    }

输出

2022-04-05 17:03:37,992 [main] DEBUG [com.wsh.mapper.UserMapper.selectEmployeeList] - ==>  Preparing: select * from employee where id = ?
2022-04-05 17:03:38,023 [main] DEBUG [com.wsh.mapper.UserMapper.selectEmployeeList] - ==> Parameters: 1(Long)
2022-04-05 17:03:38,049 [main] DEBUG [com.wsh.mapper.UserMapper.selectEmployeeList] - <==      Total: 1
2022-04-05 17:03:38,049 [main] DEBUG [com.wsh.mapper.UserMapper.updateEmployee] - ==>  Preparing: update employee set id = 1 where id = 1
2022-04-05 17:03:38,049 [main] DEBUG [com.wsh.mapper.UserMapper.updateEmployee] - ==> Parameters: 
2022-04-05 17:03:38,051 [main] DEBUG [com.wsh.mapper.UserMapper.updateEmployee] - <==    Updates: 1
2022-04-05 17:03:38,052 [main] DEBUG [com.wsh.mapper.UserMapper.selectEmployeeList] - ==>  Preparing: select * from employee where id = ?
2022-04-05 17:03:38,052 [main] DEBUG [com.wsh.mapper.UserMapper.selectEmployeeList] - ==> Parameters: 1(Long)
2022-04-05 17:03:38,057 [main] DEBUG [com.wsh.mapper.UserMapper.selectEmployeeList] - <==      Total: 1

你可能感兴趣的:(Mybatis,后端,java)