MyBatis Level 2 Cache

Keywords: Java Mybatis xml encoding

Secondary Cache

This tag needs to be added to the mapping file

    <cache/>

The select statement in the mapping statement will be cached, and the insert update delete statement in the mapping statement will refresh the cache
Cache recycling using LRU algorithm
Now the complete configuration file is as follows

<?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">
<!-- Define interface classes -->
<mapper namespace="com.ming.MyBatis.RoleMapper">
    
    <resultMap type="role" id="roleMap">
        <!-- id Map relationships to primary keys where the id Is Primary Key -->
        <id column="id" property="id" javaType="int" jdbcType="INTEGER"/>
        <!-- result Mapping relationships between other basic data types and entity classes are role_name reach roleName The mapping data type between is string reach VARCHAR Mapping relationship between -->
        <result column="role_name" property="roleName" javaType="string" jdbcType="VARCHAR"/>
        <!-- Use here typeHandler Indicates when this processing set is encountered,Will be executed com.ming.MyBatis.StringTypeHandler -->
        <result column="note" property="note" typeHandler="com.ming.MyBatis.StringTypeHandler"/>
    </resultMap>
    
    <select id="getRole" parameterType="int" resultType="com.ming.MyBatis.POJO.Role">
        SELECT id, role_name as roleName, note FROM t_role WHERE id = #{id}
    </select>
    
    <select id="findRoleByteMap" parameterType="map" resultMap="roleMap">
        SELECT id, role_name, note FROM t_role
        WHERE role_name LIKE CONCAT('%', #{roleName}, '%')
        AND note LIKE CONCAT('%', #{note}, '%')
    </select>
    
    <select id="findRoleByteMap1" resultMap="roleMap">
        SELECT id, role_name, note FROM t_role
        WHERE role_name LIKE CONCAT('%', #{roleName}, '%')
        AND note LIKE CONCAT('%', #{note}, '%')
    </select>
    
    
    <resultMap id="studentSelfCardMap" type="com.ming.MyBatis.POJO.StudentCard">
        <id column="uid" property="uid"/>
        <result column="student_number" property="studentNumber"/>
        <result column="birthplace" property="birthplace" />
        <result column="date_of_issue" property="dateOfIssue" jdbcType="DATE" javaType="java.util.Date"/>
        <result column="end_date" property="endDate" jdbcType="DATE" javaType="java.util.Date"/>
        <result column="remarks" property="remarks" />
    </resultMap>

    <select id="findStudentSelfCardByStudentId" parameterType="int" resultMap="studentSelfCardMap">
        SELECT student_card.uid, student_card.student_number, student_card.remarks,
        student_card.end_date, student_card.date_of_issue, student_card.birthplace
        FROM student_card WHERE student_card.uid = #{studentId};
    </select>
    
    <resultMap id="studentMap" type="com.ming.MyBatis.POJO.Student">
        <id column="uid" property="uid"/>
        <result column="student_name" property="studentName"/>
        <result column="gender" property="gender"/>
        <result column="student_id_number" property="studentIdNumber"/>
        <result column="remarks" property="remarks"/>
        <!--Interface representative will be invoked SQL Make an Execution Query -->
        <association property="studentCard" column="uid" select="com.ming.MyBatis.RoleMapper.findStudentSelfCardByStudentId"/>
    </resultMap>
    
    <select id="getStudent" parameterType="int" resultMap="studentMap">
        SELECT student.uid, student.gender, student.remarks, student.student_id_number,
        student.student_name
        FROM student
        WHERE student.uid = 1;
    </select>
    
    <cache/>
</mapper>

The POJO object returned needs to implement the interface for java.io.Serializable

It can also be modified

    <cache eviction="LRU" flushInterval="100000" size="1024" readOnly="true"/>

Several references to java

Strong Reference

Object object = new Object();

This is a strong reference, and when null is assigned, gc will clean up the memory object directly if there is not enough memory space

Soft reference

The SoftReference class is required to implement soft references

String str = new String("ming");      // Strong Reference
SoftReference<String> softRef = new SoftReference<String>(str);    // Soft reference

Here is a soft reference
When memory is low, it is converted to a soft reference and recycled by the garbage collector

Use the Scene Browser's Back button

Weak reference

Once an intermittent garbage collection finds a weak reference object, it will be recycled directly

WeakReference is required

String str = new String("ming");
WeakReference<String> weakReference = new WeakRefrence<String>(str);

When garbage collection scans to recycled objects, it is recycled directly

Weak references need to be combined with reference queues

Virtual reference

If an object only has a dummy reference, it is the same as no object. PhantomReference is used
Virtual references are used with reference queues, and when a garbage collection thread recycles the thread, a system notification is sent to notify it.

Posted by schris403 on Wed, 15 May 2019 16:48:36 -0700