Archive for February, 2010

Reusing Conditions in iBatis

February 15, 2010

One of the major outcome when the software industry got matured is the concept of reusage. Anything can be reused, components, methods, anything. Similarly I just came across the feature in iBatis where we can reuse the conditions in the where clause. Its very simple and reduce the size of the sqlmap file.

<select id=”selectValue”
resultMap=”ValueEntityResultGM” cacheModel=”data-cache”>
select * from VALUE_NUM
WHERE VALUE IS NOT NULL
<include refid=”idset” />
</select>

The include tag with attribute refid points to a condition saved with the name idset. This will be stored as below

<sql id=”idset-params-gm”>

<isNotNull prepend=”AND” property=”scaleid”>
scaleid = #scaleid#
</isNotNull>
<isNotEmpty prepend=”AND” property=”scaleid_list” open=”(” close=”)”>
<iterate conjunction=”OR” property=”scaleid_list”>
scaleid = #scaleid_list[]#
</iterate>
</isNotEmpty>
<isNotNull prepend=”AND” property=”ownerid”>
ownerid = #ownerid#
</isNotNull>

</sql>

The similar way this condition can be used in any number of queries.

When I keep on exploring iBatis, this is becoming more and more fun. Enjoy Coding. 🙂