Test Case:
<!--spring 相关包 集成mybatis-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.0.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-jcl</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!--spring 相关包 集成mybatis-->
spring.xml in the resource directory
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:property-placeholder location="classpath:db.properties" file-encoding="utf-8"/>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- don't forget the DataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<constructor-arg name="url" value="${url}"/>
<constructor-arg name="username" value="${username}"/>
<constructor-arg name="password" value="${password}"/>
<property name="driverClassName" value="${driver}"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.pingfan.dao"/>
</bean>
</beans>
db.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/bms_test?useUnicode=true&characterEncoding=utf-8
username=root
password=3306
test:
@Test
public void SpringMybatis(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
UserDao userdao = context.getBean(UserDao.class);
UserEntity user1 = userdao.findUserById(20l);
UserEntity user2 = userdao.findUserById(20l);
System.out.println(user1);
System.out.println(user2);
System.out.println(user1==user2);
}
result:
UserEntity(id=20, name=zhengziye,.... createdAt=null, updatedAt=null)
UserEntity(id=20, name=zhengziye, .....createdAt=null, updatedAt=null)
false
Break point debugging:
Conclusion: It is found that their two queries are not called by the same executor, which means that they are not the same session, and the executor and session are one-to-one, and the first-level cache only works in the same session , so the first-level cache is invalid