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
虚引用要和引用队列一起使用,垃圾回收线程回收该线程时,会发送一个系统通知,达到通知的作用.

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
老生常谈 String、StringBuilder、StringBuffer
[TOC] 字符串就是一连串的字符序列,Java提供了String、StringBuilder、StringBuffer三个类来封装字符串 String String类是不可变类,String对象被创建以后,对象中的字符序列是不可改变的,直到这个对象被销毁 为什么是不可变的 jdk1.8 public final class String implements java.io.Serializable, Comparable<String>, CharSequence { /** The value is used for character storage. */ private final char value[]; //jdk1.9中将char数组替换为byte数组,紧凑字符串带来的优势:更小的内存占用,更快的操作速度。 //构造函数 public String(String original) { this.value = original.value; this.hash = original.hash; } //构造函数 public String(char v...
- 下一篇
python实现归并算法
归并排序是采用分治法的一个非常典型的应用,另一个可以采用分治法的是快速排序,归并算法比快速排序速度稍低。归并排序的思想就是先递归分解数组,再合并数组。 将数组分解最小之后,然后合并两个有序数组,基本思路是比较两个数组的最前面的数,谁小就先取谁,取了后相应的指针就往后移一位。然后再比较,直至一个数组为空,最后把另一个数组的剩余部分复制过来即可。 如 设有数列{6,202,100,301,38,8,1} 初始状态:6,202,100,301,38,8,1 第一次归并后:{6,202},{100,301},{8,38},{1},比较次数:3; 第二次归并后:{6,100,202,301},{1,8,38},比较次数:4; 第三次归并后:{1,6,8,38,100,202,301},比较次数:4; 总的比较次数为:3+4+4=11; 逆序数为14; pyt
相关文章
文章评论
共有0条评论来说两句吧...