ibatis映射文件的书写:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="User">
<!-- 定义该映射文件的Cache机制 -->
<cacheModel type="LRU" id="userCache">
<!-- 设定缓存存活的时间 -->
<flushInterval hours="24"/>
<!-- 设定指定的操作,清空缓存 -->
<flushOnExecute statement="updateUser"/>
<!-- 设定缓存的容量(对象) -->
<property name="size" value="1000"/>
</cacheModel>
<!-- 定义别名 -->
<typeAlias alias="user" type="com.wyq.pojo.User"/>
<!-- 通过缓存可以提高查询效率 -->
<select id="getUsers"
parameterClass="java.lang.Integer"
resultClass="user" cacheModel="userCache">
<!-- 采用!【DATA[]】格式,是为了避免sql重的字符与xml映射文件的合法性造成影响 -->
<![CDATA[
select id,name,sex from t_user
]]>
</select>
<update id="updateUser" parameterClass="user">
<![CDATA[
update t_user set name=#name#,sex=#sex# where id=#id#
]]>
</update>
<insert id="insertUser" parameterClass="user">
insert into t_user(name,sex) values(#name#,#sex#)
</insert>
<delete id="deleteUser" parameterClass="java.lang.String">
delete from t_user where id=#value#
</delete>
</sqlMap>
<statement id="statementName"
[parameterClass="some.class.Name"] //表示输入的参数类型为Class
[resultClass="some.class.Name"] //表示输出的参数类型为Class
[parameterMap="nameOfParameterMap"] //表示输入的参数类型为Map
[resultMap="nameOfResultMap"] //表示输出的参数类型为Class
[cacheModel="nameOfCache"]
>
select * from t_user where sex = [?|#propertyName#]
order by [$simpleDynamic$]
</statement>
1、对于参数定义,尽量使用parameterClass,即直接将pojo作为statement的调用参数,如果输入的参数是java.util.map,那么map中的键值将作为输入参数,普通基本类型就是直接输入。
2、对于返回结果而言,尽量也使用resultClass,直接将字段名与pojo相对应。
3、而parameterMap与resultMap实现了pojo到数据库字段的映射配置,parameterMap 使用较少,而resultMap 则大多用于嵌套查询以及存储过程的。
<resultMap id="get_user_result" class="user">
<result property="name" column="xingming"
jdbcType="VARCHAR" javaType="java.lang.String"/>
<result property="sex" column="xingbie"
jdbcType="int" javaType="java.lang.Integer"/>
<result property="id" column="id"
jdbcType="int" javaType="java.lang.Integer"/>
</resultMap>
<parameterMap id="update_user_para" class="redemption" >
<parameter property="name"
jdbcType="VARCHAR"
javaType="java.lang.String"
nullValue=""
/>
<parameter property="sex"
jdbcType="int"
javaType="java.lang.Integer"
nullValue=""
/>
</parameterMap>
<procedure id="getUserList"
resultMap="get_user_result"
>
{call sp_getUserList()}
</procedure>
<procedure id="doUserUpdate"
parameterMap="update_user_para"
>
{call sp_doUserUpdate(#id#,#name#,#sex#)}
</procedure>
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
<statement id="statementName"
[parameterClass="some.class.Name"] //表示输入的参数类型为Class
[resultClass="some.class.Name"] //表示输出的参数类型为Class
[parameterMap="nameOfParameterMap"] //表示输入的参数类型为Map
[resultMap="nameOfResultMap"] //表示输出的参数类型为Class
[cacheModel="nameOfCache"]
>
select * from t_user where sex = [?|#propertyName#]
order by [$simpleDynamic$]
</statement>
1、对于参数定义,尽量使用parameterClass,即直接将pojo作为statement的调用参数,如果输入的参数是java.util.map,那么map中的键值将作为输入参数,普通基本类型就是直接输入。
2、对于返回结果而言,尽量也使用resultClass,直接将字段名与pojo相对应。
3、而parameterMap与resultMap实现了pojo到数据库字段的映射配置,parameterMap 使用较少,而resultMap 则大多用于嵌套查询以及存储过程的。
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)