select * from user
<trim prefix="WHERE" prefixOverride="AND |OR">
<if test="userName != null and userName.length()>0"> AND user_name=#{userName}</if>
<if test="loginName != null and loginName.length()>0"> AND login_name=#{loginName}</if>
</trim>
如果userName 为空则最终SQL为:
select * from user where login_name = 'xx'
prefix:前缀
prefixOverride:去掉第一个and或者是or
2、
update user
<trim prefix="set" suffixOverride="," suffix=" where user_id = #{userId} ">
<if test="userName != null and userName.length()>0"> user_name=#{userName} , </if>
<if test="loginName != null and loginName.length()>0"> login_name=#{loginName} , </if>
</trim>
如果userName 为空则最终SQL为:
update user set login_name='xx' where user_id='xx'