Let's say I have an interface look like this.
@Mapper
interface UserMapper {
List<User> search(@Nullable String namePattern, int minAge);
}
How can I do with the minAge
parameter which is not required the <if/>
element?
Should I just do with <if test="true"/>
?
<where>
<if test="namePattern != null">
name LIKE #{namePattern}
</if>
<if test="true"> <!-- just true??? -->
AND age >= #{minAge}
</if>
</where>
Can I peel the second <if/>
element?
<where>
<if test="namePattern != null">
name LIKE #{namePattern}
</if>
AND age >= #{minAge} <!-- will it blend? -->
</where>
Sure. <where />
removes unnecessary AND
, so it should work.
Another approach is to write the age
condition in front of <if />
s.
WHERE age >= #{minAge}
<if test="namePattern != null">
AND name LIKE #{namePattern}
</if>