本篇文章小编给大家分享一下MyBatis传入参数为List对象实现代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
传入参数为List对象
1. 场景复现
首先有如下一张表:
MySQL [test]> select * from t_entry_resource; +----+-------------+------+----------+--------+--------+---------------------+ | id | resource_id | type | title | banner | icon | add_date | +----+-------------+------+----------+--------+--------+---------------------+ | 11 | 6 | 14 | 分类 | 1.jpg | 2.jpg | 2017-11-17 11:22:30 | | 12 | 3 | 1 | 测试12 | 3.jpg | 4.jpg | 2017-11-17 11:22:30 | | 13 | 653 | 1 | 测试34 | 5.jpg | 6.jpg | 2017-11-20 02:32:26 | | 14 | 1 | 1 | 测试5 | 7.jpg | 8.jpg | 2017-11-20 02:32:51 | | 15 | 3942 | 3 | 测试6 | 9.jpg | 10.jpg | 2017-11-20 02:34:27 | +----+-------------+------+----------+--------+--------+---------------------+ 5 rows in set (0.01 sec)
如果要根据resource_id和type来批量查询记录,该如何编写Mybatis语句?
2. 解决方案
直接贴出来解决方案如下所示:
Dao层接口:
ListfindByRidAndType(List entryResources);
XML语句:
该语句利用了mybatis的foreach动态拼接SQL。
3. foreach属性
4. foreach的几种用法
(1) select count(*) from users id in (x1,x2,x3,...)
(2) select count(*) from key_cols where col_a = ? AND col_b = ?
(3) select * from t_news n where n.tags like ? or n.tags like ?