Mybatis series (23) – related concepts – secondary cache

Time:2020-10-21

Mybatis Level 2 cache is sessionfactory. If two queries are based on the same sessionfactory, the data will be fetched from the secondary cache instead of the database.

Step 1: run first, see the effect, and then learn
Step 2: imitation and troubleshooting
Step 3: Based on the previous knowledge point
Step 4: observe that there is no L2 cache
Step 5: start L2 cache
Step 6: in Category.xml Add < cache / > in
Step 7: serialize category
Step 8: run the test

Step 1: run first, see the effect, and then learn

As the old rule, first download the executable items in the download area (click to enter), configure and run them, and then learn what steps to do to achieve this effect.

Step 2: imitation and troubleshooting

After making sure that the runnable project runs correctly, follow the steps of the tutorial and imitate the code again.
It is inevitable that there is code discrepancy in the simulation process, which leads to the failure to get the expected running resultsright key(can run the project) and your own code to locate the problem.
In this way,Learning is effective and troubleshooting is efficientIt can obviously improve the learning speed and cross the threshold of learning.

It is recommended to use diffmerge software for folder comparison. Compare your own project folder with my runnable project folder.
This software is very powerful, you can know which two files in the folder are wrong, and mark them out clearly
Here is a green installation and use tutorial: diffmerge download and use tutorial

Step 3: Based on the previous knowledge point

The knowledge points of the first level cache are modified to support the second level cache

Step 4: observe no L2 cache

Mybatis series (23) - related concepts - secondary cache

Step 5: start the L2 cache

Line 11 adds a segment configuration to support L2 caching

<setting name="cacheEnabled" value="true"/>

<?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>
<settings>
    <! -- turn on the delay load switch -- >
    <setting name="lazyLoadingEnabled" value="true" />
    <! -- change active load to message load that is, load on demand -- >
    <setting name="aggressiveLazyLoading" value="false"/>
    <setting name="cacheEnabled" value="true"/>
</settings>
<typeAliases>
    <package name="com.how2java.pojo"/>
</typeAliases>
<environments default="development">
    <environment id="development">
        <transactionManager type="JDBC"/>
        <dataSource type="POOLED">
            <property name="driver" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8"/>
            <property name="username" value="root"/>
            <property name="password" value="admin"/>
        </dataSource>
    </environment>
</environments>
<mappers>
    <mapper resource="com/how2java/pojo/Category.xml"/>
    <mapper class="com.how2java.mapper.CategoryMapper"/>
    <mapper class="com.how2java.mapper.ProductMapper"/>
</mappers>
</configuration>

Step 6: in the Category.xml Add < cache / > in

Line 7 adds < cache / > to start the secondary caching of category objects

<?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.how2java.pojo">
    <cache/>
    <insert id="addCategory" parameterType="Category" >
            insert into category_ ( name ) values (#{name})   
        </insert>

    <delete id="deleteCategory" parameterType="Category" >
            delete from category_ where id= #{id}  
        </delete>

    <select id="getCategory" parameterType="_int" resultType="Category">
            select * from   category_  where id= #{id}   
        </select>

    <update id="updateCategory" parameterType="Category" >
            update category_ set name=#{name} where id=#{id}   
        </update>
    <select id="listCategory" resultType="Category">
        select * from   category_
        <if test="start!=null and count!=null">
            limit #{start},#{count}
        </if>
    </select>
</mapper>

Step 7: serialize category

Let category implement serialization interface

package com.how2java.pojo;

import java.io.Serializable;
import java.util.List;
public class Category implements Serializable{
    private int id;
    private String name;
    List<Product> products;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<Product> getProducts() {
        return products;
    }
    public void setProducts(List<Product> products) {
        this.products = products;
    }
    @Override
    public String toString() {
        return "Category [id=" + id + ", name=" + name + "]";
    }
}

Step 8: run the test

Run testmybatis again, as shown in the figure. Query the data with id = 1 in the same sessionfactory. Only the SQL statement needs to be executed for the first time, and then it is taken from the cache

Mybatis series (23) - related concepts - secondary cache

For more information, click to learn: https://how2j.cn/k/mybatis/mybatis-annotation-second-level-cache/1102.html

Recommended Today

Rabbitmq cluster of Message Oriented Middleware

In the last blog, we briefly introduced the instructions related to rabbitmq installation and configuration and the instructions of related subcommands of rabbitmqctl. Please refer to https://www.cnblogs.com/qiuhom-1874/p/13561245.html Today, let’s talk about rabbitmq cluster. The reason why we use cluster is because in a distributed application environment, rabbitmq is used to connect components Once the rabbitmq […]