Mybatis xml中in的用法

发布时间 2023-10-13 11:49:29作者: MarchXD

一、前端多选 传字符串类型给后端,用逗号(,)分隔开

后端用String类型接收该字段

/**
 *  所属部门编码list
 */
@ApiModelProperty(name = "departmentCodeList", value = "所属部门编码集")
private String departmentCodeList ;

mybatis中写法如下:

<if test="vo.departmentCodeList != null and vo.departmentCodeList != ''">
	AND r.department_code in
	<foreach item="departmentCode" collection="vo.departmentCodeList.split(',')" open="(" separator="," close=")">
    	#{departmentCode}
    </foreach>
</if>

二、前端用list形式传给后端

后端用List形式接收

@ApiModelProperty(name = "departmentCodeLists", value = "所属部门编码集")
private List<String> departmentCodeLists ;

mybatis中写法:

<if test="vo.departmentCodeLists != null and vo.departmentCodeList.size>0">
	AND r.department_code in
	<foreach collection="vo.departmentCodeLists" item="departmentCode" index="index" open="(" close=")" separator=",">
    	#{departmentCode}
	</foreach>
</if>

注意:在接受list的时候不能加该判断: list!=‘’ , 换成判断list.size>0