Java第三十七天,Mybatis框架系列,基于注解的缓存控制

一、开启 mybatis 二级缓存的步骤

1.在 mybatis 配置文件 SqlMapConfig 中开启二级缓存支持

<!-- 配置二级缓存 -->
<settings>
    <!-- 开启二级缓存的支持 -->
    <setting name="cacheEnabled" value="true"/>
</settings>

2.在持久层接口中使用注解配置二级缓存

//mybatis 基于注解方式实现配置二级缓存
@CacheNamespace(blocking=true)
public interface XXX {}

二、实战演练

注意:

  • 一级缓存是 SqlSession 级别的缓存,只要 SqlSession 没有 flush 或 close,它就存在
  • 二级缓存是 mapper 映射级别的缓存,多个 SqlSession 去操作同一个 Mapper 映射的 sql 语句,多个SqlSession 可以共用二级缓存,二级缓存是跨 SqlSession 的

1.SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 配置properties-->
    <properties resource="jdbcConfig.properties"></properties>

    <!--全局开启缓存-->
    <settings>
        <setting name="cacheEnabled" value="true"/>
    </settings>
    
    <!--使用typeAliases配置别名,它只能配置domain中类的别名 -->
    <typeAliases>
        <package name="com.lanyue.domain"></package>
    </typeAliases>

    <!--配置环境-->
    <environments default="mysql">
        <!-- 配置mysql的环境-->
        <environment id="mysql">
            <!-- 配置事务 -->
            <transactionManager type="JDBC"></transactionManager>

            <!--配置连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"></property>
                <property name="url" value="${jdbc.url}"></property>
                <property name="username" value="${jdbc.username}"></property>
                <property name="password" value="${jdbc.password}"></property>
            </dataSource>
        </environment>
    </environments>
    <!-- 配置映射文件的位置 -->
    <mappers>
        <package name="com.lanyue.dao"></package>
    </mappers>
</configuration>

2.IUserDao.java

package com.lanyue.dao;

import com.lanyue.domain.User;
import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Select;

@CacheNamespace(blocking = true)
public interface IUserDao {

    @Select("select * from user where id = #{id}")
    User findById(Integer userId);
}

3.测试类

package com.lanyue.test;

import com.lanyue.dao.IUserDao;
import com.lanyue.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;

public class SecondLevelCacheTest {

    private InputStream in;
    private  SqlSessionFactory factory;

    @Before//用于在测试方法执行之前执行
    public void init()throws Exception{
        //1.读取配置文件,生成字节输入流
        in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.获取SqlSessionFactory
        factory = new SqlSessionFactoryBuilder().build(in);

    }

    @After//用于在测试方法执行之后执行
    public void destroy()throws Exception{
        in.close();
    }

    @Test
    public void testFirstLevelCache(){
        SqlSession session1 = factory.openSession();
        IUserDao dao1 = session1.getMapper(IUserDao.class);
        User user1 = dao1.findById(41);
        //一级缓存消失
        session1.close();

        SqlSession session2 = factory.openSession();
        IUserDao dao2 = session2.getMapper(IUserDao.class);
        User user2 = dao2.findById(41);
        session2.close();

        System.out.println(user1);
        System.out.println(user2);
        System.out.println(user1 == user2);
    }


}

 

posted @ 2020-08-10 15:43  IT蓝月  阅读(135)  评论(0编辑  收藏  举报
Live2D