目录:
学习笔记零 - 起源
学习笔记一 - 概念与入门
学习笔记二 - 基础配置
学习笔记四 - 技巧与实践
高级特性:使用高速缓存提高性能,iBATIS数据访问对象,DAO使用进阶,扩展iBATIS
使用高速缓存提高性能
内容
- 高速缓存理念
- 高速缓存配置
- 高速缓存策略
1 一个简单的iBATIS高速缓存示例
iBATIS的高速缓存机制完全基于配置的。
<cacheModel id="categoryCache" type="MEMORY">
<flushOnExecute statement="insert"/>
<flushOnExecute statement="update"/>
<flushOnExecute statement="delete"/>
<property name="reference-type" value="WEAK"/>
</cacheModel>
<select id="getCategory" parameterClass="Category" resultClass="Category" cacheModel="categoryCache">
SELECT *
FROM Category
WHERE categoryId=#categoryId#
</select>
MEMORY:高速缓存类型,直接将查询结果存储在内存中。
flushOnExecute:当某个特定高速缓存被访问时,其存储结果将被清除。
2 iBATIS高速缓存的理念
高速缓存通常用来存储那些长效的、不会发生改变的数据。高速缓存也可以用于那些可修改的对象。
此处的难点是,需要保证请求的数据是否存在于当前高速缓存中,如果不存在,需要自己将它保存到高速缓存中。需要保证高速缓存中的数据是否已经过时。
iBATIS的思想是建立SQL语句到对象间的映射,而不是数据库表到对象间的映射。
3 高速缓存模型
高速缓存模型时一种高速缓存配置。高速缓存通过标签<cacheModel>定义,包含以下属性:
- id : 必须,唯一ID用来调用。
- Type:必须,高速缓存类型,MEMORY、LRU、FIFO和OSCACHE。
- readOnly:可选。
- serialize:可选,表示是否进行深复制。
3.1 type属性
iBATIS提供了4个默认的高速缓存实现
- MEMORY : 将缓存数据保存在内存中,直到垃圾收集器将它移除。
- FIFO: 这个模型中,高速缓存的数据量是固定的,使用先进先出(first in first out)算法来移除高速缓存中的数据。
- LRU:这个模型的数据量也是固定的,使用最近最少使用算法来移除高速缓存中的数据(least recently used)。
- OSCACHE:这个模型使用OpenSymphony高速缓存
3.2 readOnly属性
该属性仅仅是一个为高速缓存模型提供指令的指示器,用于告诉高速缓存模型应该如何检索和保存已高速缓存对象。当设置为true时,那高速缓存模型就会返回某个对象引用。
3.3 serialize属性
serialize属性用于指示高速缓存对象该如何返回,当属性为true时,高速缓存所请求的每个对象都将作为一个深副本返回。
3.4 联合使用readOnly属性和serialize属性
readOnly | serialize | 结果 | 原因 |
---|---|---|---|
True | False | 好 | 可以快速地检索出已高速缓存的对象。返回一个共享的实例。 |
False | True | 好 | 返回一个缓存对象的深副本 |
False | False | 警告 | 高速缓存仅仅同调用线程会话的生命周期有关,且不能被其他线程调用。 |
True | Ture | 坏 | 同False,True组合一样。 |
4 如何使用高速缓存模型中的标签
4.1 高速缓存的清除
高速缓存具有两个标签 <flushOnExecute> 和<flushInterval>标签
<flushOnExecute>标签
定义查询已映射语句,其执行将引起高速缓存的清除。
<sqlMap namespace="Category">
…
<cacheModel id="categoryCache" type="MEMORY">
…
<flushOnExecute statement="Category.insert"/>
…
</cacheModel>
…
<select id="getCategory" parameterClass="Category" resultClass="Category" cacheModel="categoryCache">
SELECT *
FROM Category
WHERE parentCategoryId=#categoryId#
</select>
…
<insert id="insert" parameterClass="Category" >
INSERT INTO Category
(title,description,sequence)
VALUES
(#title#,#description#,#sequence#)
</insert>
…
</sqlMap>
<flushInterval>标签
定义一个时间间隔,高速缓存将以此间隔定期清除
属性:hours,minutes,seconds,millseconds,都是可选项。
示例:
<sqlMap namespace="Category">
…
<cacheModel id="categoryCache" type="MEMORY">
…
<flushInterval hours= "12" />
…
</cacheModel>
…
<select id="getCategory" parameterClass="Category" resultClass="Category" cacheModel="categoryCache">
SELECT *
FROM Category
WHERE parentCategoryId=#categoryId#
</select>
…
</sqlMap>
4.2 设置高速缓存模型实现的特性
高速缓存模型是iBATIS框架中的组件,他允许用户自己定制,因此必须有一种方式能够为这些组件提供任意的值。<property>标签就是用来完成此任务的。
<property>标签的属性:Name,value
5 高速缓存模型的类型
高速缓存模型具备MEMORY,LRU,FIFO,OSCACHE
5.1 MEMORY
MEMORY高速远程基于引用,具备WEAK,SOFT,STRONG三种引用类型。
例子:
<cacheModel id="categoryCache" type="MEMORY">
<flushInterval hours="24"/>
<flushOnExecute statement="insert"/>
<flushOnExecute statement="update"/>
<flushOnExecute statement="delete"/>
<property name="reference-type" value="WEAK"/>
</cacheModel>
5.2 LRU
LRU类型的高速缓存使用最近最少使用策略来管理高速缓存。
LRU的propery标签唯一能指定的是size,用来指定高速缓存中的对象的最大数目。
<cacheModel id="categoryCache" type="LRU">
<flushInterval hours="24"/>
<flushOnExecute statement="insert"/>
<flushOnExecute statement="update"/>
<flushOnExecute statement="delete"/>
<property name="size" value="200"/>
</cacheModel>
LRU高速缓存对于那些数据的不同子集(subsets of data)都用在某段时间内的应用程序非常有用
5.3 FIFO
FIFO高速缓存模型采用先进先出的管理策略。唯一需要指定的property也是size。
<cacheModel id="categoryCache" type="FIFO">
<flushInterval hours="24"/>
<flushOnExecute statement="insert"/>
<flushOnExecute statement="update"/>
<flushOnExecute statement="delete"/>
<property name="size" value="1000"/>
</cacheModel>
5.4 OSCACHE
该高速缓存模型采用了OpenSymphony公司的产品OSCache2.0,需要JAR文件依赖以及配置文件。
5.5 自己的高速缓存模型
需要了解两点,1,高速缓存模型实际上都是CacheController接口的实现。
2,the names are type aliases that map to the fully qualified names of those implementations.
6 确定高速缓存策略
当需要缓存只读的长效数据时,我们可以使用LRU策略。比如购物车的类别。
如果需要缓存的对象易变,就要好好考虑是否使用高速缓存技术,如果需要使用,可以用MEMORY模式,并且设置reference-type为WEAK
旧的静态数据,比如统计数据,可以用FIFO模式
iBATIS数据访问对象
内容
- DAO基本原理
- 配置
- SQLMapDAO示例
DAO模式用于隐藏API格子的独特实现。
1 iBATIS DAO的简单示例
iBATIS Dao:
![Alt text](./iBATIS DAO.png)
1.1 Dao.xml 配置文件
要配置DaoManager,必须从一个XML配置文件开始,该文件通常被命名为dao.xml
dao.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE daoConfig
PUBLIC
"-//ibatis.apache.org//DTD DAO Configuration 2.0//EN"
"http://ibatis.apache.org/dtd/dao-2.dtd">
<daoConfig>
<context id="example">
<transactionManager type="SQLMAP">
<property name="SqlMapConfigResource" value="examples/SqlMapConfig.xml"/>
</transactionManager>
<dao interface="examples.dao.AccountDao" implementation="examples.dao.impl.AccountDao"/>
</context>
</