Using redis as the secondary cache of mybatis
This paper introduces the configuration and application of using mybatis-redis project as the second-level cache of mybatis in production project.
First, add a dependency to pom:
Org.mybatis.caches mybatis-redis 1.0.0-beta2
After the dependency is added successfully, create the configuration file redis.properties for redis under src/main/resources
# 1.0 redis factory configuration
Host=xxx.xxx.xxx.xxx
Port=6379
Password=xxxxxxxx
Timeout=5000
UsePool=true
# redis pool configuration
MaxTotal=600
MaxIdle=300
MinIdle=10
MaxWaitMillis=2000
TestOnBorrow=false
TestOnReturn=false
After the configuration file is created successfully, write the configuration in between in the generated xxxMapper.xml
UseCache indicates whether caching is required
FlushCache indicates whether the cache needs to be flushed after insertion.
The test code is as follows. Refresh the cache synchronously after update and use the default configuration.
@ Testpublic void test01 () {Vc3JourneyAttributeDefinitions vc3JourneyAttributeDefinitions=new Vc3JourneyAttributeDefinitions (); vc3JourneyAttributeDefinitions=vc3JourneyAttributeDefinitionsMapper.selectByPrimaryKey (1L); vc3JourneyAttributeDefinitions.setAttributeName ("Total distance of valid pulses"); vc3JourneyAttributeDefinitionsMapper.updateByPrimaryKey (vc3JourneyAttributeDefinitions); vc3JourneyAttributeDefinitions=vc3JourneyAttributeDefinitionsMapper.selectByPrimaryKey (1L); System.out.println (vc3JourneyAttributeDefinitions.getAttributeName ());}
Https://github.com/mybatis/redis-cache from above github
You can download the source code to view.