验证日志

springboot mybatis plus二级缓存开启及缓存命中验证完整代码案例_mybatis

D:\Develop\Java8\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.1.4\lib\idea_rt.jar=57012:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.1.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\Raycus\.m2\repository\org\junit\platform\junit-platform-launcher\1.8.2\junit-platform-launcher-1.8.2.jar;C:\Users\Raycus\.m2\repository\org\junit\platform\junit-platform-engine\1.8.2\junit-platform-engine-1.8.2.jar;C:\Users\Raycus\.m2\repository\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;C:\Users\Raycus\.m2\repository\org\junit\platform\junit-platform-commons\1.8.2\junit-platform-commons-1.8.2.jar;C:\Users\Raycus\.m2\repository\org\apiguardian\apiguardian-api\1.1.2\apiguardian-api-1.1.2.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.1.4\lib\idea_rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.1.4\plugins\junit\lib\junit5-rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.1.4\plugins\junit\lib\junit-rt.jar;D:\Develop\Java8\jre\lib\charsets.jar;D:\Develop\Java8\jre\lib\deploy.jar;D:\Develop\Java8\jre\lib\ext\access-bridge-64.jar;D:\Develop\Java8\jre\lib\ext\cldrdata.jar;D:\Develop\Java8\jre\lib\ext\dnsns.jar;D:\Develop\Java8\jre\lib\ext\jaccess.jar;D:\Develop\Java8\jre\lib\ext\jfxrt.jar;D:\Develop\Java8\jre\lib\ext\localedata.jar;D:\Develop\Java8\jre\lib\ext\nashorn.jar;D:\Develop\Java8\jre\lib\ext\sunec.jar;D:\Develop\Java8\jre\lib\ext\sunjce_provider.jar;D:\Develop\Java8\jre\lib\ext\sunmscapi.jar;D:\Develop\Java8\jre\lib\ext\sunpkcs11.jar;D:\Develop\Java8\jre\lib\ext\zipfs.jar;D:\Develop\Java8\jre\lib\javaws.jar;D:\Develop\Java8\jre\lib\jce.jar;D:\Develop\Java8\jre\lib\jfr.jar;D:\Develop\Java8\jre\lib\jfxswt.jar;D:\Develop\Java8\jre\lib\jsse.jar;D:\Develop\Java8\jre\lib\management-agent.jar;D:\Develop\Java8\jre\lib\plugin.jar;D:\Develop\Java8\jre\lib\resources.jar;D:\Develop\Java8\jre\lib\rt.jar;D:\Workspace\Java\mybatis-plus-cache-example\mybatis-plus-cache-example-start\target\test-classes;D:\Workspace\Java\mybatis-plus-cache-example\mybatis-plus-cache-example-start\target\classes;D:\Develop\Repository\com\baomidou\mybatis-plus-boot-starter\3.5.3\mybatis-plus-boot-starter-3.5.3.jar;D:\Develop\Repository\com\baomidou\mybatis-plus\3.5.3\mybatis-plus-3.5.3.jar;D:\Develop\Repository\com\baomidou\mybatis-plus-extension\3.5.3\mybatis-plus-extension-3.5.3.jar;D:\Develop\Repository\com\baomidou\mybatis-plus-core\3.5.3\mybatis-plus-core-3.5.3.jar;D:\Develop\Repository\com\baomidou\mybatis-plus-annotation\3.5.3\mybatis-plus-annotation-3.5.3.jar;D:\Develop\Repository\com\github\jsqlparser\jsqlparser\4.4\jsqlparser-4.4.jar;D:\Develop\Repository\org\springframework\boot\spring-boot-autoconfigure\2.6.13\spring-boot-autoconfigure-2.6.13.jar;D:\Develop\Repository\org\springframework\boot\spring-boot\2.6.13\spring-boot-2.6.13.jar;D:\Develop\Repository\org\springframework\boot\spring-boot-starter-jdbc\2.6.13\spring-boot-starter-jdbc-2.6.13.jar;D:\Develop\Repository\com\zaxxer\HikariCP\4.0.3\HikariCP-4.0.3.jar;D:\Develop\Repository\org\springframework\spring-jdbc\5.3.23\spring-jdbc-5.3.23.jar;D:\Develop\Repository\org\springframework\spring-tx\5.3.23\spring-tx-5.3.23.jar;D:\Develop\Repository\org\springframework\boot\spring-boot-starter-web\2.6.13\spring-boot-starter-web-2.6.13.jar;D:\Develop\Repository\org\springframework\boot\spring-boot-starter\2.6.13\spring-boot-starter-2.6.13.jar;D:\Develop\Repository\org\springframework\boot\spring-boot-starter-logging\2.6.13\spring-boot-starter-logging-2.6.13.jar;D:\Develop\Repository\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;D:\Develop\Repository\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;D:\Develop\Repository\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;D:\Develop\Repository\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;D:\Develop\Repository\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;D:\Develop\Repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\Develop\Repository\org\yaml\snakeyaml\1.29\snakeyaml-1.29.jar;D:\Develop\Repository\org\springframework\boot\spring-boot-starter-json\2.6.13\spring-boot-starter-json-2.6.13.jar;D:\Develop\Repository\com\fasterxml\jackson\core\jackson-databind\2.13.4.2\jackson-databind-2.13.4.2.jar;D:\Develop\Repository\com\fasterxml\jackson\core\jackson-annotations\2.13.4\jackson-annotations-2.13.4.jar;D:\Develop\Repository\com\fasterxml\jackson\core\jackson-core\2.13.4\jackson-core-2.13.4.jar;D:\Develop\Repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.4\jackson-datatype-jdk8-2.13.4.jar;D:\Develop\Repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.4\jackson-datatype-jsr310-2.13.4.jar;D:\Develop\Repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.4\jackson-module-parameter-names-2.13.4.jar;D:\Develop\Repository\org\springframework\boot\spring-boot-starter-tomcat\2.6.13\spring-boot-starter-tomcat-2.6.13.jar;D:\Develop\Repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.68\tomcat-embed-core-9.0.68.jar;D:\Develop\Repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.68\tomcat-embed-el-9.0.68.jar;D:\Develop\Repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.68\tomcat-embed-websocket-9.0.68.jar;D:\Develop\Repository\org\springframework\spring-web\5.3.23\spring-web-5.3.23.jar;D:\Develop\Repository\org\springframework\spring-beans\5.3.23\spring-beans-5.3.23.jar;D:\Develop\Repository\org\springframework\spring-webmvc\5.3.23\spring-webmvc-5.3.23.jar;D:\Develop\Repository\org\springframework\spring-aop\5.3.23\spring-aop-5.3.23.jar;D:\Develop\Repository\org\springframework\spring-context\5.3.23\spring-context-5.3.23.jar;D:\Develop\Repository\org\springframework\spring-expression\5.3.23\spring-expression-5.3.23.jar;D:\Workspace\Java\mybatis-plus-cache-example\mybatis-plus-cache-example-web\target\classes;D:\Develop\Repository\org\mybatis\spring\boot\mybatis-spring-boot-starter\2.2.2\mybatis-spring-boot-starter-2.2.2.jar;D:\Develop\Repository\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\2.2.2\mybatis-spring-boot-autoconfigure-2.2.2.jar;D:\Develop\Repository\org\mybatis\mybatis\3.5.9\mybatis-3.5.9.jar;D:\Develop\Repository\org\mybatis\mybatis-spring\2.0.7\mybatis-spring-2.0.7.jar;D:\Develop\Repository\com\mysql\mysql-connector-j\8.0.31\mysql-connector-j-8.0.31.jar;D:\Develop\Repository\org\projectlombok\lombok\1.18.24\lombok-1.18.24.jar;D:\Develop\Repository\org\springframework\boot\spring-boot-starter-test\2.6.13\spring-boot-starter-test-2.6.13.jar;D:\Develop\Repository\org\springframework\boot\spring-boot-test\2.6.13\spring-boot-test-2.6.13.jar;D:\Develop\Repository\org\springframework\boot\spring-boot-test-autoconfigure\2.6.13\spring-boot-test-autoconfigure-2.6.13.jar;D:\Develop\Repository\com\jayway\jsonpath\json-path\2.6.0\json-path-2.6.0.jar;D:\Develop\Repository\net\minidev\json-smart\2.4.8\json-smart-2.4.8.jar;D:\Develop\Repository\net\minidev\accessors-smart\2.4.8\accessors-smart-2.4.8.jar;D:\Develop\Repository\org\ow2\asm\asm\9.1\asm-9.1.jar;D:\Develop\Repository\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;D:\Develop\Repository\jakarta\xml\bind\jakarta.xml.bind-api\2.3.3\jakarta.xml.bind-api-2.3.3.jar;D:\Develop\Repository\jakarta\activation\jakarta.activation-api\1.2.2\jakarta.activation-api-1.2.2.jar;D:\Develop\Repository\org\assertj\assertj-core\3.21.0\assertj-core-3.21.0.jar;D:\Develop\Repository\org\hamcrest\hamcrest\2.2\hamcrest-2.2.jar;D:\Develop\Repository\org\junit\jupiter\junit-jupiter\5.8.2\junit-jupiter-5.8.2.jar;D:\Develop\Repository\org\junit\jupiter\junit-jupiter-api\5.8.2\junit-jupiter-api-5.8.2.jar;D:\Develop\Repository\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;D:\Develop\Repository\org\junit\platform\junit-platform-commons\1.8.2\junit-platform-commons-1.8.2.jar;D:\Develop\Repository\org\apiguardian\apiguardian-api\1.1.2\apiguardian-api-1.1.2.jar;D:\Develop\Repository\org\junit\jupiter\junit-jupiter-params\5.8.2\junit-jupiter-params-5.8.2.jar;D:\Develop\Repository\org\junit\jupiter\junit-jupiter-engine\5.8.2\junit-jupiter-engine-5.8.2.jar;D:\Develop\Repository\org\junit\platform\junit-platform-engine\1.8.2\junit-platform-engine-1.8.2.jar;D:\Develop\Repository\org\mockito\mockito-core\4.0.0\mockito-core-4.0.0.jar;D:\Develop\Repository\net\bytebuddy\byte-buddy\1.11.22\byte-buddy-1.11.22.jar;D:\Develop\Repository\net\bytebuddy\byte-buddy-agent\1.11.22\byte-buddy-agent-1.11.22.jar;D:\Develop\Repository\org\objenesis\objenesis\3.2\objenesis-3.2.jar;D:\Develop\Repository\org\mockito\mockito-junit-jupiter\4.0.0\mockito-junit-jupiter-4.0.0.jar;D:\Develop\Repository\org\skyscreamer\jsonassert\1.5.1\jsonassert-1.5.1.jar;D:\Develop\Repository\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;D:\Develop\Repository\org\springframework\spring-core\5.3.23\spring-core-5.3.23.jar;D:\Develop\Repository\org\springframework\spring-jcl\5.3.23\spring-jcl-5.3.23.jar;D:\Develop\Repository\org\springframework\spring-test\5.3.23\spring-test-5.3.23.jar;D:\Develop\Repository\org\xmlunit\xmlunit-core\2.8.4\xmlunit-core-2.8.4.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit5 com.zxl.mybatispluscacheexample.CacheTest2,testCache
14:38:17.563 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
14:38:17.573 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
14:38:17.604 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.zxl.mybatispluscacheexample.CacheTest2] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
14:38:17.617 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.zxl.mybatispluscacheexample.CacheTest2], using SpringBootContextLoader
14:38:17.621 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.zxl.mybatispluscacheexample.CacheTest2]: class path resource [com/zxl/mybatispluscacheexample/CacheTest2-context.xml] does not exist
14:38:17.622 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.zxl.mybatispluscacheexample.CacheTest2]: class path resource [com/zxl/mybatispluscacheexample/CacheTest2Context.groovy] does not exist
14:38:17.622 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.zxl.mybatispluscacheexample.CacheTest2]: no resource found for suffixes {-context.xml, Context.groovy}.
14:38:17.622 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.zxl.mybatispluscacheexample.CacheTest2]: CacheTest2 does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
14:38:17.672 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.zxl.mybatispluscacheexample.CacheTest2]
14:38:17.735 [main] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [D:\Workspace\Java\mybatis-plus-cache-example\mybatis-plus-cache-example-start\target\classes\com\zxl\mybatispluscacheexample\Application.class]
14:38:17.737 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.zxl.mybatispluscacheexample.Application for test class com.zxl.mybatispluscacheexample.CacheTest2
14:38:17.841 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.zxl.mybatispluscacheexample.CacheTest2]: using defaults.
14:38:17.841 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
14:38:17.859 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@543295b0, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@54422e18, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@117159c0, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@3e27ba32, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@7ef82753, org.springframework.test.context.support.DirtiesContextTestExecutionListener@3b0fe47a, org.springframework.test.context.transaction.TransactionalTestExecutionListener@202b0582, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@235ecd9f, org.springframework.test.context.event.EventPublishingTestExecutionListener@1ca3b418, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@58cbafc2, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@2034b64c, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@75d3a5e0, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@74d1dc36, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@7161d8d1, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@74e28667]
14:38:17.863 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@350aac89 testClass = CacheTest2, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@1c5920df testClass = CacheTest2, locations = '{}', classes = '{class com.zxl.mybatispluscacheexample.Application}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@4f209819, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@39529185, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@5119fb47, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@393671df, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@6c80d78a, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@2669b199], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true]], class annotated with @DirtiesContext [false] with mode [null].

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::               (v2.6.13)

