本文主要是介绍MyBatis篇】四. MyBatis高级第二章 缓存(共三章),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
MyBatis高级第二章 缓存
github源码(day56-mybatis-senior) https://github.com/1196557363/ideaMavenProject
【MyBatis篇】四. MyBatis高级第一章 #{}和${}符号的区别
项目准备
- 因为在同一个module中所以大环境不用改,参考第一章的项目准备
- 将接口com.wpj.dao.IUserDao2 改为 com.wpj.dao2.IUserDao2 已示区分知识点
- mapper也改为mapper2 接口对应映射文件的namespace记得改。
- MyBatis的mapper映射记得加。
缓存
1.1 一级缓存sqlSession
1.1.1 接口
package com.wpj.dao2;import com.wpj.bean.*;
import org.apache.ibatis.annotations.*;import java.util.*;
/*** ClassName: IUserDao2* Description:** @author JieKaMi* @version 1.0* @date: 2020\1\7 0007 19:02* @since JDK 1.8*/
public interface IUserDao2 {User getUserById(Integer id);
}
1.1.2 接口对应mapper
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.wpj.dao2.IUserDao2"><select id="getUserById" resultType="user">select * from user where id = #{id}</select></mapper>
1.1.3 test
import com.wpj.bean.*;
import com.wpj.dao.*;
import com.wpj.dao2.IUserDao2;
import org.apache.ibatis.io.*;
import org.apache.ibatis.session.*;
import org.junit.*;import java.io.*;
import java.util.*;/*** ClassName: MyBatisTest* Description:** @author JieKaMi* @version 1.0* @date: 2020\1\7 0007 19:19* @since JDK 1.8*/
public class MyBatisTest2 {/*** 测试一级缓存*/@Testpublic void testSqlSessionCache(){String config = "MyBatis.xml";InputStream is = null;try {is = Resources.getResourceAsStream(config);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);// 可以和数据库建立连接SqlSession sqlSession = sqlSessionFactory.openSession();IUserDao2 userDao2 = sqlSession.getMapper(IUserDao2.class);User user = userDao2.getUserById(1);System.out.println(user);System.out.println("-------sqlSession分割线--------");User user2 = userDao2.getUserById(2);System.out.println(user2);sqlSession.close();} catch (IOException e) {e.printStackTrace();} finally {if(is!=null) {try {is.close();} catch (IOException e) {e.printStackTrace();}}}}
}
1.1.4 结果
Preparing: select * from user where id = ?
Parameters: 1(Integer)
Total: 1
User{id=1, name=‘wpj’, pwd=‘123’, age=22}
-------sqlSession分割线--------
User{id=1, name=‘wpj’, pwd=‘123’, age=22}
发了1条sql语句,一级缓存已经开启。(默认开启) 不能在sqlSession.close()之后去查询。
1.2 二级缓存
1.2.1 test
/*** 测试二级缓存*/@Testpublic void testSqlSessionFactoryCache(){String config = "MyBatis.xml";InputStream is = null;try {is = Resources.getResourceAsStream(config);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);SqlSession sqlSession = sqlSessionFactory.openSession();IUserDao2 userDao2 = sqlSession.getMapper(IUserDao2.class);User user = userDao2.getUserById(1);System.out.println(user);System.out.println("-------sqlSessionFactory分割线--------");SqlSession sqlSession2 = sqlSessionFactory.openSession();IUserDao2 userDao22 = sqlSession2.getMapper(IUserDao2.class);User user2 = userDao22.getUserById(1);System.out.println(user2);sqlSession.close();sqlSession2.close();} catch (IOException e) {e.printStackTrace();} finally {if(is!=null) {try {is.close();} catch (IOException e) {e.printStackTrace();}}}}
1.2.2 结果
Opening JDBC Connection
Created connection 1920467934.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@727803de]
Preparing: select * from user where id = ?
Parameters: 1(Integer)
Total: 1
User{id=1, name=‘wpj’, pwd=‘123’, age=22}
-------sqlSessionFactory分割线--------
Opening JDBC Connection
Created connection 1514160588.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@5a4041cc]
Preparing: select * from user where id = ?
Parameters: 1(Integer)
Total: 1
User{id=1, name=‘wpj’, pwd=‘123’, age=22}
连接了2次JDBC Connection 很明显没有二级缓存 (默认关闭)
1.3 开启二级缓存
1.3.1 接口对应mapper文件中添加<cache />
<!-- 开启二级缓存 --><cache />
1.3.2 User实现序列化接口
public class User implements Serializable{private Integer id;private String name;private String pwd;private Integer age;...
}
1.3.3 sqlSession要提交事务
/*** 测试二级缓存*/@Testpublic void testSqlSessionFactoryCache(){String config = "MyBatis.xml";InputStream is = null;try {is = Resources.getResourceAsStream(config);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);SqlSession sqlSession = sqlSessionFactory.openSession();IUserDao2 userDao2 = sqlSession.getMapper(IUserDao2.class);User user = userDao2.getUserById(1);sqlSession.commit(); // 开启二级缓存需提交事务System.out.println(user);System.out.println("-------sqlSessionFactory分割线--------");SqlSession sqlSession2 = sqlSessionFactory.openSession();IUserDao2 userDao22 = sqlSession2.getMapper(IUserDao2.class);User user2 = userDao22.getUserById(1);System.out.println(user2);sqlSession.close();sqlSession2.close();} catch (IOException e) {e.printStackTrace();} finally {if(is!=null) {try {is.close();} catch (IOException e) {e.printStackTrace();}}}}
1.3.4 结果
Opening JDBC Connection
Created connection 282432134.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@10d59286]
Preparing: select * from user where id = ?
Parameters: 1(Integer)
Total: 1
User{id=1, name=‘wpj’, pwd=‘123’, age=22}
-------sqlSessionFactory分割线--------
2020-01-07 21:27:48 DEBUG com.wpj.dao2.IUserDao2:62 - Cache Hit Ratio [com.wpj.dao2.IUserDao2]: 0.5
User{id=1, name=‘wpj’, pwd=‘123’, age=22}
只连接了1次JDBC Connection,并且第二次是从cache中取得 开启了二级缓存。
1.2.2 二级缓存的其他配置
<!--useCachetrue:默认值false:可以设置这条sql语句的结果不缓存,只是对二级缓存才有效
-->
<select id="getUserById" resultType="user" useCache="false">select * from user where id = #{id}
</select>
<!-- flushCache二级缓存默认会在insert,update,detele操作后刷新缓存,通过配置false达到不刷新。-->
<!--size:设置缓存中最大能放1024个对象readOnly:设置放在缓存中的数据是否是只读的(一般都是只读的)eviction:设置缓存的清空策略LRU:最少使用原则,移除最长时间不使用的对象 FIFO:先进先出原则,按照对象进入缓存顺序进行回收flushInterval:缓存刷新时间间隔
-->
<cache size="1024" readOnly="true" eviction="LRU" flushInterval="100000"/>
说明
这篇关于MyBatis篇】四. MyBatis高级第二章 缓存(共三章)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!