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)進行動態查詢了。
希望能幫到大家。
及時總結,不用加班。。。