一级缓存:与数据库同一次会话期间查询到的数据会放在本地缓存中,以后如果需要获取相同的数据,直接从缓存中拿,没必要再去查询数据库。
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();
来源:https://www.cnblogs.com/xiximayou/p/12228034.html