/*开启内置的二级缓存步骤
1、对实体进行序列化,implements Serializable
2、在相应的dao映射文件中添加<cache/>标签*/
Dao映射文件:
<mapper namespace="org.lfz.dao.IStudentDao">
<cache />
<insert id="insertStudent">
insert into tb_student(name,age,score) values(#{name},#{age},#{score})
</insert>
<select id="selectStudentById" resultType="Student">
select id,name,age,score from tb_student where id=#{xxx}
</select>
<select id="selectStudentById2" resultType="Student">
select id,name,age,score from tb_student where id=#{xxx}
</select>
</mapper>
JUnitTest
/*证明二级缓存的存在*/
/*开启内置的二级缓存步骤
1、对实体进行序列化
2、在相应的dao映射文件中添加<cache/>标签*/
@Test
public void testSelectStudentById() {
sqlSession = MyBatisUtils.getSqlSession();
dao = sqlSession.getMapper(IStudentDao.class);
Student stu = dao.selectStudentById(2);
System.out.println(stu);
/*消除一级缓存的影响,
* 一旦sqlSession的关闭,
一级缓存也就被清空*/
sqlSession.close();
sqlSession = MyBatisUtils.getSqlSession();
dao = sqlSession.getMapper(IStudentDao.class);
Student stu1 = dao.selectStudentById(2);
System.out.println(stu1);
}