接口在传多个参数时
可能遇到映射文件无法获得传入的参数
这里展示一个测试多个参数接口方法
我采用的方法是将多个参数放入Map里
无需再更改接口名注释
大家可以看下做下参考(主要还是自己记录 防止忘记)
controller层
@GetMapping(value = "/getFan")
public AjaxResult getFan(@RequestParam(name = "ids", required = false) Long[] ids,
@RequestParam(name = "stagingIds", required = false) Long[] stagingIds,
@RequestParam(name = "circuitIds", required = false) Long[] circuitIds,
@RequestParam(name = "simplifyCode", required = false) String[] simplifyCode) {
Map<String, Object> map = new HashMap<>();
if (ids != null && ids.length > 0) {
map.put("ids", ids);
}
if (stagingIds != null && stagingIds.length > 0) {
map.put("stagingIds", stagingIds);
}
if (circuitIds != null && circuitIds.length > 0) {
map.put("circuitIds", circuitIds);
}
/**
* mybatis多个参数用map接收
*/
List<BasicFan> fanByIds = basicFanService.getCustomStandard(map);
return AjaxResult.success(fanByIds);
}
service层
List<BasicFan> getCustomStandard(Map<String, Object> map);
impl层
@Override
public List<BasicFan> getCustomStandard(Map<String, Object> map) {
return basicFanMapper.getCustomStandard(map);
}
mapper文件
<select id="getCustomStandard" resultMap="BasicFanResult">
select ....省略
<where>
<if test="ids != null ">
and bf.id in
<foreach item="id" collection="ids" open="(" separator="," close=")">
#{id}
</foreach>
</if>
<if test="stagingIds != null ">
and bf.staging_id in
<foreach item="id" collection="stagingIds" open="(" separator="," close=")">
#{id}
</foreach>
</if>
<if test="circuitIds != null ">
and bf.circuit_id in
<foreach item="id" collection="circuitIds" open="(" separator="," close=")">
#{id}
</foreach>
</if>
</where>
</select>
controller层写好后 主要是mapper文件的格式
map接收的参数名必须和collection里参数名一样 否则也会找不到参数!
|