MyBatis传入数组集合类并使用foreach遍历
时间:2021-06-11 08:11:59|栏目:JAVA代码|点击: 次
这篇文章主要介绍了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);
}


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




