优化MyBatis中SQL语句:使用`foreach`和`concat()`函数提高效率
优化后的SQL如下:
<when test="key != null and key == 'l3flag' ">
<if test="!param.canSeachl3Flag()">
<if test="!value.contains('')">
and (
<foreach collection="value" separator="or" item="item">
issue.l3_flag like concat('%', #{item}, '%')
</foreach>
)
</if>
<if test="!value.contains('')">
and (issue.l3_flag like '%是%否%' or issue.l3_flag like '%否%是%')
</if>
</if>
<if test="!value.contains('')">
and issue.l3_flag is null
</if>
</when>
优化说明:
- 将
issue.l3_flag like <foreach>中的括号移到foreach标签的open和close属性中。 - 使用
concat()函数代替字符串拼接操作。 - 将
or连接的条件放在一个括号内,避免出现优先级问题。 - 将重复的条件判断
!param.canSeachl3Flag()提取到外层if中,避免重复判断。
原文地址: https://www.cveoy.top/t/topic/qDI5 著作权归作者所有。请勿转载和采集!