现在的位置: 首页 > 编程语言 > 正文

MyBatis传入数组集合类并使用foreach遍历

2020年02月18日 编程语言 ⁄ 共 730字 ⁄ 字号 评论关闭

这篇文章主要介绍了MyBatis传入数组集合类并使用foreach遍历,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

在mapper中传入数组或集合类,使用foreach标签遍历出其中的值与SQL语句拼接

JAVA dao层接口

public interface UserDao { public List<User> getUsersByCollection(Collection collection);}

mapper文件

<select id="getUsersByCollection" resultMap="userMapper"> select * from users where id in <foreach collection="list" item="id" open="(" close=")" separator=","> #{id} </foreach></select>

测试

@Testpublic void getUsersByCollection() { Collection collection = new ArrayList<Integer>(); collection.add(1); collection.add(3); collection.add(5); List<User> users = userDao.getUsersByCollection(collection); System.out.println(users);}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

以上就上有关MyBatis传入数组集合类并使用foreach遍历的相关介绍,要了解更多mybatis, 数组, 集合, forEach内容请登录学步园。

抱歉!评论已关闭.