SqlMapConfig.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<!--cacheModelsEnabled = 启用SqlMapClient上的缓存机制-->
<!--classInfoCacheEnabled = 保存缓存类,减少类的重用-->
<!--statementCachingEnabled = iBATIS 2.3.0以后版本中有 ,启动预处理缓存机制-->
<!--enhancementEnabled = 针对POJO启用字节码增强机getter/setter的调用效能-->
<!--lazyLoadingEnabled = 启用延迟加载机制-->
<!--useStatementNamespaces = 使用Statement命名空间-->
<!--errorTracingEnabled = 启用错误日志,方便开发阶段调试-->
<!--maxRequests = 最大并发请求数(Statement并发数)-->
<!--maxSessions = 最大Session数。即当前最大允许的并发SqlMapClient数-->
<!--maxTransactions = 最大并发事务数-->
<settings
cacheModelsEnabled="true"
classInfoCacheEnabled="true"
statementCachingEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
useStatementNamespaces="true"
errorTracingEnabled="true"
/>
<sqlMap resource="com/caiyl/src/dao/student.xml"/>
</sqlMapConfig>
student.xml:
<sqlMap namespace="student">
<typeAlias alias="student" type="com.caiyl.Student" />
<!-- 配置缓存模型 -->
<cacheModel id="stu-cache" type="OSCache" readOnly="true"
serialize="true">
<!-- 设定缓存存活的时间 -->
<flushInterval hours="24" />
<!-- 指定DML操作,清空缓存 -->
<flushOnExecute statement="insertStu" />
<flushOnExecute statement="updateStu" />
<flushOnExecute statement="deleteStu" />
<!-- 缓存的容量(对象) -->
<property value="500" name="size" />
</cacheModel>
<resultMap id="StudentMap" class="student">
<result property="id" column="ID" />
<result property="name" column="NAME" />
</resultMap>
<!-- cacheModel指定使用的缓存id -->
<select id="selectAllUser" resultMap="StudentMap" cacheModel="stu-cache">
select * from student
</select>
<insert id="insertStu" parameterClass="student">
insert into student(Id,name) VALUES (#id#, #name#)
</insert>
.......
</sqlMap>
此时ibatis相同的sql只会执行一次