2024-10-29 14:38:18.152  INFO 19696 --- [           main] c.z.mybatispluscacheexample.CacheTest2   : Starting CacheTest2 using Java 1.8.0_202 on LAPTOP-0EUI3LK1 with PID 19696 (started by Raycus in D:\Workspace\Java\mybatis-plus-cache-example\mybatis-plus-cache-example-start)
2024-10-29 14:38:18.153 DEBUG 19696 --- [           main] c.z.mybatispluscacheexample.CacheTest2   : Running with Spring Boot v2.6.13, Spring v5.3.23
2024-10-29 14:38:18.153  INFO 19696 --- [           main] c.z.mybatispluscacheexample.CacheTest2   : No active profile set, falling back to 1 default profile: "default"
2024-10-29 14:38:19.515  INFO 19696 --- [           main] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: class path resource [static/index.html]
 _ _   |_  _ _|_. ___ _ |    _ 
| | |\/|_)(_| | |_\  |_)||_|_\ 
     /               |         
                        3.5.3 
2024-10-29 14:38:20.912  INFO 19696 --- [           main] c.z.mybatispluscacheexample.CacheTest2   : Started CacheTest2 in 3.013 seconds (JVM running for 3.907)
2024-10-29 14:38:21.191 DEBUG 19696 --- [           main] c.z.m.twolevel.mapper.UserMapper2        : Cache Hit Ratio [com.zxl.mybatispluscacheexample.twolevel.mapper.UserMapper2]: 0.0
2024-10-29 14:38:21.200 DEBUG 19696 --- [           main] o.s.jdbc.datasource.DataSourceUtils      : Fetching JDBC Connection from DataSource
2024-10-29 14:38:21.201  INFO 19696 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2024-10-29 14:38:21.395  INFO 19696 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2024-10-29 14:38:21.400 DEBUG 19696 --- [           main] c.z.m.t.m.UserMapper2.selectAllUsers     : ==>  Preparing: SELECT * FROM users
2024-10-29 14:38:21.420 DEBUG 19696 --- [           main] c.z.m.t.m.UserMapper2.selectAllUsers     : ==> Parameters: 
2024-10-29 14:38:21.441 DEBUG 19696 --- [           main] c.z.m.t.m.UserMapper2.selectAllUsers     : <==      Total: 2
第一次查询结果:[UserEntity2(id=1, name=Alice, age=30), UserEntity2(id=2, name=Bob, age=25)]
2024-10-29 14:38:21.447  WARN 19696 --- [           main] o.apache.ibatis.io.SerialFilterChecker   : As you are using functionality that deserializes object streams, it is recommended to define the JEP-290 serial filter. Please refer to https://docs.oracle.com/pls/topic/lookup?ctx=javase15&id=GUID-8296D8E8-2B93-4B9A-856E-0A65AF9B8C66
2024-10-29 14:38:21.448 DEBUG 19696 --- [           main] c.z.m.twolevel.mapper.UserMapper2        : Cache Hit Ratio [com.zxl.mybatispluscacheexample.twolevel.mapper.UserMapper2]: 0.5
第二次查询结果:[UserEntity2(id=1, name=Alice, age=30), UserEntity2(id=2, name=Bob, age=25)]
2024-10-29 14:38:21.528  INFO 19696 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2024-10-29 14:38:21.535  INFO 19696 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

