默认情况下是没有开启缓存的,除了局部的 session 缓存,可以增强变现而且处理循环依赖也是必须的。要开启二级缓存,你需要在你的 SQL 映射文件中添加一行:<cache/>这个简单语句的效果如下:
1.映射语句文件中的所有 select 语句将会被缓存。
2.映射语句文件中的所有 insert, update 和 delete 语句会刷新缓存。
3.缓存会使用 Least Recently Used( LRU,最近最少使用的)算法来收回。
4.根据时间表(比如 no Flush Interval,没有刷新间隔),缓存不会以任何时间顺序来刷新
5.缓存会存储列表集合或对象(无论查询方法返回什么)的 1024 个引用。
6.缓存会被视为是 read/write(可读/可写)的缓存,意味着对象检索不是共享的,而且可以安全地被调用者修改,而不干扰其他调用者或线程所做的潜在修改。
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>
可用的收回策略有:
1.LRU:
最近最少使用的:移除最长时间不被使用的对象。默认
2.FIFO:
先进先出:按对象进入缓存的顺序来移除它们。
3.SOFT:
软引用:移除基于垃圾回收器状态和软引用规则的对象。
4.WEAK:
弱引用:更积极地移除基于垃圾收集器状态和弱引用规则的对象。
flushInterval(刷新间隔)可以被设置为任意的正整数,而且它们代表一个合理的毫秒形式的时间段。默认情况是不设置,也就是没有刷新间隔,缓存仅仅调用语句时刷新。
size(引用数目)可以被设置为任意正整数,要记住你缓存的对象数目和你运行环境的可用内存资源数目。默认值是 1024
readOnly(只读)属性可以被设置为 true 或 false。只读的缓存会给所有调用者返回缓存对象的相同实例。因此这些对象不能被修改。这提供了很重要的性能优势。可读写的缓存会返回缓存对象的拷贝(通过序列化)。这会慢一些,但是安全,因此默认是 false。
使用自定义个缓存:
<cache type=”com.domain.something.MyCustomCache”/>
public interface Cache {
String getId();
int getSize();
void putObject(Object key, Object value);
Object getObject(Object key);
boolean hasKey(Object key);
Object removeObject(Object key);
void clear();
ReadWriteLock getReadWriteLock();
}
参照缓存:
<cache-ref namespace=”com.someone.application.data.SomeMapper”/>
动态SQL
1.if
<select id=”findActiveBlogWithTitleLike” parameterType=”Blog” resultType=”Blog”>
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<if test=”title != null”>
AND title like #{title}
</if>
</select>
<select id=”findActiveBlogLike” parameterType=”Blog” resultType=”Blog”>
SELECT * FROM BLOG WHERE state = „ACTIVE‟
<if test=”title != null”>
AND title like #{title}
</if>
<if test=”author != null and author.name != null”>
AND title like #{author.name}
</if>
</select>
2.choose、when、otherwise
<select id=”findActiveBlogLike” parameterType=”Blog” resultType=”Blog”>
SELECT * FROM BLOG WHERE state = „ACTIVE‟
<choose>
<when test=”title != null”>
AND title like #{title}
</when>
<when test=”author != null and author.name != null”>
AND title like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
3.trim、where、set
<select id=”findActiveBlogLike” parameterType=”Blog” resultType=”Blog”>
SELECT * FROM BLOG
<where>
<if test=”state != null”>
state = #{state}
</if>
<if test=”title != null”>
AND title like #{title}
</if>
<if test=”author != null and author.name != null”>
AND title like #{author.name}
</if>
</where>
</select>
where 元素知道如果由被包含的标记返回任意内容,就仅仅插入“ ”。而且,如果以“ ”或“ OR”开头的内容,那么就会跳过 WHERE 不插入。
where 元素没有做出你想要的,你可以使用 trim 元素来自定义。比如,和 where元素相等的 trim 元素是:
<trim prefix="WHERE" prefixOverrides="AND |OR ">
…
</trim>
属性采用管道文本分隔符来覆盖,这里的空白也是重要的。它的结果就是移除在 overrides 属性中指定的内容, 插入在 with 属性中的内容。
set。 set 元素可以被用于动态包含更新的列,而不包含不需更新的。
<update id="updateAuthorIfNecessary" parameterType="domain.blog.Author">
update Author
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
<if test="bio != null">bio=#{bio}</if>
</set>
where id=#{id}
</update>
4.foreach
<select id="selectPostIn" parameterType="list" resultType="domain.blog.Post">
SELECT * FROM POST P WHERE ID in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
</select>