一,MyBatis一级缓存(本地缓存)
<select id="selectStudentByIdAndName" flushCache=”true” resultType="student"> select * from student where sid=#{Sid} and s_name=#{Sname} </select>
public class MyBatisTest { public static void main( String[] args ) { SqlSession openSession = null; try { //mybatis配置文件 String resourse="mybatis-cfg.xml"; //通过 Resources 工具类将 ti -config.xm 配置文件读入 Reader InputStream inputStream=Resources.getResourceAsStream(resourse); //通过 SqlSessionFactoryBuilder 建造类使用 Reader 创建 SqlSessionFactory工厂对象 SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); //通过SqlSessionFactory工厂得到SqlSession openSession = sqlSessionFactory.openSession(); //通过反射机制来获取对应的Mapper实例 StudentMapper mapper=openSession.getMapper(StudentMapper.class); Student student1=mapper.selectStudentByIdAndName(2,"danghh"); Student student2=mapper.selectStudentByIdAndName(2,"danghh"); System.out.println(student1); openSession.commit(); } catch (IOException e) { e.printStackTrace(); }finally { //最后一定不要忘记关闭 SqlSession ,否则会因为连接没有关闭导致数据库连接数过多,造成系统崩旗 openSession.close(); } } }
[DEBUG] - Setting autocommit to false on JDBC Connection[com.mysql.jdbc.JDBC4Connection@dfd3711] [DEBUG] - ==> Preparing: select * from student where sid=? and s_name=? [DEBUG] - ==> Parameters: 2(Integer), danghh(String) [DEBUG] - <== Total: 1 Student{SID=2, Sname=‘danghh‘, Sage=22, Ssex=‘nv‘, course=null}
Student{SID=2, Sname=‘danghh‘, Sage=22, Ssex=‘nv‘, course=null}
通过结果可以看出,由于代码中查询是在一个SqlSession,且两次查询过程中没有更新信息,不会导致一级缓存失效,所以结果只进行了一次数据库查询。
那如果是在两个SqlSession中分别进行查询呢?
结果:
[DEBUG] - Opening JDBC Connection [DEBUG] - Checked out connection 234698513 from pool. [DEBUG] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@dfd3711] [DEBUG] - ==> Preparing: select * from student where sid=? and s_name=? [DEBUG] - ==> Parameters: 2(Integer), danghh(String) [DEBUG] - <== Total: 1 [DEBUG] - Opening JDBC Connection [DEBUG] - Created connection 1836797772. [DEBUG] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6d7b4f4c] [DEBUG] - ==> Preparing: select * from student where sid=? and s_name=? [DEBUG] - ==> Parameters: 2(Integer), danghh(String) [DEBUG] - <== Total: 1 Student{SID=2, Sname=‘danghh‘, Sage=22, Ssex=‘nv‘, course=null} Student{SID=2, Sname=‘danghh‘, Sage=22, Ssex=‘nv‘, course=null}
[DEBUG] - ==> Preparing: select * from student where sid=? and s_name=? [DEBUG] - ==> Parameters: 2(Integer), hjj(String) [DEBUG] - <== Total: 1 [DEBUG] - ==> Preparing: update student set S_name=?,Sage=?,Ssex=? where Sid=? [DEBUG] - ==> Parameters: hjj(String), 23(Integer), null, 2(Integer) [DEBUG] - <== Updates: 1 [DEBUG] - ==> Preparing: select * from student where sid=? and s_name=? [DEBUG] - ==> Parameters: 2(Integer), hjj(String) [DEBUG] - <== Total: 1 Student{SID=2, Sname=‘hjj‘, Sage=23, Ssex=‘null‘, course=null} Student{SID=2, Sname=‘hjj‘, Sage=23, Ssex=‘null‘, course=null}
(这个参数是二级缓存的全局开关,默认值是 true ,初始状态为启用状态,所以也可忽略此步的配置)
(由于MyBatis二级缓存和命名空间namespace是绑定的 ,即二级缓存还需要在 Mapper.xml 映射文件中配置或者在 Mapper.java 接口中配置。)
代码:
public class MyBatisTest { public static void main( String[] args ) { SqlSession openSession1 = null; SqlSession openSession2 = null; try { //mybatis配置文件 String resourse="mybatis-cfg.xml"; //通过 Resources 工具类将 ti -config.xm 配置文件读入 Reader InputStream inputStream=Resources.getResourceAsStream(resourse); //通过 SqlSessionFactoryBuilder 建造类使用 Reader 创建 SqlSessionFactory工厂对象 SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); //通过SqlSessionFactory工厂得到SqlSession1 openSession1 = sqlSessionFactory.openSession(); StudentMapper mapper1=openSession1.getMapper(StudentMapper.class); //通过SqlSessionFactory工厂得到SqlSession2 openSession2 = sqlSessionFactory.openSession(); StudentMapper mapper2=openSession2.getMapper(StudentMapper.class); //使用会话1进行查询,此次查询结果只会存储在一级缓存中 Student student1=mapper1.selectStudentByIdAndName(2,"hjj"); System.out.println(student1); //使用会话2进行查询,前面会话未关闭,数据不会被刷到二级缓存中,所以本次仍会执行sql Student student2=mapper2.selectStudentByIdAndName(2,"hjj"); System.out.println(student2); //使用会话2进行查询,由于前面已执行过该方法,所以可在一级缓存中查到 Student student3=mapper2.selectStudentByIdAndName(2,"hjj"); System.out.println(student3); openSession1.commit(); } catch (IOException e) { e.printStackTrace(); }finally { //最后一定不要忘记关闭 SqlSession ,否则会因为连接没有关闭导致数据库连接数过多,造成系统崩旗 openSession1.close(); } } }
[DEBUG] - Cache Hit Ratio [MyBatisDemo.StudentMapper]: 0.0 [DEBUG] - Opening JDBC Connection [DEBUG] - Checked out connection 234698513 from pool. [DEBUG] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@dfd3711] [DEBUG] - ==> Preparing: select * from student where sid=? and s_name=? [DEBUG] - ==> Parameters: 2(Integer), hjj(String) [DEBUG] - <== Total: 1 Student{SID=2, Sname=‘hjj‘, Sage=23, Ssex=‘null‘, course=null} [DEBUG] - Cache Hit Ratio [MyBatisDemo.StudentMapper]: 0.0 [DEBUG] - Opening JDBC Connection [DEBUG] - Created connection 1843368112. [DEBUG] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6ddf90b0] [DEBUG] - ==> Preparing: select * from student where sid=? and s_name=? [DEBUG] - ==> Parameters: 2(Integer), hjj(String) [DEBUG] - <== Total: 1 Student{SID=2, Sname=‘hjj‘, Sage=23, Ssex=‘null‘, course=null} [DEBUG] - Cache Hit Ratio [MyBatisDemo.StudentMapper]: 0.0 Student{SID=2, Sname=‘hjj‘, Sage=23, Ssex=‘null‘, course=null} [DEBUG] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@dfd3711] [DEBUG] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@dfd3711] [DEBUG] - Returned connection 234698513 to pool.
该颜色:表示会话1第一次查询的结果,由于第一次查询,一级缓存和二级缓存中都没有数据,所以Mapper命中率为0.0,且进行了数据库查询,并将结果存储到会话1一级缓存中。
该颜色:表示会话2第一次查询的结果,由于会话1没有关闭,所以会话1的一级缓存不会刷到Mapper的二级缓存中,并且是在会话2中第一次查询该方法,所以Mapper命中率为0.0,且进行了数据库查询,并将结果存储到会话2的一级缓存中。
运行结果:
[DEBUG] - Cache Hit Ratio [MyBatisDemo.StudentMapper]: 0.0 [DEBUG] - Opening JDBC Connection [DEBUG] - Checked out connection 234698513 from pool. [DEBUG] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@dfd3711] [DEBUG] - ==> Preparing: select * from student where sid=? and s_name=? [DEBUG] - ==> Parameters: 2(Integer), hjj(String) [DEBUG] - <== Total: 1 Student{SID=2, Sname=‘hjj‘, Sage=23, Ssex=‘null‘, course=null} [DEBUG] - Cache Hit Ratio [MyBatisDemo.StudentMapper]: 0.0 Student{SID=2, Sname=‘hjj‘, Sage=23, Ssex=‘null‘, course=null} [DEBUG] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@dfd3711] [DEBUG] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@dfd3711] [DEBUG] - Returned connection 234698513 to pool. [DEBUG] - Cache Hit Ratio [MyBatisDemo.StudentMapper]: 0.3333333333333333 Student{SID=2, Sname=‘hjj‘, Sage=23, Ssex=‘null‘, course=null} [DEBUG] - Cache Hit Ratio [MyBatisDemo.StudentMapper]: 0.5
原文:https://www.cnblogs.com/bwj1234562021/p/14471386.html