需求:字段中为一个数组,前端下拉选择多个code列表来查询
分析:如果是单字段的可以使用in 查询 ,如果mysql 的版本为5.7以上的话可以使用JSON函数查询,由于本地数据库版本为5.6,不支持JSON函数,所以使用like查询
因为下拉的code是一个并集,所以使用OR查询,使用到<foreach>标签
默认代码<foreach>一般为
<if test="query.brCode != null and query.brCode.size() > 0">
and (
<foreach collection="query.brCode" index="index" item="item">
or rr.b_list LIKE CONCAT('%',#{item},'%')
</foreach>
)
</if>
这样之后我们需要取消第一个or 标签,想到idnex 的作用
最终实现:
<if test="query.brCode != null and query.brCode.size() > 0">
and (
<foreach collection="query.brCode" index="index" item="item">
<if test="index != 0 ">
or
</if>
br_list LIKE CONCAT('%',#{item},'%')
</foreach>
)
</if>
遇到问题:
mybaties java.lang.NumberFormatException: For input string: "{0=null}"
由于刚开始使用了#{index}导致