Mybatis series (22) – related concepts – first level cache

Time:2020-10-22

The first level cache of mybatis is on the session. As long as the data checked by the session is put on the session, the next time you query the data with the same ID, you can directly extract it from the cache instead of in the database.

Step 1: run first, see the effect, and then learn
Step 2: imitation and troubleshooting
Step 3: look up the data with the same ID in a session
Step 4: look up the data with the same ID in different sessions

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.

Compare with the software diffmerge. 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: look up the data with the same ID in a session

As shown in the figure, query the category object with id = 1 twice in session 1.
The first time, it will fetch data from the database, but the second time, it will not access the database, but directly from the session.

Mybatis series (22) - related concepts - first level cache

package com.how2java;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.how2java.pojo.Category;
public class TestMybatis {
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session1 = sqlSessionFactory.openSession();
        Category c1 = session1.selectOne("getCategory", 1);
        System.out.println(c1);
        Category c2 = session1.selectOne("getCategory", 1);
        System.out.println(c2);
        session1.commit();
        session1.close();
    }

}

Step 4: look up the data with the same ID in different sessions

This time, if you open another session and retrieve the data with the same ID, you will find that SQL statements need to be executed, confirming that the level 1 cache is in the session.

Mybatis series (22) - related concepts - first level cache

package com.how2java;

import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.how2java.pojo.Category;
public class TestMybatis {
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session1 = sqlSessionFactory.openSession();
        Category c1 = session1.selectOne("getCategory", 1);
        System.out.println(c1);
        Category c2 = session1.selectOne("getCategory", 1);
        System.out.println(c2);
        session1.commit();
        session1.close();
        SqlSession session2 = sqlSessionFactory.openSession();
        Category c3 = session2.selectOne("getCategory", 1);
        System.out.println(c3);
        session2.commit();
        session2.close();
    }
}

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

Recommended Today

Blog based on beego, go blog

Go Blog A beego based development, can quickly create personal blog, CMS system Include functions see Official website of go bloggo-blog.cn Demo siteleechan.online Update log time function January 23, 2020 New top post function February 2, 2020 New custom navigation function February 4, 2020 New site announcement function February 6, 2020 New link module February […]