专栏首页数据分析与挖掘mybatis缓存之一级缓存

mybatis缓存之一级缓存

一级缓存:与数据库同一次会话期间查询到的数据会放在本地缓存中,以后如果需要获取相同的数据,直接从缓存中拿,没必要再去查询数据库。

package com.gong.mybatis.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.Test;

import com.gong.mybatis.bean.Department;
import com.gong.mybatis.bean.Employee;
import com.gong.mybatis.dao.EmployeeMapper;
import com.gong.mybatis.mapper.EmployeeMapperDynamicSql;

public class TestMybatis4 {
    
    public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(is);
    }

    @Test
    public void test() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
            Employee em = mapper.getEmpById(1);
            System.out.println(em);
            Employee em2 = mapper.getEmpById(1);
            System.out.println(em2);
            System.out.println(em == em2);
            openSession.commit();
        } finally {
            // TODO: handle finally clause
            openSession.close();
        }
        
        
    }
    
}

输出结果:

DEBUG 01-22 10:18:00,037 ==>  Preparing: select id,last_name lastName,email,gender from tbl_employee where id = ?   (BaseJdbcLogger.java:145) 
DEBUG 01-22 10:18:00,108 ==> Parameters: 1(Integer)  (BaseJdbcLogger.java:145) 
DEBUG 01-22 10:18:00,175 <==      Total: 1  (BaseJdbcLogger.java:145) 
Employee [id=1, lastName=dema, gender=1, email=dema@qq.com, dept=null]
Employee [id=1, lastName=dema, gender=1, email=dema@qq.com, dept=null]
true

说明:一级缓存的作用:在一次会话中,使用了相同的查询方法,所以系统只会发送一次sql请求,同时可以看到,em和em2是同一个对象,说明em2并不是重新发送sql请求得到的,而是直接从缓存中获得的。

一级缓存失效的四种情况:

1、使用不同的SqlSession对象

package com.gong.mybatis.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.Test;

import com.gong.mybatis.bean.Department;
import com.gong.mybatis.bean.Employee;
import com.gong.mybatis.dao.EmployeeMapper;
import com.gong.mybatis.mapper.EmployeeMapperDynamicSql;

public class TestMybatis4 {
    
    public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(is);
    }

    @Test
    public void test() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
            Employee em = mapper.getEmpById(1);
            System.out.println(em);
            
            SqlSession openSession2 = sqlSessionFactory.openSession();
            EmployeeMapper mapper2 = openSession2.getMapper(EmployeeMapper.class);
            Employee em2 = mapper2.getEmpById(1);
            System.out.println(em2);
            
            System.out.println(em == em2);
            openSession.commit();
        } finally {
            // TODO: handle finally clause
            openSession.close();
        }
        
        
        
    }
    
}

结果:

DEBUG 01-22 10:23:05,279 ==>  Preparing: select id,last_name lastName,email,gender from tbl_employee where id = ?   (BaseJdbcLogger.java:145) 
DEBUG 01-22 10:23:05,323 ==> Parameters: 1(Integer)  (BaseJdbcLogger.java:145) 
DEBUG 01-22 10:23:05,367 <==      Total: 1  (BaseJdbcLogger.java:145) 
Employee [id=1, lastName=dema, gender=1, email=dema@qq.com, dept=null]
DEBUG 01-22 10:23:05,387 ==>  Preparing: select id,last_name lastName,email,gender from tbl_employee where id = ?   (BaseJdbcLogger.java:145) 
DEBUG 01-22 10:23:05,388 ==> Parameters: 1(Integer)  (BaseJdbcLogger.java:145) 
DEBUG 01-22 10:23:05,393 <==      Total: 1  (BaseJdbcLogger.java:145) 
Employee [id=1, lastName=dema, gender=1, email=dema@qq.com, dept=null]
false

2、同一个SqlSession对象,但查询条件不一样:

package com.gong.mybatis.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.Test;

import com.gong.mybatis.bean.Department;
import com.gong.mybatis.bean.Employee;
import com.gong.mybatis.dao.EmployeeMapper;
import com.gong.mybatis.mapper.EmployeeMapperDynamicSql;

public class TestMybatis4 {
    
    public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(is);
    }

    @Test
    public void test() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
            Employee em = mapper.getEmpById(1);
            System.out.println(em);
            
//            SqlSession openSession2 = sqlSessionFactory.openSession();
//            EmployeeMapper mapper2 = openSession2.getMapper(EmployeeMapper.class);
            Employee em2 = mapper.getEmpById(3);
            System.out.println(em2);
            
            System.out.println(em == em2);
            openSession.commit();
        } finally {
            // TODO: handle finally clause
            openSession.close();
        }
        
        
        
    }
    
}

输出:

