mybatis之map传参(List和对象)

本文介绍了一种使用Map传参的方式来进行SQL动态查询的方法。通过实例展示了如何将包含List和对象的数据作为参数传递给Mapper,并在mapper.xml中通过foreach和if标签实现动态SQL的构造。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

map传参在我们实际开发会经常用到,也是一种特别方便的传参方式。

话不多说上代码

service层:、

Map<String, Object> params = new HashMap<String, Object>();
List<Integer> passengerType = new ArrayList<>();
passengerType.add(1);
passengerType.add(2);
passengerType.add(3);
params.put("userId", 123456789L);
params.put("passengerType", passengerType);
List<UserCommonTravelers> list=userCommonTravelersMapper.selectUserComTraInfoByUserId(params);

Mapper文件:

 //根据userid和passengerType查询
 List<UserCommonTravelers> selectUserComTraInfoByUserId(Map<String, Object> params);

mapper.xml文件:

<select id="selectUserComTraInfoByUserId" resultMap="BaseResultMap" parameterType="java.util.Map">
  select * from 
  user_common_travelers 
  where 1=1 and passenger_type in
   <foreach item="item" index="index" collection="passengerType" open="(" separator="," close=")">  
   #{item}  
   </foreach>

   <if test="userId != null" >
     and user_id = #{userId}
   </if>

  </select>

注:collection=”passengerType”

好了,通过以上代码大家就知道了怎么用map传入带有list和对象(Long)进行动态查询了。

希望能帮到大家。

及时总结,不用加班。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值