If Conditionals in Java Select Tag

13 views
Skip to first unread message

Some User

unread,
Jun 10, 2024, 10:23:18 AMJun 10
to mybatis-user
In XML versions of mybatis for java, you could do a query with an if statement embedded inside a Select query like this:

<select id="getUsers">
   SELECT * FROM user
   WHERE
   1 = 1
   <isNotNull property="firstName"> AND firstName = #firstName#</isNotNull>
   <isNotNull property="lastName"> AND lastName = #lastName#</isNotNull>
</select>

I'm trying to write this same query using the java @Select tag instead of the xml tags, but I'm not sure what to put in the @Select to conditionally filter on firstName/lastName. Currently, I have:

@Select("Select * from user where 1 = 1 ?????????")
public List<User> getUsers(@Param("firstName") String firstName, @Param("lastName") String lastName);

I'm not sure what I put in the ????? part of the @Select annotation. Since firstName and lastName are optional search parameters, I can't do:

@Select("Select * from user where 1 = 1 and firstName = #{firstName} and lastName = #{lastName}")

So is there an if conditional that I could put into the select tag to only conditionally include the first or last name in the query? Users should be able to search for either firstName alone OR lastName alone OR for both firstName and lastName.

Note - I am using the Spring Boot integration if that changes the answer at all.

Thanks for your help!!!

Iwao AVE!

unread,
Jun 10, 2024, 10:39:03 AMJun 10
to mybati...@googlegroups.com
Hello,

You need to use <script> in annotation.

Regards,
Iwao

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/02248db2-fe63-4bbc-9163-2625b1ccc3ccn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages