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!!!