MyBatis传多个参数接口展示

接口在传多个参数时 可能遇到映射文件无法获得传入的参数

这里展示一个测试多个参数接口方法

我采用的方法是将多个参数放入Map里 另一种方法 将多个参数建一个新的实体类

map接收的参数名必须和collection里参数名一样 否则也会找不到参数!

无需再更改接口名注释 大家可以看下做下参考(主要还是自己记录 防止忘记)

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文件的格式

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜の雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值