<select id ="FindByCondition" parameterType="User" resultType="user">
select * from user where 1=1
<if test="id!=0">
and id =#{id}
</if>
<if test="username!=null">
and username =#{username}
</if>
<if test="password!=null">
and password =#{password}
</if>
</select>
MyBaits Sql动态查询,其中where 1 =1 在使用中是可以省略的直接把 where标签套在if的外面即可
<select id ="FindByCondition" parameterType="User" resultType="user">
<where>
select * from user
<if test="id!=0">
and id =#{id}
</if>
<if test="username!=null">
and username =#{username}
</if>
<if test="password!=null">
and password =#{password}
</if>
</select>
</where>
//动态sql查询语句
if test中多条件
@Select("<script> select * from users <where> " +
" <if test=\"name != null and name != ''\">\nand name = #{name}</if> " +
"<if test=\"address != null and address != ''\">\nand address = #{address}</if> " +
"<if test=\"email != null and email != ''\">\nand email = #{email}</if>" +
" </where></script>")
List<User> FindUserByUser(User user);