@RequestParam在post请求中,确实可以使用,而且加不加这个注解,我都可以收到参数

@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 示例如下:
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&gt;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调用示例:
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&gt;0">
        id in
        <foreach close=")" collection="idList" index="index" item="item" open="(" separator=",">
          #{idList[${index}]}
        </foreach>
      </if>
    </where>
  </select>
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值