Mybatis-plus中动态SQL<where>、<if>条件判断的and写法
用where和if进行组合,当条件不成立时,if条件后的内容包括and会存在,因此会对整个sql语句产生影响写法一每个if判断前添加and时,在if前添加1 = 1<select id="select" resultType="com.tzh.bean.Entity">select * from departmentwhere 1=1<if test="id!=null">
·
用where和if进行组合,当条件不成立时,if条件后的内容包括and会存在,因此会对整个sql语句产生影响
写法一:
每个if判断前添加and时,在if前添加1 = 1
<select id="select" resultType="com.tzh.bean.Entity">
select * from department
where 1=1
<if test="id!=null">
and id=#{id}
</if>
<if test="name!=null and name!=''">
and t_name like #{name}
</if>
<if test="email!=null and email.trim()!=''">
and email=#{email}
</if>
</select>
写法一:
每个if判断后添加and时,在if判断后添加1 = 1
<select id="select" resultType="com.tzh.bean.Entity">
select * from department
where
<if test="id!=null">
id=#{id} and
</if>
<if test="name!=null and name!=''">
t_name like #{name} and
</if>
<if test="email!=null and email.trim()!=''">
email=#{email} and
</if>
1=1
</select>
建议方式:
使用转义符
<select id="select" resultType="com.tzh.bean.Entity">
select * from department
where
<if test="id!=null">
id=#{id}
</if>
<if test="name!=null && name!=""">
and t_name like #{name}
</if>
<if test="email!=null and email.trim()!=""">
and email=#{email}
</if>
</where>
附上转义符表
更多推荐
已为社区贡献1条内容
所有评论(0)