近日使用ibatis,把日常用法记录下来,以备以后用的时候能够再捡起来。ibatis的核心类是SqlMapClient。
1. 直接使用sql语句,用法为:
SqlMapClientImpl.update(String id);
这个可以在不需要接收外界传入参数时的更新语句时使用。
2. 在一个SQL中更新多条记录的时候,用update(String id)效率更高,如:
update td_product t set t.update_type=2, t.is_indexed='0' where t.up_flag = 0
而不能用显得有些浮肿的先创建对象,再获取对象的List,再对List进行批量操作的方法,如果这样,程序的开发速度慢不说,而且调试麻烦,程序的运行速度也低下,是事倍功半的做法。精简快速就是标准,要记住这个。
3. 配置文件示例:
<update id="openProductShow" parameterClass="com.mic.escrow.seller.bean.po.product.ProductBean">
<![CDATA[
update td_product t set t.is_indexed='0'
]]>
<dynamic>
<isNotEmpty property="updateType">
<isEqual property="updateType" compareValue="0" prepend=",">
<![CDATA[UPDATE_TYPE = 3 ]]>
</isEqual>
<isEqual property="updateType" compareValue="1" prepend=",">
<![CDATA[UPDATE_TYPE = 2 ]]>
</isEqual>
</isNotEmpty>
</dynamic>
<![CDATA[
where t.CHECKEDPROD = 0 and t.PRODUCTSTATUS = '0'
]]>
</update>
这个语句的作用就是根据传出的参数updateType的值,动态执行SQL语句,当updateType的值为0时,执行条件update_type=3,当值为1时,执行条件update_type=2, prepend是语句的前缀。
当传入list参数时,可以使用这样的语句:
<isNotNull property="emailFrom" prepend="and" removeFirstPrepend="true">
<iterate property="emailFrom" open="(" close=")" conjunction="or" >
<![CDATA[ email_from = #emailFrom[]#]]>
</iterate>
</isNotNull>
iterate用于迭代list中的值,这个语句用于item in (condition1,condition2)的场景,或者item=condition1 or item=condition2这样的情形。
ibatis使用技巧
1710

被折叠的 条评论
为什么被折叠?



