Mybatis中的一级缓存和二级缓存(本博文只是针对一级缓存说明)
概述
ORM框架一般都会有缓存机制,做为其中一员的Mybatis也存在缓存。功能是用以提升查询的效率和服务给数据库带来压力。同样的Mybatis也存在有一级缓存和二级缓存,并且预留了集成第三方缓存的接口类:【cache】。
缓存角色
【一级缓存】
- MyBatis默认支持一级缓存。在没有任何配置情况下,默认开启一级缓存,MyBatis 的一级缓存是在会话(SqlSession)层面进行缓存的;
- 如果同样SqlSession对象查询相同数据,则只会查询一次。因在第一次查询时向数据库发送SQL语句,并将查询结果放入到SQLSESSION中(作为缓存存在)。后续在此做相同对象查询时,则直接从缓存中查询该对象即可(省略了访问数据库操作,提高查询效率);
- Mybatis开启一个数据库会话时,会创建一个新的SqlSession对象表示一次数据库会话,SqlSession对象中会有一个新的Executor对象。Executor对象持有一个新的PerpetualCache对象;会话结束时,SqllSession对象及其内部的 Executor 对象还有PerpetualCache对象都会一同释放掉;
- 如SqlSession调用close()方法,会释放掉一级缓存PerpetualCache对象,一级缓存将不可用;
- 如SqlSession调用clearCache()方法,会清空PerpetualCache对象中数据,但该对象仍可使用;
- SqlSession中insert、update及delete,都会清空PerpetualCache对象的数据,但该对象仍可继续使用。
一级缓存执行流程
MyBatis回话流程:当客户端开启一次数据回话SqlSession对象中创建一个本地缓存(local cache)。执行查询前尝试从本地缓存中查找是否存在,如果存在本地缓存中,就直接从缓存中取出,然后返回给客户端。否则,从数据库读取数据,将查询结果存入缓存并返回给用户。
验证(下面通过代码进行实际验证):
1、pom文件
2、数据库配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis01"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/cache/mapper/personMapper.xml"/> </mappers> </configuration>
3、实体类
public class Person { /* 人员ID */ private int id; /* 人员名称 */ private String name; /* 人员年龄 */ private int age; /* 人员性别 */ private Boolean sex; public Person() { } public Person(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public Person(int id, String name, int age, Boolean sex) { this.id = id; this.name = name; this.age = age; this.sex = sex; } public Boolean getSex() { return sex; } public void setSex(Boolean sex) { this.sex = sex; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
4、接口类
package com.cache.mapper; import com.cache.bean.Person; import java.util.List; // 操作mybatis接口 public interface PersonMapper { /* 根据ID查询人员信息 */ Person queryPersonById(int id); /* 根据ID修改人员信息 */ void updateStudentById(Person person); }
5、接口对应mapper类
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.cache.mapper.PersonMapper"> <select id="queryPersonById" parameterType="int" resultType="com.cache.bean.Person"> select id,name,age from t_person where id = #{id} </select> <update id="updateStudentById" parameterType="com.cache.bean.Person"> UPDATE t_person SET NAME =#{name},age = #{age} where id =#{id} </update> </mapper>
6、数据库表结构和数据
7、测试类
public class test { /** * 演示一级缓存:根据ID查询人的信息 */ @Test public void test01() throws Exception{ Reader reader = Resources.getResourceAsReader("mybatis-01.xml"); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); PersonMapper personMapper = session.getMapper(PersonMapper.class); /* 默认使用一级缓存演示 */ Person person = personMapper.queryPersonById(1001); System.out.println("初次查询结果为:"+person); Person person01 = personMapper.queryPersonById(1001); System.out.println("第二次查询结果为:"+person); session.close(); }
执行结果:
"C:\Program Files\Java\jdk1.8.0_25\bin\java" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\lib\idea_rt.jar=28708:C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\lib\idea_rt.jar;C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\plugins\junit\lib\junit-rt.jar;C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\plugins\junit\lib\junit5-rt.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\platform\junit-platform-launcher\1.5.2\junit-platform-launcher-1.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\apiguardian\apiguardian-api\1.1.0\apiguardian-api-1.1.0.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\platform\junit-platform-engine\1.5.2\junit-platform-engine-1.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\platform\junit-platform-commons\1.5.2\junit-platform-commons-1.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\jupiter\junit-jupiter-engine\5.5.2\junit-jupiter-engine-5.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\jupiter\junit-jupiter-api\5.5.2\junit-jupiter-api-5.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\vintage\junit-vintage-engine\5.5.2\junit-vintage-engine-5.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\junit\junit\4.12\junit-4.12.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\rt.jar;D:\ideaworkspace\ProjectStudy\mybatis-cache-03\target\classes;D:\download\lib\mavenTollTransfer\mic-repository\org\hamcrest\hamcrest-core\2.1\hamcrest-core-2.1.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\hamcrest\hamcrest\2.1\hamcrest-2.1.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\mybatis\mybatis\3.4.5\mybatis-3.4.5.jar;D:\download\lib\mavenTollTransfer\mic-repository\mysql\mysql-connector-java\5.1.44\mysql-connector-java-5.1.44.jar;D:\download\lib\mavenTollTransfer\mic-repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\slf4j\slf4j-log4j12\1.7.12\slf4j-log4j12-1.7.12.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\download\lib\mavenTollTransfer\mic-repository\cglib\cglib\3.3.0\cglib-3.3.0.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\ow2\asm\asm\7.1\asm-7.1.jar;D:\download\lib\mavenTollTransfer\mic-repository\net\logstash\logback\logstash-logback-encoder\5.3\logstash-logback-encoder-5.3.jar;D:\download\lib\mavenTollTransfer\mic-repository\com\fasterxml\jackson\core\jackson-databind\2.10.3\jackson-databind-2.10.3.jar;D:\download\lib\mavenTollTransfer\mic-repository\com\fasterxml\jackson\core\jackson-annotations\2.10.3\jackson-annotations-2.10.3.jar;D:\download\lib\mavenTollTransfer\mic-repository\com\fasterxml\jackson\core\jackson-core\2.10.3\jackson-core-2.10.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit5 com.cache.test.test,test01 [lsjSso]2021-10-20 12:55:30,892-org.apache.ibatis.logging.LogFactory-0 [main]DEBUGorg.apache.ibatis.logging.LogFactory-Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter. [lsjSso]2021-10-20 12:55:31,036-org.apache.ibatis.datasource.pooled.PooledDataSource-144 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-PooledDataSource forcefully closed/removed all connections. [lsjSso]2021-10-20 12:55:31,037-org.apache.ibatis.datasource.pooled.PooledDataSource-145 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-PooledDataSource forcefully closed/removed all connections. [lsjSso]2021-10-20 12:55:31,037-org.apache.ibatis.datasource.pooled.PooledDataSource-145 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-PooledDataSource forcefully closed/removed all connections. [lsjSso]2021-10-20 12:55:31,037-org.apache.ibatis.datasource.pooled.PooledDataSource-145 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-PooledDataSource forcefully closed/removed all connections. [lsjSso]2021-10-20 12:55:31,137-org.apache.ibatis.transaction.jdbc.JdbcTransaction-245 [main]DEBUGorg.apache.ibatis.transaction.jdbc.JdbcTransaction-Opening JDBC Connection [lsjSso]2021-10-20 12:55:31,414-org.apache.ibatis.datasource.pooled.PooledDataSource-522 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-Created connection 52908367. [lsjSso]2021-10-20 12:55:31,415-org.apache.ibatis.transaction.jdbc.JdbcTransaction-523 [main]DEBUGorg.apache.ibatis.transaction.jdbc.JdbcTransaction-Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@327514f] [lsjSso]2021-10-20 12:55:31,418-com.cache.mapper.PersonMapper.queryPersonById-526 [main]DEBUGcom.cache.mapper.PersonMapper.queryPersonById-==> Preparing: select id,name,age from t_person where id = ? [lsjSso]2021-10-20 12:55:31,459-com.cache.mapper.PersonMapper.queryPersonById-567 [main]DEBUGcom.cache.mapper.PersonMapper.queryPersonById-==> Parameters: 1001(Integer) [lsjSso]2021-10-20 12:55:31,500-com.cache.mapper.PersonMapper.queryPersonById-608 [main]DEBUGcom.cache.mapper.PersonMapper.queryPersonById-<== Total: 1 初次查询结果为:Person{id=1001, name='zhangsan', age=27} 第二次查询结果为:Person{id=1001, name='zhangsan', age=27} [lsjSso]2021-10-20 12:55:31,501-org.apache.ibatis.transaction.jdbc.JdbcTransaction-609 [main]DEBUGorg.apache.ibatis.transaction.jdbc.JdbcTransaction-Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@327514f] [lsjSso]2021-10-20 12:55:31,502-org.apache.ibatis.transaction.jdbc.JdbcTransaction-610 [main]DEBUGorg.apache.ibatis.transaction.jdbc.JdbcTransaction-Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@327514f] [lsjSso]2021-10-20 12:55:31,502-org.apache.ibatis.datasource.pooled.PooledDataSource-610 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-Returned connection 52908367 to pool. Process finished with exit code 0
分析:
分析一:查询两次结果只进行一次查询
分析二:查询了两次执行结果
验证执行,commit操作后缓存失效:
第一:测试代码修改update操作后再查询两次,其他保持不变:
A、修改测试代码为:
@Test public void test01() throws Exception{ Reader reader = Resources.getResourceAsReader("mybatis-01.xml"); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); PersonMapper personMapper = session.getMapper(PersonMapper.class); /* 默认使用一级缓存演示 Person person = personMapper.queryPersonById(1001); System.out.println("初次查询结果为:"+person); Person person01 = personMapper.queryPersonById(1001); System.out.println("第二次查询结果为:"+person); session.close(); */ /* 清除缓存演示 */ Person personFirst = personMapper.queryPersonById(1001); System.out.println("update执行前,操作第一次查询结果为:"+personFirst); Person personSecond = personMapper.queryPersonById(1001); System.out.println("update执行前,操作第二次查询结果为:"+personSecond); Person personEntity = new Person(1001,"HuanCun",26); personMapper.updateStudentById(personEntity); System.out.println("执行update操作,清除一级缓存!"); session.commit(); System.out.println("update执行后commit提交成功!"); Person personEarCache = personMapper.queryPersonById(1001); System.out.println("update执行后,初次查询结果为:"+personEarCache); Person person01 = personMapper.queryPersonById(1001); System.out.println("update执行后,第二次查询结果为:"+person01); session.close(); }
B、验证结果
"C:\Program Files\Java\jdk1.8.0_25\bin\java" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\lib\idea_rt.jar=13417:C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\lib\idea_rt.jar;C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\plugins\junit\lib\junit-rt.jar;C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\plugins\junit\lib\junit5-rt.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\platform\junit-platform-launcher\1.5.2\junit-platform-launcher-1.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\apiguardian\apiguardian-api\1.1.0\apiguardian-api-1.1.0.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\platform\junit-platform-engine\1.5.2\junit-platform-engine-1.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\platform\junit-platform-commons\1.5.2\junit-platform-commons-1.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\jupiter\junit-jupiter-engine\5.5.2\junit-jupiter-engine-5.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\jupiter\junit-jupiter-api\5.5.2\junit-jupiter-api-5.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\vintage\junit-vintage-engine\5.5.2\junit-vintage-engine-5.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\junit\junit\4.12\junit-4.12.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\rt.jar;D:\ideaworkspace\ProjectStudy\mybatis-cache-03\target\classes;D:\download\lib\mavenTollTransfer\mic-repository\org\hamcrest\hamcrest-core\2.1\hamcrest-core-2.1.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\hamcrest\hamcrest\2.1\hamcrest-2.1.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\mybatis\mybatis\3.4.5\mybatis-3.4.5.jar;D:\download\lib\mavenTollTransfer\mic-repository\mysql\mysql-connector-java\5.1.44\mysql-connector-java-5.1.44.jar;D:\download\lib\mavenTollTransfer\mic-repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\slf4j\slf4j-log4j12\1.7.12\slf4j-log4j12-1.7.12.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\download\lib\mavenTollTransfer\mic-repository\cglib\cglib\3.3.0\cglib-3.3.0.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\ow2\asm\asm\7.1\asm-7.1.jar;D:\download\lib\mavenTollTransfer\mic-repository\net\logstash\logback\logstash-logback-encoder\5.3\logstash-logback-encoder-5.3.jar;D:\download\lib\mavenTollTransfer\mic-repository\com\fasterxml\jackson\core\jackson-databind\2.10.3\jackson-databind-2.10.3.jar;D:\download\lib\mavenTollTransfer\mic-repository\com\fasterxml\jackson\core\jackson-annotations\2.10.3\jackson-annotations-2.10.3.jar;D:\download\lib\mavenTollTransfer\mic-repository\com\fasterxml\jackson\core\jackson-core\2.10.3\jackson-core-2.10.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit5 com.cache.test.test,test01 [lsjSso]2021-10-20 13:14:00,298-org.apache.ibatis.logging.LogFactory-0 [main]DEBUGorg.apache.ibatis.logging.LogFactory-Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter. [lsjSso]2021-10-20 13:14:00,459-org.apache.ibatis.datasource.pooled.PooledDataSource-161 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-PooledDataSource forcefully closed/removed all connections. [lsjSso]2021-10-20 13:14:00,459-org.apache.ibatis.datasource.pooled.PooledDataSource-161 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-PooledDataSource forcefully closed/removed all connections. [lsjSso]2021-10-20 13:14:00,459-org.apache.ibatis.datasource.pooled.PooledDataSource-161 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-PooledDataSource forcefully closed/removed all connections. [lsjSso]2021-10-20 13:14:00,460-org.apache.ibatis.datasource.pooled.PooledDataSource-162 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-PooledDataSource forcefully closed/removed all connections. [lsjSso]2021-10-20 13:14:00,554-org.apache.ibatis.transaction.jdbc.JdbcTransaction-256 [main]DEBUGorg.apache.ibatis.transaction.jdbc.JdbcTransaction-Opening JDBC Connection [lsjSso]2021-10-20 13:14:00,832-org.apache.ibatis.datasource.pooled.PooledDataSource-534 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-Created connection 52908367. [lsjSso]2021-10-20 13:14:00,832-org.apache.ibatis.transaction.jdbc.JdbcTransaction-534 [main]DEBUGorg.apache.ibatis.transaction.jdbc.JdbcTransaction-Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@327514f] [lsjSso]2021-10-20 13:14:00,834-com.cache.mapper.PersonMapper.queryPersonById-536 [main]DEBUGcom.cache.mapper.PersonMapper.queryPersonById-==> Preparing: select id,name,age from t_person where id = ? [lsjSso]2021-10-20 13:14:00,869-com.cache.mapper.PersonMapper.queryPersonById-571 [main]DEBUGcom.cache.mapper.PersonMapper.queryPersonById-==> Parameters: 1001(Integer) [lsjSso]2021-10-20 13:14:00,891-com.cache.mapper.PersonMapper.queryPersonById-593 [main]DEBUGcom.cache.mapper.PersonMapper.queryPersonById-<== Total: 1 update执行前,操作第一次查询结果为:Person{id=1001, name='HuanCun', age=26} update执行前,操作第二次查询结果为:Person{id=1001, name='HuanCun', age=26} [lsjSso]2021-10-20 13:14:00,892-com.cache.mapper.PersonMapper.updateStudentById-594 [main]DEBUGcom.cache.mapper.PersonMapper.updateStudentById-==> Preparing: UPDATE t_person SET NAME =?,age = ? where id =? [lsjSso]2021-10-20 13:14:00,892-com.cache.mapper.PersonMapper.updateStudentById-594 [main]DEBUGcom.cache.mapper.PersonMapper.updateStudentById-==> Parameters: HuanCun(String), 26(Integer), 1001(Integer) [lsjSso]2021-10-20 13:14:00,950-com.cache.mapper.PersonMapper.updateStudentById-652 [main]DEBUGcom.cache.mapper.PersonMapper.updateStudentById-<== Updates: 1 执行update操作,清除一级缓存! [lsjSso]2021-10-20 13:14:00,951-org.apache.ibatis.transaction.jdbc.JdbcTransaction-653 [main]DEBUGorg.apache.ibatis.transaction.jdbc.JdbcTransaction-Committing JDBC Connection [com.mysql.jdbc.JDBC4Connection@327514f] update执行后commit提交成功! [lsjSso]2021-10-20 13:14:00,953-com.cache.mapper.PersonMapper.queryPersonById-655 [main]DEBUGcom.cache.mapper.PersonMapper.queryPersonById-==> Preparing: select id,name,age from t_person where id = ? [lsjSso]2021-10-20 13:14:00,953-com.cache.mapper.PersonMapper.queryPersonById-655 [main]DEBUGcom.cache.mapper.PersonMapper.queryPersonById-==> Parameters: 1001(Integer) [lsjSso]2021-10-20 13:14:00,954-com.cache.mapper.PersonMapper.queryPersonById-656 [main]DEBUGcom.cache.mapper.PersonMapper.queryPersonById-<== Total: 1 update执行后,初次查询结果为:Person{id=1001, name='HuanCun', age=26} update执行后,第二次查询结果为:Person{id=1001, name='HuanCun', age=26} [lsjSso]2021-10-20 13:14:00,954-org.apache.ibatis.transaction.jdbc.JdbcTransaction-656 [main]DEBUGorg.apache.ibatis.transaction.jdbc.JdbcTransaction-Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@327514f] [lsjSso]2021-10-20 13:14:00,955-org.apache.ibatis.transaction.jdbc.JdbcTransaction-657 [main]DEBUGorg.apache.ibatis.transaction.jdbc.JdbcTransaction-Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@327514f] [lsjSso]2021-10-20 13:14:00,955-org.apache.ibatis.datasource.pooled.PooledDataSource-657 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-Returned connection 52908367 to pool. Process finished with exit code 0