DEBUG 01-22 10:25:29,976 ==>  Preparing: select id,last_name lastName,email,gender from tbl_employee where id = ?   (BaseJdbcLogger.java:145) 
DEBUG 01-22 10:25:30,021 ==> Parameters: 1(Integer)  (BaseJdbcLogger.java:145) 
DEBUG 01-22 10:25:30,086 <==      Total: 1  (BaseJdbcLogger.java:145) 
Employee [id=1, lastName=dema, gender=1, email=dema@qq.com, dept=null]
DEBUG 01-22 10:25:30,088 ==>  Preparing: select id,last_name lastName,email,gender from tbl_employee where id = ?   (BaseJdbcLogger.java:145) 
DEBUG 01-22 10:25:30,093 ==> Parameters: 3(Integer)  (BaseJdbcLogger.java:145) 
DEBUG 01-22 10:25:30,096 <==      Total: 1  (BaseJdbcLogger.java:145) 
Employee [id=3, lastName=小红, gender=0, email=xiaohong@qq.com, dept=null]
false

3、同一个SqlSession,但在两次查询之间执行了增删改操作

package com.gong.mybatis.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.Test;

import com.gong.mybatis.bean.Department;
import com.gong.mybatis.bean.Employee;
import com.gong.mybatis.dao.EmployeeMapper;
import com.gong.mybatis.mapper.EmployeeMapperDynamicSql;

public class TestMybatis4 {
    
    public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(is);
    }

    @Test
    public void test() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
            Employee em = mapper.getEmpById(1);
            System.out.println(em);
            
//            SqlSession openSession2 = sqlSessionFactory.openSession();
//            EmployeeMapper mapper2 = openSession2.getMapper(EmployeeMapper.class);
            mapper.deleteEmp(5);
            Employee em2 = mapper.getEmpById(1);
            System.out.println(em2);
            
            System.out.println(em == em2);
            openSession.commit();
        } finally {
            // TODO: handle finally clause
            openSession.close();
        }
        
        
        
    }
    
}

输出:

DEBUG 01-22 10:27:54,536 ==>  Preparing: select id,last_name lastName,email,gender from tbl_employee where id = ?   (BaseJdbcLogger.java:145) 
DEBUG 01-22 10:27:54,586 ==> Parameters: 1(Integer)  (BaseJdbcLogger.java:145) 
DEBUG 01-22 10:27:54,636 <==      Total: 1  (BaseJdbcLogger.java:145) 
Employee [id=1, lastName=dema, gender=1, email=dema@qq.com, dept=null]
DEBUG 01-22 10:27:54,639 ==>  Preparing: delete from tbl_employee where id=?   (BaseJdbcLogger.java:145) 
DEBUG 01-22 10:27:54,640 ==> Parameters: 5(Integer)  (BaseJdbcLogger.java:145) 
DEBUG 01-22 10:27:54,700 <==    Updates: 1  (BaseJdbcLogger.java:145) 
DEBUG 01-22 10:27:54,702 ==>  Preparing: select id,last_name lastName,email,gender from tbl_employee where id = ?   (BaseJdbcLogger.java:145) 
DEBUG 01-22 10:27:54,705 ==> Parameters: 1(Integer)  (BaseJdbcLogger.java:145) 
DEBUG 01-22 10:27:54,712 <==      Total: 1  (BaseJdbcLogger.java:145) 
Employee [id=1, lastName=dema, gender=1, email=dema@qq.com, dept=null]
false

4、同一个SqlSession,但手动清除了一级缓存

即在两次查询之间使用了:

openSession.clearCache();

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • java(一)基础知识

    绝命生
  • mybatis扩展之使用PageHelper插件进行分页(是真好用)

    绝命生
  • mybatis扩展之自定义类型处理器处理枚举类型

    绝命生
  • 利用word2vec对关键词进行聚类

    按照一般的思路,可以用新闻ID向量来表示某个关键词,这就像广告推荐系统里面用用户访问类别向量来表示用户一样,然后就可以用kmeans的方法进行聚类了。不过对于新...

    机器学习AI算法工程
  • java 对比两文件哪行被更改了

    50504501 50504501 50504502 50504502 50504503 50504503 50504504 50504504 5050...

    葫芦
  • 解决PKIX问题:unable to find valid certification path to requested target【X509TrustManager】

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。

    奋飛
  • 【原创】自己动手实现RPC服务调用框架

    Java高级架构
  • Java基础-21(02)总结字符流,IO流编码问题,实用案例必做一遍

    C:把集合中的数据存储到文本文件 package cn.itcast_02; import java.io.BufferedWriter; import jav...

    Java帮帮
  • java的poi技术读取Excel数据到MySQL

    这篇blog是介绍java中的poi技术读取Excel数据,然后保存到MySQL数据中。

    Hongten
  • java简单框架设计

         设计框架包可以作为一个工具给大家用,需要有完全不同设计思路给出来,不同于我们去做一个web服务、网站、

    杉枫

扫码关注云+社区

领取腾讯云代金券