Question

I have an iBatis query like

<select id="filterQuery" resultMap="guideline" parameterClass="filter">
   <![CDATA[
       SELECT * FROM TABLE_NAME
       WHERE distance BETWEEN #lowerLimit AND #upperLimit 
       AND max_distance BETWEEN #maxLowerLimit AND #maxUpperLimit
       AND region=#state
   ]]>
</select>

But fields like- lowerLimit, upperLimit, maxLowerLimit, maxUpperLimit and state could be null in which case, WHERE clause on the corresponding column should be ignored.

I know that I can use isNotNull to achieve the same. But I was wondering if something like this with an AND operation is allowed in order to validate both the fields.

<isNotNull property="lowerLimit" &&  property="upperLimit">

Or else how should I do the same?

Was it helpful?

Solution

You cannot use an AND within the <isNotNull> element. Instead, try something like this:

<select id="filterQuery" resultMap="guideline" parameterClass="filter">
    SELECT * FROM TABLE_NAME
    <dynamic prepend="WHERE">
       <isNotNull property="lowerLimit" prepend="and">
           distance <![CDATA[>=]]> #lowerLimit#
       </isNotNull>

       <isNotNull property="upperLimit" prepend="and">
           distance <![CDATA[<=]]> #upperLimit
       </isNotNull>

       <isNotNull property="maxLowerLimit" prepend="and">
           max_distance <![CDATA[>=]]> #maxLowerLimit#
       </isNotNull>

       <isNotNull property="maxUpperLimit" prepend="and">
           max_distance <![CDATA[<=]]> #maxUpperLimit#
       </isNotNull>

       <isNotNull property="state" prepend="and">
           region = #state#
       </isNotNull>
    </dynamic>
</select>

The <dynamic> element will include the WHERE clause only if at least one of the <isNotNull> elements produces output.

OTHER TIPS

You can use prepend="AND" attribute in <isNotNull> to add more constraints.

There are some useful examples in documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top