Process finished with exit code 0

关键代码

启动类

package com.zxl.mybatispluscacheexample;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.zaxxer.hikari.HikariDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@SpringBootApplication
@MapperScan("com.zxl.mybatispluscacheexample")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public DataSource getDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://10.30.0.95:3306/test?useSSL=false&serverTimezone=UTC");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        return dataSource;
    }

    @Bean
    public MybatisPlusInterceptor getMybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
}

Mapper

package com.zxl.mybatispluscacheexample.twolevel.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zxl.mybatispluscacheexample.twolevel.entity.UserEntity2;
import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@CacheNamespace
public interface UserMapper2 extends BaseMapper<UserEntity2> {

    @Select("SELECT * FROM users")
    List<UserEntity2> selectAllUsers();
}

测试类

package com.zxl.mybatispluscacheexample;

import com.zxl.mybatispluscacheexample.twolevel.mapper.UserMapper2;
import com.zxl.mybatispluscacheexample.twolevel.entity.UserEntity2;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
public class CacheTest2 {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Test
    public void testCache() {
        try (SqlSession sqlSession = sqlSessionFactory.openSession(false)) {
            // 获取 SqlSession 中的 Mapper
            UserMapper2 mapper = sqlSession.getMapper(UserMapper2.class);

            // 第一次查询
            List<UserEntity2> usersFirst = mapper.selectAllUsers();
            System.out.println("第一次查询结果:" + usersFirst);

            // 模拟关闭当前 SqlSession,释放缓存
            sqlSession.close();

            // 新开一个 SqlSession,尝试从缓存中读取数据
            try (SqlSession sqlSessionNew = sqlSessionFactory.openSession(false)) {
                UserMapper2 mapperNew = sqlSessionNew.getMapper(UserMapper2.class);
                List<UserEntity2> usersSecond = mapperNew.selectAllUsers();
                System.out.println("第二次查询结果:" + usersSecond);

                // 断言两次查询结果相同
                assertThat(usersFirst).isEqualTo(usersSecond);
            }
        }
    }
}

完整代码

通过百度网盘分享的文件:mybatis-plus-cache-example-2.zip
链接:https://pan.baidu.com/s/1-_hO03X-vzf_UulRZRezlA?pwd=xhhf 提取码:xhhf