MyBatis-01:缓存

 MyBatis一级缓存

一级缓存实效的情况

1、不是同一个SqlSession;

2、是同一个SqlSession但是查询条件不一样;

3、同一个SqlSession但是,在两次查询的过程中进行了增删改;

4、通过SqlSession.clearCache()手动清空缓存;

/**
     * 默认开启一级缓存,也就是在同一个sqlSession中查询同样到数据,
     * 第二次会从缓存中获取而不会执行sql语句
     * 跟是不是一个mapper没有关系
     */
    @Test
    public void testCacheMapperTest(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        CacheMapper mapper = sqlSession.getMapper(CacheMapper.class);
        CacheMapper mapper1 = sqlSession.getMapper(CacheMapper.class);
        Emp empByEid = mapper.getEmpByEid(1);
        System.out.println(empByEid);
        Emp empByEid1 = mapper.getEmpByEid(1);
        System.out.println(empByEid1);
        Emp empByEid2 = mapper1.getEmpByEid(1);
        System.out.println(empByEid2);
        //当不是同一个sqlsession的时候就不会从缓存中获取
        SqlSession sqlSession1 = SqlSessionUtils.getSqlSession();
        CacheMapper mapper2 = sqlSession1.getMapper(CacheMapper.class);
        Emp empByEid3 = mapper2.getEmpByEid(1);
        System.out.println(empByEid3);
        sqlSession.cl
    }

MyBatis二级缓存

二级缓存是SqlSessionFactory级别

SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
            SqlSessionFactory factory = sqlSessionFactoryBuilder.build(resourceAsStream);
            sqlSession = factory.openSession(true);

 二级缓存开启条件

1、在核心配置文件中,设置全局属性cacheEnabled="true“;默认为true

2、在映射文件中设置<cache />标签

3、二级缓存必须在SqlSession关闭或者提交之后才有效;

4、查询的数据所转换的实体类类型必须实现序列化的接口;

使二级缓存失效的情况

手动清除缓存只对一级缓存有效;

两次查询之间进行了任意的增删改,会让一级和二级缓存失效;

@Test
    public void testTwoCache(){
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
            //同一个SqlSessionFactory
            SqlSessionFactory SqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
            //不同SqlSession
            SqlSession sqlSession1 = SqlSessionFactory.openSession(true);
            CacheMapper mapper1 = sqlSession1.getMapper(CacheMapper.class);
            Emp emp1 = mapper1.getEmpByEid(1);
            System.out.println(emp1);
            sqlSession1.close();
            System.out.println("**********************************");
            SqlSession sqlSession2 = SqlSessionFactory.openSession(true);
            CacheMapper mapper2 = sqlSession2.getMapper(CacheMapper.class);
            Emp emp2 = mapper2.getEmpByEid(1);
            System.out.println(emp2);
            sqlSession2.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
DEBUG 03-03 16:57:12,044 Cache Hit Ratio [whut.zyf.mybatis.mapper.CacheMapper]: 0.0(LoggingCache.java:60) 
DEBUG 03-03 16:57:12,662 ==>  Preparing: select * from t_emp where eid = ?(BaseJdbcLogger.java:137) 
DEBUG 03-03 16:57:12,738 ==> Parameters: 1(Integer)(BaseJdbcLogger.java:137) 
DEBUG 03-03 16:57:12,836 <==      Total: 1(BaseJdbcLogger.java:137) 
Emp{eid=1, empName='张三', age=23, sex='男', email='123@qq.com', dept=null}
**********************************
WARN  03-03 16:57:12,851 As you are using functionality that deserializes object streams, it is recommended to define the JEP-290 serial filter. Please refer to https://docs.oracle.com/pls/topic/lookup?ctx=javase15&id=GUID-8296D8E8-2B93-4B9A-856E-0A65AF9B8C66(SerialFilterChecker.java:46) 
DEBUG 03-03 16:57:12,862 Cache Hit Ratio [whut.zyf.mybatis.mapper.CacheMapper]: 0.5(LoggingCache.java:60) 
Emp{eid=1, empName='张三', age=23, sex='男', email='123@qq.com', dept=null}

 补充:MyBatis插件工具使用步骤

/**
 * 分页插件使用
 * 1、pom.xml中配置
 * <dependency>
 *             <groupId>com.github.pagehelper</groupId>
 *             <artifactId>pagehelper</artifactId>
 *             <version>5.2.0</version>
 * </dependency>
 * 2、在mybatis核心配置文件mybatis-config.xml中配置
 * <plugins>
 *         <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
 *</plugins>
 */

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小馨java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值