<if test="mwCodeList != null">
and t.CODE in
<choose>
<when test="mwCodeList.size == 0">
(null)
</when>
<when test="mwCodeList.size > 0">
<foreach collection="mwCodeList" item="item" open="(" close=")" separator=",">
'${item}'
</foreach>
</when>
</choose>
</if>
如果使用mybaits-plus,框架就自动帮我们解决了这个小问题
/**
* 字段 IN (value.get(0), value.get(1), ...)
* <p>例: in("id", Arrays.asList(1, 2, 3, 4, 5))</p>
*
* <li> 如果集合为 empty 则不会进行 sql 拼接 </li>
*
* @param condition 执行条件
* @param column 字段
* @param coll 数据集合
* @return children
*/
Children in(boolean condition, R column, Collection<?> coll);
/**
* ignore
*/
default Children in(R column, Object... values) {
return in(true, column, values);
}
/**
* 字段 IN (v0, v1, ...)
* <p>例: in("id", 1, 2, 3, 4, 5)</p>
*
* <li> 如果动态数组为 empty 则不会进行 sql 拼接 </li>
*
* @param condition 执行条件
* @param column 字段
* @param values 数据数组
* @return children
*/
default Children in(boolean condition, R column, Object... values) {
return in(condition, column, Arrays.stream(Optional.ofNullable(values).orElseGet(() -> new Object[]{}))
.collect(toList()));
}
/**
* ignore
*/
default Children notIn(R column, Collection<?> coll) {
return notIn(true, column, coll);
}
网友评论