欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Mybatis传入 List<Map<String,Object>>的入参

程序员文章站 2022-06-30 20:50:02
...

直接上代码吧:

service层:

 Map<String,Object>  paramMap = new HashMap<>();
        List<Map<String,Object>>  paramMapList = new ArrayList<>();
        for(ChannelParam channelParam:paramList){
            paramMap.put("device_serial",channelParam.getDeviceSerial());
            paramMap.put("channel_no",channelParam.getChannelNo());
            paramMapList.add(paramMap);
        }
        List<DeviceChannel> deviceChannelList = channelMapper.selectListByChannel(paramMapList);

备注:这里的ChannelParam类是个实体类

@Data
public class ChannelParam {

    private String deviceSerial;
    private Integer channelNo;
}

mapper层:

List<DeviceChannel> selectListByChannel(List<Map<String,Object>>  paramMapList);

sql配置文件:

<select id="selectListByChannel" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from device_channel
    where
    <foreach item="item" index="key" collection="list"
             open="" separator="OR" close="">
      device_serial=#{item.device_serial} and
      channel_no = #{item.channel_no}
    </foreach>

  </select>

解释一下:   collection这里用的是list,这个是mybatis源码中默认的,非list用collection,可以跟进源码看看

     item.device_serial 和 item.channel_no  就相当于 获取map的key名和value