Search code examples
ibatismybatis

ibatis dynamic sql using two conditions


I would like to use a dynamic sql statement that executes only when the variable is not null AND greater than zero. Like this:

<isNotNull prepend="AND" property="ProprietaryId">
    <isGreaterThan prepend="AND" property="ProprietaryId" compareValue="0">
        G.PROPRIETARY_ID = #ProprietaryId#
    </isGreaterThan>
</isNotNull>

but without prepending two 'AND's.

I have read the documentation but have found no good example.


Solution

  • To work around to this issue I almost never use the "prepend" feature, but instead write an sql like this:

    WHERE 1=1
    <isNotNull property="ProprietaryId">
        <isGreaterThan property="ProprietaryId" compareValue="0">
        AND G.PROPRIETARY_ID = #ProprietaryId#
        </isGreaterThan>
    </isNotNull>