_024_MyBatis_证明二级缓存存在

 /*开启内置的二级缓存步骤
    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);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值