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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

Esrevinud的笔记   2021-04-21 我要评论

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

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们