MyBatis 二級緩存

二級緩存

須要在映射文件中添加該標籤java

<cache/>
複製代碼

映射語句中的select語句將會被緩存, 映射語句中的insert update delete 語句將會刷新緩存 緩存使用LRU算法回收 如今完整的配置文件以下算法

<?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.ming.MyBatis.RoleMapper">
	
	<resultMap type="role" id="roleMap">
		<!-- id爲主鍵映射關係 其中數據庫中的id爲主鍵 -->
		<id column="id" property="id" javaType="int" jdbcType="INTEGER"/>
		<!-- result爲其餘基本數據類型和實體類之間的映射 映射關係爲role_name 到 roleName之間的映射 數據類型爲string到VARCHAR之間的映射關係 -->
		<result column="role_name" property="roleName" javaType="string" jdbcType="VARCHAR"/>
		<!-- 這裏使用typeHandler表示遇到此處理集的時候,將會執行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"/>
		<!--將會調用接口表明的SQL 進行執行查詢 -->
		<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>
複製代碼

返回的POJO對象須要實現java.io.Serializable的接口數據庫

一樣也能夠修改瀏覽器

<cache eviction="LRU" flushInterval="100000" size="1024" readOnly="true"/>
複製代碼

java的幾種引用緩存

強引用

Object object = new Object();
複製代碼

這是強引用,當其賦值爲null的時候,若內存空間不足,gc會直接清理掉該內存對象bash

軟引用

須要使用SoftReference類,實現軟引用mybatis

String str = new String("ming");      // 強引用
SoftReference<String> softRef = new SoftReference<String>(str);    // 軟引用
複製代碼

這裏爲軟引用 當內存不足時,會轉換爲軟引用,垃圾回收器進行回收app

使用場景 瀏覽器的回退按鈕ui

弱引用

一旦不定時運行的垃圾回收其發現有弱引用對象,將會直接回收該對象spa

須要使用WeakReference

String str = new String("ming");
WeakReference<String> weakReference = new WeakRefrence<String>(str);
複製代碼

當垃圾回收其掃描到回收對象的時候,會直接進行回收掉

弱引用須要和引用隊列聯合使用

虛引用

若是一個對象僅僅持有虛引用,那麼就和沒有同樣.使用的是PhantomReference 虛引用要和引用隊列一塊兒使用,垃圾回收線程回收該線程時,會發送一個系統通知,達到通知的做用.

相關文章
相關標籤/搜索