mybatis的二级缓存机制

mapper.xml的文件配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yin.mapper.TeacherMapper">
//开启二级缓存机制的配置,设置每个60秒自动刷新一次,最大缓存量512,只读开启
    <cache eviction="FIFO"
           flushInterval="60000"
           size="512"
           readOnly="true"/>

    <select id="getTeacher3" resultMap="TeacherStudent2">
        select * from teacher where id=#{id};
    </select>

    <resultMap id="TeacherStudent2" type="Teacher">
        <collection property="students" column="id" javaType="ArrayList"
        ofType="Student" select="getStudentByTeacherId"></collection>
    </resultMap>

    <select id="getStudentByTeacherId" resultType="Student">
        select * from student where tid=#{id};
    </select>

</mapper>

一个测试类

 @Test
    public void test3(){
        SqlSession sqlSession = MybatisUtils.getSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher = mapper.getTeacher3(1);
        System.out.println(teacher);

        for (Student student : teacher.getStudents()
        ) {
            System.out.println(student);

        }
        sqlSession.close();
        System.out.println("--------------------------------");

        SqlSession sqlSession2 = MybatisUtils.getSession();
        TeacherMapper mapper2 = sqlSession2.getMapper(TeacherMapper.class);
        Teacher teacher2 = mapper2.getTeacher3(1);
        System.out.println(teacher2);

        for (Student student : teacher2.getStudents()
        ) {
            System.out.println(student);
        }
        System.out.println(teacher==teacher2);
        sqlSession2.close();
    }

控制台输出

Created connection 2011482127.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@77e4c80f]
==>  Preparing: select * from teacher where id=?;
==> Parameters: 1(Integer)
<==    Columns: id, name
<==        Row: 1, 秦老师
Cache Hit Ratio [com.yin.mapper.TeacherMapper]: 0.0
====>  Preparing: select * from student where tid=?;
====> Parameters: 1(Integer)
<====    Columns: id, name, tid
<====        Row: 1, 小明, 1
<====        Row: 2, 小红, 1
<====        Row: 3, 小张, 1
<====        Row: 4, 小李, 1
<====        Row: 5, 小王, 1
<====      Total: 5
<==      Total: 1
Teacher(id=null, name=秦老师, students=[Student(id=1, name=小明, tid=1, teacher=null), Student(id=2, name=小红, tid=1, teacher=null), Student(id=3, name=小张, tid=1, teacher=null), Student(id=4, name=小李, tid=1, teacher=null), Student(id=5, name=小王, tid=1, teacher=null)])
Student(id=1, name=小明, tid=1, teacher=null)
Student(id=2, name=小红, tid=1, teacher=null)
Student(id=3, name=小张, tid=1, teacher=null)
Student(id=4, name=小李, tid=1, teacher=null)
Student(id=5, name=小王, tid=1, teacher=null)
Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@77e4c80f]
Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@77e4c80f]
Returned connection 2011482127 to pool.
--------------------------------
Cache Hit Ratio [com.yin.mapper.TeacherMapper]: 0.3333333333333333
Teacher(id=null, name=秦老师, students=[Student(id=1, name=小明, tid=1, teacher=null), Student(id=2, name=小红, tid=1, teacher=null), Student(id=3, name=小张, tid=1, teacher=null), Student(id=4, name=小李, tid=1, teacher=null), Student(id=5, name=小王, tid=1, teacher=null)])
Student(id=1, name=小明, tid=1, teacher=null)
Student(id=2, name=小红, tid=1, teacher=null)
Student(id=3, name=小张, tid=1, teacher=null)
Student(id=4, name=小李, tid=1, teacher=null)
Student(id=5, name=小王, tid=1, teacher=null)

分析:
从控制台可以看出在第一次查询关闭后,第一次查询的数据直接放入二级缓存中,后面的相同查询会直接从以及查询中查询数据,这就是mybatis二级缓存的机制。

上一篇:spring-ioc注解-理解2 零配置文件


下一篇:sqlserver查询语法