在使用mybatis过程中可能存在传入多个参数的可能性 其中可能有单纯的字符类型也可能有复杂的list类型 使用map则解决了这些问题
首先 说一下list作为参数的使用方法
<span style="white-space:pre"> </span> select id, city_id, owner_id from item where id in
<span style="white-space:pre"> </span> <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
<span style="white-space:pre"> </span> #{item}
<span style="white-space:pre"> </span></foreach>
mybatis的xml配置文件里不需要写参数类型 只在相对应的接口里把方法入参类型写清楚就ok了 eg
<span style="white-space:pre"> </span>List<Item> getItemByItemIds(List<Integer> itemIds);
传入map类型 写法与传入list相似 xml里不需要写参数类型 如下
select r.id, r.info_id, r.item_id,
from rent_content r
where
r.id NOT IN (
SELECT
rent_content_id
FROM
rent_calendar
WHERE
rent_content_id = r.id
AND begin_time <#{endDate}
AND end_time > #{startDate}
)
and r.car_item_id in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
只在相对应的接口里写清楚就ok了
List<RentContent> selectTime(HashMap hashMap);
foreach 元素的功能是非常强大的,它允许你指定一个集合,声明可以用在元素体内的集合项和索引变量。它也允许你指定开闭匹配的字符串以及在迭代中间放置分隔符。这个元素是很智能的,因此它不会偶然地附加多余的分隔符。
注意 你可以将一个 List 实例或者数组作为参数对象传给 MyBatis,当你这么做的时候,MyBatis 会自动将它包装在一个 Map 中并以名称为键。List 实例将会以“list”作为键,而数组实例的键将是“array”
相关链接http://mybatis.github.io/mybatis-3/zh/dynamic-sql.html