MyBatis缓存介绍与入门

2025-07-03 01:21:16

1、mybatis包含一个非常强大的查询缓存特性,它可以非常方便的配置和定制,缓存可以极大的提升查询效率。

MyBatis缓存介绍与入门

2、mybatis提供了两级缓存,一级缓存(本地缓存):与数据库同一个会话期间查询到的数据回放在本地缓存中。以后如果需要获取相同的数据,直接从缓存中拿,没必要再去查询数据库。@Test public void testFirsetLevelCache() throws Exception { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); try { EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class); Employee employee = employeeMapper.getEmpById(1); System.out.println(employee); Employee employee2 = employeeMapper.getEmpById(1); System.out.println(employee2); }finally { sqlSession.close(); }

MyBatis缓存介绍与入门

3、上述步骤我们查询同一个员工查询了两个,执行单元测试查看数据库打印语句是否只执行了一次。

MyBatis缓存介绍与入门

4、我们在代码中比较两次查询到的对象,比较对象的引用地址是否是一样的。package com.gwolf;import java.io.InputStream;import java.util.ArrayList;import java.util.List;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.gwolf.bean.Employee;import com.gwolf.dao.EmployeeMapper;public class AppTest { /*@Test public void test() throws Exception { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); try { //Employee employee = sqlSession.selectOne("com.gwolf.dao.EmployeeMapper.selectEmployee", 1); Employee employee = new Employee(null,"技术部","geolf2010@126.com","M"); sqlSession.insert("addEmp", employee); System.out.println(employee.getEmpId()); }finally { sqlSession.close(); } }*/ /*@Test public void test1() throws Exception { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); try { EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class); Employee employee = new Employee(); employee.setEmpId(1); employee.setEmpName("%曹%"); List<Employee> employees = employeeMapper.getEmpsByConditionChoose(employee); System.out.println(employees); }finally { sqlSession.close(); } }*/ /* @Test public void testBatchSave() throws Exception { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); try { EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class); List<Employee> emps = new ArrayList<Employee>(); emps.add(new Employee(null, "cc", "gwolf_201@126.com", "M")); emps.add(new Employee(null, "ce", "gwolf_201@126.com", "M")); employeeMapper.addEmps(emps);; }finally { sqlSession.close(); } }*/ @Test public void testFirsetLevelCache() throws Exception { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); try { EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class); Employee employee = employeeMapper.getEmpById(1); System.out.println(employee); Employee employee2 = employeeMapper.getEmpById(1); System.out.println(employee2); System.out.println(employee == employee2); }finally { sqlSession.close(); } } }

MyBatis缓存介绍与入门

5、运行此单元测试方法,查看打印的结果是否是true,这个相等更加说明了没有去重新查询数据库了。

MyBatis缓存介绍与入门

6、mybatis提供了两级缓存,一级缓存(二级缓存),下一课程我们将介绍二级缓存。

MyBatis缓存介绍与入门
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
相关推荐
  • 阅读量:48
  • 阅读量:82
  • 阅读量:74
  • 阅读量:33
  • 阅读量:45
  • 猜你喜欢