MyBatis 二級快取
二級快取
需要在對映檔案中新增該標籤
<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會直接清理掉該記憶體物件
軟引用
需要使用SoftReference類,實現軟引用
String str = new String("ming");// 強引用 SoftReference<String> softRef = new SoftReference<String>(str);// 軟引用
這裡為軟引用
當記憶體不足時,會轉換為軟引用,垃圾回收器進行回收
使用場景 瀏覽器的回退按鈕
弱引用
一旦不定時執行的垃圾回收其發現有弱引用物件,將會直接回收該物件
需要使用WeakReference
String str = new String("ming"); WeakReference<String> weakReference = new WeakRefrence<String>(str);
當垃圾回收其掃描到回收物件的時候,會直接進行回收掉
弱引用需要和引用佇列聯合使用
虛引用
如果一個物件僅僅持有虛引用,那麼就和沒有一樣.使用的是PhantomReference
虛引用要和引用佇列一起使用,垃圾回收執行緒回收該執行緒時,會發送一個系統通知,達到通知的作用.