<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);
本文详细介绍了在MyBaits中如何使用动态SQL进行灵活的数据查询。通过示例展示了如何根据参数条件动态生成SQL语句,包括单条件和多条件查询,以及如何使用where标签优化查询语句。
3905

被折叠的 条评论
为什么被折叠?



