MyBatis articles] four. MyBatis Superior Chapter cache (three chapters)

MyBatis Superior Chapter cache

github source ( day56-the mybatis-Senior ) https://github.com/1196557363/ideaMavenProject
difference MyBatis articles [] four. MyBatis senior first chapter and $ # {} {} symbols

Project Preparation

  1. So do not change because the environment in the same module, a reference to the first chapter of the Project Preparation
  2. Interface com.wpj. DAO .IUserDao2 changed com.wpj. DAO 2 .IUserDao2 has been shown to distinguish between knowledge
  3. mapper also changed mapper2 interfaces corresponding to remember to change the mapping file namespace.
  4. The mapper MyBatis mapping remember plus.

Cache

Here Insert Picture Description

1.1 cache sqlSession

1.1.1 Interface

package com.wpj.dao2;

import com.wpj.bean.*;
import org.apache.ibatis.annotations.*;

import java.util.*;
/**
 * ClassName: IUserDao2
 * Description:
 *
 * @author JieKaMi
 * @version 1.0
 * @date: 2020\1\7 0007 19:02
 * @since JDK 1.8
 */
public interface IUserDao2 {
    User getUserById(Integer id);
}

1.1.2 interface corresponds mapper

<?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.wpj.dao2.IUserDao2">

    <select id="getUserById" resultType="user">
        select * from user where id = #{id}
    </select>

</mapper>

1.1.3 test

import com.wpj.bean.*;
import com.wpj.dao.*;
import com.wpj.dao2.IUserDao2;
import org.apache.ibatis.io.*;
import org.apache.ibatis.session.*;
import org.junit.*;

import java.io.*;
import java.util.*;

/**
 * ClassName: MyBatisTest
 * Description:
 *
 * @author JieKaMi
 * @version 1.0
 * @date: 2020\1\7 0007 19:19
 * @since JDK 1.8
 */
public class MyBatisTest2 {
    /**
     * 测试一级缓存
     */
    @Test
    public void testSqlSessionCache(){
        String config = "MyBatis.xml";
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream(config);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            // 可以和数据库建立连接
            SqlSession sqlSession = sqlSessionFactory.openSession();
            IUserDao2 userDao2 = sqlSession.getMapper(IUserDao2.class);
            User user = userDao2.getUserById(1);
            System.out.println(user);
            System.out.println("-------sqlSession分割线--------");
            User user2 = userDao2.getUserById(2);
            System.out.println(user2);

            sqlSession.close();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(is!=null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

1.1.4 results

Here Insert Picture Description
Preparing: select * from user where id = ?
Parameters: 1(Integer)
Total: 1
User{id=1, name=‘wpj’, pwd=‘123’, age=22}
-------sqlSession分割线--------
User{id=1, name=‘wpj’, pwd=‘123’, age=22}

Made a sql statement, L1 cache is turned on. After (enabled by default) can not sqlSession.close () to make inquiries.

1.2 the secondary cache

1.2.1 test

	/**
     * 测试二级缓存
     */
    @Test
    public void testSqlSessionFactoryCache(){
        String config = "MyBatis.xml";
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream(config);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

            SqlSession sqlSession = sqlSessionFactory.openSession();
            IUserDao2 userDao2 = sqlSession.getMapper(IUserDao2.class);
            User user = userDao2.getUserById(1);
            System.out.println(user);

            System.out.println("-------sqlSessionFactory分割线--------");

            SqlSession sqlSession2 = sqlSessionFactory.openSession();
            IUserDao2 userDao22 = sqlSession2.getMapper(IUserDao2.class);
            User user2 = userDao22.getUserById(1);
            System.out.println(user2);

            sqlSession.close();
            sqlSession2.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(is!=null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

1.2.2 结果

Opening JDBC Connection
Created connection 1920467934.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@727803de]
Preparing: select * from user where id = ?
Parameters: 1(Integer)
Total: 1
User{id=1, name=‘wpj’, pwd=‘123’, age=22}

-------sqlSessionFactory分割线--------

Opening JDBC Connection
Created connection 1514160588.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@5a4041cc]
Preparing: select * from user where id = ?
Parameters: 1(Integer)
Total: 1
User{id=1, name=‘wpj’, pwd=‘123’, age=22}
Here Insert Picture Description

连接了2次JDBC Connection 很明显没有二级缓存 (默认关闭)

1.3 开启二级缓存

1.3.1 接口对应mapper文件中添加<cache />

	<!-- 开启二级缓存 -->
	<cache />

1.3.2 User实现序列化接口

public class User implements Serializable{
    private Integer id;
    private String name;
    private String pwd;
    private Integer age;
    ...
}

1.3.3 sqlSession要提交事务

/**
     * 测试二级缓存
     */
    @Test
    public void testSqlSessionFactoryCache(){
        String config = "MyBatis.xml";
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream(config);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

            SqlSession sqlSession = sqlSessionFactory.openSession();
            IUserDao2 userDao2 = sqlSession.getMapper(IUserDao2.class);
            User user = userDao2.getUserById(1);
            sqlSession.commit(); // 开启二级缓存需提交事务
            System.out.println(user);

            System.out.println("-------sqlSessionFactory分割线--------");

            SqlSession sqlSession2 = sqlSessionFactory.openSession();
            IUserDao2 userDao22 = sqlSession2.getMapper(IUserDao2.class);
            User user2 = userDao22.getUserById(1);
            System.out.println(user2);

            sqlSession.close();
            sqlSession2.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(is!=null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

1.3.4 结果

Opening JDBC Connection
Created connection 282432134.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@10d59286]
Preparing: select * from user where id = ?
Parameters: 1(Integer)
Total: 1
User{id=1, name=‘wpj’, pwd=‘123’, age=22}
-------sqlSessionFactory分割线--------
2020-01-07 21:27:48 DEBUG com.wpj.dao2.IUserDao2:62 - Cache Hit Ratio [com.wpj.dao2.IUserDao2]: 0.5
User{id=1, name=‘wpj’, pwd=‘123’, age=22}
Here Insert Picture Description

Connecting only once JDBC Connection, and the second opening is to obtain a secondary cache from the cache.

Other configurations of the secondary cache 1.2.2

<!--
	useCache
		true:默认值
		false:可以设置这条sql语句的结果不缓存,只是对二级缓存才有效
-->
<select id="getUserById" resultType="user" useCache="false">
	select * from user where id = #{id}
</select>
<!-- flushCache
		二级缓存默认会在insert,update,detele操作后刷新缓存,通过配置false达到不刷新。
 -->
<!--
	size:设置缓存中最大能放1024个对象
	readOnly:设置放在缓存中的数据是否是只读的(一般都是只读的)
	eviction:设置缓存的清空策略
		LRU:最少使用原则,移除最长时间不使用的对象 
		FIFO:先进先出原则,按照对象进入缓存顺序进行回收
	flushInterval:缓存刷新时间间隔
-->
<cache size="1024" readOnly="true" eviction="LRU" flushInterval="100000"/>
Explanation

Here Insert Picture Description

Published 42 original articles · won praise 8 · views 2442

Guess you like

Origin blog.csdn.net/TheNew_One/article/details/103881187