MyBatis 查询语句优化:如何只显示 state 为 1 的数据
要让查询结果只显示 state 为 1 的数据,可以在 <where> 标签内添加条件判断:
<where>
<if test="staffname != null and staffname.length != 0">
staff.staffname LIKE CONCAT('%', #{staffname}, '%')
</if>
<if test="branchname != null and branchname.length != 0">
branch.branchname LIKE CONCAT('%', #{branchname}, '%')
</if>
<if test="deptname != null and deptname.length != 0">
department.deptname LIKE CONCAT('%', #{deptname}, '%')
</if>
<if test="state != null and state == 1">
AND staff.state = 1
</if>
</where>
在 <if> 标签中添加一个条件判断,判断是否传入了 state 参数,并且判断 state 是否等于 1,如果满足条件,则添加一个额外的条件语句 AND staff.state = 1。
这样就可以限制查询结果只显示 state 为 1 的数据。
原文地址: http://www.cveoy.top/t/topic/hGnj 著作权归作者所有。请勿转载和采集!