Preface:

Now the JAVA industry, seems to have been SpringBoot + SpringCloud world, early SSH,SSM framework has been old, and the JPA framework combined with SpringBoot saves a lot of add, delete, change and check SQL, but it is relatively clumsy, In the face of some complex and changeable logic often powerless, and the corresponding Mybatis is welcomed by the majority of JAVA siege lions due to its high flexibility. Before the integration of Springboot + Mybatis, a few days ago to see an interview to ask a question, Mybatis level cache, level two cache. I think this is also an important point, so today I decided to take a closer look at the mysterious first and second level cache.

The prophet

  • Level 1 cache is the SqlSession level cache. The sqlSession object is constructed when operating on the database, and there is a data structure (HashMap) in the object to store cached data. The cache data area (HashMap) between different SQLsessions does not affect each other. Level 1 caching is disabled by default.
  • Level 2 cache is mapper level cache, multiple SQLsessions to operate the SAME MAPper SQL statement, multiple SQLsessions can share level 2 cache, level 2 cache is cross-SQLSession. Enable level 2 caching (entity classes must be serialized) and configure it in the configuration file.

MyBatis- Plus configuration essentials

Key Point 1

The core configuration of Mybatis – Plus in Springboot is as follows

mybatis-plus.configuration.cache-enabled=true
mybatis-plus.mapper-locations=classpath*:/mapper/*.xml
mybatis-plus.type-aliases-package=com.sch.app.mybatis.entity
logging.level.com.sch.app.mybatis.mapper= debug
Copy the code

In addition to the basic SpringBoot dependencies, there are

Key Points 2

< the dependency > < groupId > com. Baomidou < / groupId > < artifactId > mybatis - plus - the boot - starter < / artifactId > < version > 3.3.2 rainfall distribution on 10-12 < / version >  </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>Copy the code

Key Point 3

  • The mybatis statement generates generatorConfig. XML, which is used to generate the required basic entity classes and interfaces and mapper files in one step (under the Resouses directory).

    <property name="nullCatalogMeansCurrent" value="true"/> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <javaModelGenerator targetPackage="com.sch.app.mybatis.entity" targetProject="SpringbootMybatis\src\main\java"> <property name="enableSubPackages" value="true"/> <! <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="mapper" targetProject="SpringbootMybatis\src\main\resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.sch.app.mybatis.mapper" targetProject="SpringbootMybatis\src\main\java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <! <table schema=" tableName="d_dictionary"></table> <table schema="" tableName="d_dictionary_type"></table> <table schema="" tableName="c_resource"></table> <table schema="" tableName="c_role"></table> <table schema="" tableName="c_role_resource"></table> <table schema="" tableName="c_user_online"></table> <table schema="" tableName="c_user"></table> <table schema="" tableName="c_user_role"></table> <table schema="" tableName="test"></table>  </context>Copy the code

The Run Mybatis Generator is available under the Eclipse plugin market

  • Click Execute to generate the following

Test of Mybatis-plus level 1 cache

  • First, you must enable the log for viewing the effect

    logging.level.com.sch.app.mybatis.mapper= debug

Com. SCH. App, mybatis mapper is the mapper interfaces directory

  • Test Code 1

    @Autowired private SqlSessionFactory sqlSessionFactory;

    @RequestMapping(value = “/testMybatis”) @ResponseBody public void testMybatis(){ SqlSession sqlSession = sqlSessionFactory.openSession(); TestMapper testMapper = sqlSession.getMapper(TestMapper.class); for (int i = 0; i < 3; i++) { Test selectByPrimaryKey = testMapper.selectByPrimaryKey(5); The info (” result: “+ selectByPrimaryKey getUsername ()); }

  • As a result,

  • As you can see, there is only one search and no SQL print for the second or third times

  • Test Code 2

    @RequestMapping(value = “/testMybatis”) @ResponseBody public void testMybatis(){ SqlSession sqlSession = sqlSessionFactory.openSession(); TestMapper testMapper = sqlSession.getMapper(TestMapper.class); for (int i = 0; i < 3; i++) { Test selectByPrimaryKey = testMapper.selectByPrimaryKey(5); The info (” result: “+ selectByPrimaryKey getUsername ()); If (I = = 2) {selectByPrimaryKey. SetUsername (” liu cherish jun’s sister “); testMapper.updateByPrimaryKey(selectByPrimaryKey); Test selectByPrimaryKey2 = testMapper.selectByPrimaryKey(5); The info (” updated user name: “+ selectByPrimaryKey2. GetUsername ()); }}

  • Print result:

Insert, update, or delete will clear the level 1 cache in SqlSession. Level 1 cache is not cleared only for query operations.

Mybatis- Plus level 2 cache test

Level 2 cache can be enabled in mapper in addition to configuration files

  • Test Code 1

     @RequestMapping(value = "/testMybatis2")
    	    @ResponseBody
    	    public void testMybatis2(){
    	    	SqlSession openSession1 = sqlSessionFactory.openSession();
    	    	SqlSession openSession2 = sqlSessionFactory.openSession();
    	    	TestMapper mapper1 = openSession1.getMapper(TestMapper.class);
    	    	TestMapper mapper2 = openSession2.getMapper(TestMapper.class);
    	    	Test selectByPrimaryKey = mapper1.selectByPrimaryKey(5);
    	    	System.out.println(selectByPrimaryKey.getUsername());
    	    	openSession1.close();
    	    	Test selectByPrimaryKey2 = mapper2.selectByPrimaryKey(5);
    	    	System.out.println(selectByPrimaryKey2.getUsername());
    	    	openSession2.close();
    	   } 
    Copy the code
  • The test results

The test result shows that the code for the first time check mapper1. SelectByPrimaryKey (5) when the SQL, then close the first session the second checked with other sqlseeison no call SQL, It shows that secondary roughing has nothing to do with SQLSEesion and is related to MAPper.

  • Test Code 2

    @RequestMapping(value = “/testMybatis3″) @ResponseBody public void testMybatis3(){ SqlSession openSession1 = sqlSessionFactory.openSession(); SqlSession openSession2 = sqlSessionFactory.openSession(); SqlSession openSession3 = sqlSessionFactory.openSession(); TestMapper mapper1 = openSession1.getMapper(TestMapper.class); TestMapper mapper2 = openSession2.getMapper(TestMapper.class); TestMapper mapper3 = openSession3.getMapper(TestMapper.class); Test selectByPrimaryKey = mapper1.selectByPrimaryKey(5); System.out.println(selectByPrimaryKey.getUsername()); openSession1.close(); SelectByPrimaryKey. SetUsername (” liu cherish jun elder sister “); mapper2.updateByPrimaryKey(selectByPrimaryKey); openSession2.commit();

    	    	Test selectByPrimaryKey3 = mapper3.selectByPrimaryKey(5);
    	    	System.out.println(selectByPrimaryKey3.getUsername());
    	    	openSession3.close();
    	   } 
    Copy the code
  • Print the result

“Accordingly, did update mapper2. UpdateByPrimaryKey (selectByPrimaryKey); After that, the level 2 cache is cleared. Features are similar to level 1 caching.

In addition to the first time, we can use userCache to set specific statements to disable level 2 caching

  • To perform http://localhost:8080/testMybatis2 after printing results

  • The selectByPrimaryKey query was checked from the database twice after the secondary cache was disabled.

summary

  • Level-1 cache is started by default and belongs to the session level. When a session performs the same query for several times, level-1 cache is enabled. If the queried data is updated or deleted, level-1 cache is checked from the database again instead of being used.
  • Mybatis -plus.configuration.cache-enabled=true, 2. Enable the corresponding MAPper file. 3. Implement the Serializable interface for the corresponding entity class. To disable level 2 caching for a SQL statement, add useCache= “false” to the SQL statement definition of the specific XML. Also remember that it has nothing to do with the session and everything to do with the namespace of XML, the concrete Mapper.
  • In the same namespace of the Mapper, if the cache needs to be refreshed after other INSERT, update, or DELETE operations, dirty reads will occur if the cache is not refreshed.
  • If the attribute flushCache= true is set in the Statement configuration, the level-2 cache can be flushed. If the value is false, dirty reads may occur. Opensession.clearcache () enables flushing of level 1 caches.

Thank you for reading here, the article is inadequate, welcome to point out; If you think it’s good, give me a thumbs up.

Also welcome to pay attention to my public number: Java programmers gathering place, Maidong will share Java related technical articles or industry information every day, welcome to pay attention to and forward the article!