您的位置:首页 > 数据库

mybatis使用foreach实现sql的in查询

2018-02-07 00:00 525 查看
摘要: 先取出id集合,然后在mybatis中使用foreach实现in(1,2,3)查询

dao层 ITemplateDao.java:

List<BrokerTemplate>  GetTemplateListByIds(@Param(value = "ids") List<Long> ids);


resource:Template.xml

<select id="GetTemplateListByIds" resultMap="BrokerTemplate">
SELECT tb1.broker_template_id,
tb1.broker_id,
tb1.type,
tb1.content,
tb1.is_default,
tb1.create_time,
tb1.update_time,
tb1.is_deleted,
tb1.delete_time,
tb1.name,
tb1.`is_locked`
FROM broker_template  tb1
WHERE broker_template_id IN
<foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
#{id}
</foreach>
</select>

<foreach>中 属性如下

collection :是遍历的数据集合,这里是我的list<Long> ids。

item : 每个子项命名。需要跟foreach内使用时一致,这里是#{id}

index:索引位置

open :在遍历执行之前先附加的字符串。

close : 在遍历执行完成后附加的字符串。

separator:遍历每个item子项后附加的字符串(即子项之间自定义的间隔符号)

栗子: open="(" close=")" separator="," 拼出来就是 ( item1,item2, item3 )
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: