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

Mybatis 一级缓存 (20)的更多相关文章

  1. MyBatis一级缓存引起的无穷递归

    MyBatis一级缓存引起的无穷递归 引言: 最近在项目中参与了一个领取优惠劵的活动,当多个用户领取同一张优惠劵的时候,使用了数据库锁控制并发,起初的设想是:如果多个人同时领一张劵,第一个到达的人领取 ...

  2. MyBatis 一级缓存与二级缓存

    MyBatis一级缓存 MyBatis一级缓存默认开启,一级缓存为Session级别的缓存,在执行以下操作时一级缓存会清空 1.执行session.clearCache(); 2.执行CUD操作 3. ...

  3. mybatis一级缓存详解

    mybatis缓存分为一级缓存,二级缓存和自定义缓存.本文重点讲解一级缓存 一:前言 在介绍缓存之前,先了解下mybatis的几个核心概念: * SqlSession:代表和数据库的一次会话,向用户提 ...

  4. 0065 MyBatis一级缓存与二级缓存

    数据库中数据虽多,但访问频率却不同,有的数据1s内就会有多次访问,而有些数据几天都没人查询,这时候就可以将访问频率高的数据放到缓存中,就不用去数据库里取了,提高了效率还节约了数据库资源 MyBatis ...

  5. MyBatis 一级缓存避坑

    MyBatis 一级缓存(MyBaits 称其为 Local Cache)无法关闭,但是有两种级别可选: package org.apache.ibatis.session; /** * @autho ...

  6. 关于mybatis 一级缓存引发的问题

    场景: 由于在一个方法中存在多个不同业务操作 private void insertOrUpdateField(CompanyReport entity) { //计算并数据 calcReportDa ...

  7. MyBatis一级缓存(转载)

    <深入理解mybatis原理> MyBatis的一级缓存实现详解 及使用注意事项 http://demo.netfoucs.com/luanlouis/article/details/41 ...

  8. MyBatis 一级缓存、二级缓存全详解(一)

    目录 MyBatis 一级缓存.二级缓存全详解(一) 什么是缓存 什么是MyBatis中的缓存 MyBatis 中的一级缓存 初探一级缓存 探究一级缓存是如何失效的 一级缓存原理探究 还有其他要补充的 ...

  9. Mybatis一级缓存和二级缓存总结

    1:mybatis一级缓存:级别是session级别的,如果是同一个线程,同一个session,同一个查询条件,则只会查询数据库一次 2:mybatis二级缓存:级别是sessionfactory级别 ...

  10. Mybatis一级缓存、二级缓存详讲

    Mybatis 一级缓存.二级缓存 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 查询缓存 首先,我们先看一下这个标题“查询缓存”,那就说明跟增.删.改是没有任何关联的,只有在查询 ...

随机推荐

  1. Ansible部署及配置介绍

    原文转自:https://www.cnblogs.com/itzgr/p/10233932.html作者:木二 目录 一 Ansible的安装部署 1.1 PIP方式 1.2 YUM方式 二 Ansi ...

  2. AQS实现原理

    AQS实现原理 AQS中维护了一个volatile int state(共享资源)和一个CLH队列.当state=1时代表当前对象锁已经被占用,其他线程来加锁时则会失败,失败的线程被放入一个FIFO的 ...

  3. abp element 显示分页

    App.vue添加组件 <template> <div id="app"> <dataTable></dataTable> < ...

  4. Redis核心原理与实践--列表实现原理之quicklist结构

    在上一篇文章<Redis列表实现原理之ziplist结构>,我们分析了ziplist结构如何使用一块完整的内存存储列表数据. 同时也提出了一个问题:如果链表很长,ziplist中每次插入或 ...

  5. 论文解读(PCL)《Prototypical Contrastive Learning of Unsupervised Representations》

    论文标题:Prototypical Contrastive Learning of Unsupervised Representations 论文方向:图像领域,提出原型对比学习,效果远超MoCo和S ...

  6. UI自动化测试:App的Webview页面元素左滑

      一.前言 在做App自动化测试时,我们会遇到如上图所示的列表数据页面左滑删除场景,一般可以通过location.rect方法获取对应列表的元素坐标,然后使用TouchAction或者swipe滑动 ...

  7. 宝塔面板使用PM2命令提示Command Not Found解决方案

    1.查看node版本 进入/www/server/nvm/versions/node 查看node版本 2.复制以下代码 以node版本v12.18.1举例 PATH=$PATH:/www/serve ...

  8. Java中的三大特性:封装、继承、多态

    封装: 概念:封装可以被认为是一个保护屏障,防止该类的代码和数据被其他类随意访问,适当的封装可以让代码更容易理解与维护,也加强了代码的安全性. 原则:将属性隐藏起来,若需要访问某个属性,提供公共方法对 ...

  9. P1088 [NOIP2004 普及组] 火星人

    题目描述 人类终于登上了火星的土地并且见到了神秘的火星人.人类和火星人都无法理解对方的语言,但是我们的科学家发明了一种用数字交流的方法.这种交流方法是这样的,首先,火星人把一个非常大的数字告诉人类科学 ...

  10. Windows系统如何找到占用端口的进程并杀掉

    1.先建立用户环境变量:C\WINDOWS/system32 2.输入:cmd,打开命令控制台,然后输入ipconfig 3.再输入:netstat -ano(可以找到所有的进程连接端口及对应PID) ...