开启二级缓存:
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级别缓存