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

希望能幫到大家。

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章