MyBatis缓存介绍与入门

 时间:2026-02-16 01:11:10

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缓存介绍与入门

  • mybatis的使用
  • MyBatis环境配置及入门
  • mybatis传参类型
  • mybatis的动态注解
  • mybatis如何分页查询
  • 热门搜索
    赴美旅游签证有效期 大连市旅游景点 即墨旅游景点大全 山东旅游资讯网 金华旅游团 敦煌旅游路线 旅游房地产 国内的旅游胜地 浙江省中青国际旅游有限公司 去厦门旅游要带什么