Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamic query error in ibatis

I got an error with a dynamic query. This is the query that 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 FirstName is not null, the query works. But if the property is null and the other left property LastName is not null, I get a SQL error.

Because in this condition the SQL statement becomes as follow:

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

How can I remove the first-prepended AND if the first property becomes Null

P.S:

I also tried removeFirstPrepend="true"

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

But unfortunately, it doesn't work either.

like image 627
AKZap Avatar asked Sep 05 '25 03:09

AKZap


1 Answers

try with

<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

like image 197
John Wa Ha Avatar answered Sep 07 '25 22:09

John Wa Ha