Mybatis条件查询<where>标签

tech2023-07-28  100

Mybatis总结2020https://blog.csdn.net/libusi001/article/details/100066086

一、场景

username为null时, sql为 select * from t_sys_user where and user_phone='' 肯定会报错 select * from t_sys_user where <if test="username != null > user_name= #{userName}) </if> <if test="userPhone!= null> and user_phone= #{userPhone}) </if>

二、解决

1 在where 后面添加一个 1=1 2 使用where标签(mybatis已经提供给我们解决方案了,为什么不去使用呢?)

select * from t_sys_user <where> <if test="username != null > and userName= #{userName}) </if> <if test="nickname != null> and nickName = #{nickName }) </if> </where>

where 元素知道只有在一个以上的if条件有值的情况下才去插入“WHERE”子句。

若最后的内容是“AND”或“OR”开头的,where 元素自动加上或者删除AND OR 这种连接词。

 

 

最新回复(0)