方式一:前台传参为数组,后台SQ查询案例:
一般为多选场景:查询;
举例如下:
传值:“status” : [“保存”,“关闭”],
不传值:“status”: [],
传给后台:
控制层:
@PostMapping(value = "/getList")
@AuthInterceptor("mcg:getxxx:getList")
public Result getList(HttpServletRequest request,@RequestBody String param) {
try {
Map<String, Object> paramMap = JsonUtils.readValue(param, Map.class);
return Result.success().result(xxxService.getList(request,paramMap));
} catch (Exception e) {
log.error("XxxListController getList is error===:" + e.getMessage(), e);
return Result.failure("查询失败");
}
}
Mapper.xml层:
<!-- status:传数组 -->
<if test = "null != status and status.size > 0">
and status in
<foreach collection="status" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
方式二:前台传参为拼接字符串,后台SQ查询案例:
举例如下:
传值:“status”: “保存,关闭”,
不传值:“status”: “”,
后台需要针对拼接的字符串做拆分;如:参数通过英文逗号“,”拼接;
如下代码:
// 字符串转集合输出
private List<String> getSplitList(String par){
List<String> list = new ArrayList<>();
if("" != par){
String[] arr = par.split(","); // 根据英文逗号拆分
for (int i = 0; i < arr.length; i++) {
list.add(arr[i]);
}
}
return list;
}
public PageInfo<Map<String, Object>> getList(Map<String, Object> paramMap) {
List<String> statusList = getSplitList(String.valueOf(paramMap.get("status")));
paramMap.put("statusList",statusList);
List<Map<String, Object>> result = xxxListMapper.getList(paramMap); // 表单查询数据
}
<!-- status:传集合 -->
<if test = "null != statusList and statusList.size > 0">
and status in
<foreach collection="statusList" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
总结:以上两种方式都可以解决多条件查询场景需求;