二级缓存

开启二级缓存:
mybatis-config.xml

    <settings>
        <!-- 开启全局缓存开关; -->
        <setting name="cacheEnabled" value="true"/>
    </settings>

在需要使用的二级缓存的dao层标注:
TeacherDao.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.dao.TeacherDao">
    <!-- 使用mybatis默认二级缓存 -->
    <cache></cache>
    <cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache>
    
    <resultMap type="com.atguigu.bean.Teacher" id="teacherMap">
        <id property="id" column="id" />
        <result property="address" column="address" />
        <result property="birth" column="birth_date" />
        <result property="course" column="class_name" />
        <result property="name" column="teacherName" />
    </resultMap>

另外,该bean需要实现序列化接口

public class Teacher implements Serializable{

二级缓存是namespace级别缓存

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。