软件编程
位置:首页>> 软件编程>> java编程>> MyBatis传入数组集合类并使用foreach遍历

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

作者:Esrevinud的笔记  发布时间:2022-04-19 02:25:00 

标签:MyBatis,数组,集合,foreach

这篇文章主要介绍了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>

测试


@Test
public 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);
}

来源:https://www.cnblogs.com/esrevinud/p/12299195.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com