1. 程式人生 > >mybatis之map傳參(List和物件)

mybatis之map傳參(List和物件)

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)進行動態查詢了。

希望能幫到大家。

及時總結,不用加班。。。