@PostMapping("/test/getByArray")
public ResponseBO<List<String>> getByArray(@RequestParam String ids){
log.debug("REST request to get testData");
List<String> idList = null;
if (ids!=null){
idList = Arrays.asList(ids.split(","));
}
List<String> list1 = tVulAssetsService.getByArray(idList);
return new ResponseBO<>(list1,SUCCESS);
}
postman 示例如下:
这里dao层不加@Param,会报错
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘idList’ not found. Available parameters are [collection, list]
List<String> getByArray(@Param("idList") List<String> idList);
sql语句
<select id="getByArray" parameterType="list" resultType="string">
select vul_id from t_vul_assets
<where>
<if test="idList!=null and idList.size>0">
id in
<foreach close=")" collection="idList" index="index" item="item" open="(" separator=",">
#{item}
</foreach>
</if>
</where>
</select>
~
(后来百度查了查,dao层里的参数为List类型的话,
你可以传递一个 List 实例或者数组作为参数对象传给 MyBatis。当你这么做的时 候,MyBatis 会自动将它包装在一个 Map 中,用名称在作为键。List 实例将会以“list” 作为键,而数组实例将会以“array”作为键。)
加上@Param就是让mybatis知道这个key叫"idList"了
这个前端传一个","号隔开的大字符串还是有点不够灵活,虽然这样避免了get请求中,url长度有限制的问题,且看另一种方法:
@PostMapping("/tVulAssetss/getByArray")
public ResponseBO<List<String>> getByArray(@RequestBody Map<String,List<String>> listData){
log.debug("REST request to get testData");
List<String> list1 = tVulAssetsService.getByArray(listData);
return new ResponseBO<>(list1,SUCCESS);
}
postman调用示例:
dao层没有用@Param注解
List<String> getByArray(Map<String,List<String>> idList);
sql语句
<select id="getByArray" parameterType="map" resultType="string">
select vul_id from t_vul_assets
<where>
<if test="idList!=null and idList.size>0">
id in
<foreach close=")" collection="idList" index="index" item="item" open="(" separator=",">
#{idList[${index}]}
</foreach>
</if>
</where>
</select>