Dynamic query error in ibatis

I got an error with a dynamic query. This is the query I have:

<dynamic>
    select * from Tbl_Member
    WHERE

    <isNotNull property="FirstName">
      FIRST_NAME = #lastName#
    </isNotNull>

    <isNotNull prepend="and" property="lastName">
      LAST_NAME = #lastName#
    </isNotNull>

    <isNotNull prepend="and" property="">

    </isNotNull>

     .
     .
     .
     .

</dynamic>

If the property is FirstNamenot null, the request works. But if the property is null and the other left property is LastNamenot null, I get an SQL error.

Since in this condition the SQL statement is as follows:

  select * from Tbl_Member WHERE AND LAST_NAME = ? ...

How to remove the first added ANDif the first property becomesNull

PS:

I also tried removeFirstPrepend="true"

<isNotNull prepend="and" property="lastName" removeFirstPrepend="true">
  LAST_NAME = #lastName#
</isNotNull>

But, unfortunately, it does not work either.

+3
source share
1 answer

try

<dynamic prepend="WHERE">

select * from Tbl_Member
<dynamic prepend="WHERE">

<isNotNull prepend="and" property="FirstName">
  FIRST_NAME = #lastName#
</isNotNull>

<isNotNull prepend="and" property="lastName">
  LAST_NAME = #lastName#
</isNotNull>

<isNotNull prepend="and" property="">

</isNotNull>

 .
 .
 .
 .

</dynamic>

http://ibatis.apache.org/docs/dotnet/datamapper/ch03s09.html

+3
source

All Articles