1. Case introduction
The results of Mybatis query database are stored in Redis database as key-value pairs instead of secondary cache in Mybatis. When querying the same operation, we can read the results directly in Redis to improve efficiency.
2. Case Realization
2.1 Importing project dependencies
<!--redis rely on--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> <scope>test</scope> </dependency> <!--mybatis integration redis--> <dependency> <groupId>org.mybatis.caches</groupId> <artifactId>mybatis-redis</artifactId> <version>1.0.0-beta2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency>
2.2 Prepare entity classes and databases
Entity classes must implement serialization interfaces (when Redis is turned off, data is saved from memory to disk)
public class User implements Serializable{ private Integer id; private String name; private Integer age; private Date birthday; private BigDecimal salary; }
2.3 Prepare Mybatis related classes
(1) Create mapper interface
(2) Create Mybatis.xml
<?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"/> <!--data source--> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///person"/> <property name="username" value="root"/> <property name="password" value="xxx"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/project/Redis/mapper/usermapper.xml"/> </mappers> </configuration>
(3) Creating interface mapping files
Modify the secondary cache type to the RedisCache class
<?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.project.Redis.project.mapper.UserMapper"> <!--Two ways--> <!--Way 1: Create your own classes MyCache,Realization cache Method <cache type="com.ujiuye.cache.my.MyCache"/>--> <!--Mode 2: Import mybatis-redis Dependency, automatic deposit redis--> <cache type="org.mybatis.caches.redis.RedisCache"/> <select id="get" resultType="com.project.Redis.project.bean.User"> select * from user where id = #{id} </select> </mapper>
2.4 Create the jedis tool class (Redis connection pool object)
package com.project.Redis.utils; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; // jedis is a java client tool to connect redis, and the tool class returns a redis object public class JedisUtil { private static JedisPool pool = null; static { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxIdle(10); config.setMaxTotal(50); config.setMinIdle(5); pool= new JedisPool(config,"127.0.0.1",6379); } public static Jedis getJedis(){ return pool.getResource(); } }
2.5 test
public class App { @Test public void testCache() throws Exception{ SqlSession session = MybatisUtil.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); User user = mapper.get(1); session.commit(); session.close(); System.out.println(user); System.out.println("==========================="); SqlSession session2 = MybatisUtil.getSession(); UserMapper mapper2 = session2.getMapper(UserMapper.class); User user2 = mapper2.get(1); session2.commit(); session2.close(); System.out.println(user2); } }
2.6 results
You can see that two identical query statements are executed, the query is executed only once, and the result is read directly from the cache for the second time.