mybatis_07动态SQL_foreach循环

废话不多说,直接上代码!

<select id="findUserByforeach" parameterType="userQueryVO" resultMap="userByresultmap">
select * from user
<where>
<!--
for(item:items){} foreach:表示一个foreach循环
collection:集合参数的名称,如果是直接传入集合参数,则该处只能写[list],相当于上面的items
item:相当于上面的item,元素名称id,每次遍历出来的id
open:开始遍历时拼接的串,
close:结束遍历时拼接的串
separator:遍历出每个对象需要拼接的字符
-->
<if test="ids!=null and ids.size()>0">
<foreach collection="ids" item="id" open="and id in(" close=")" separator=",">
${id}
</foreach>
</if>
</where>
</select>

包装类(供参考) userQueryVO:

package com.ahd.vo;

import com.ahd.model.User;
import org.apache.logging.log4j.core.config.Order; import java.util.List; public class UserQueryVO {
private User user;
private Order order;
private List<Integer> ids; public List<Integer> getIds() {
return ids;
} public void setIds(List<Integer> ids) {
this.ids = ids;
} public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
} }

userQueryVO

传入参数是数组的情况

<!--java.util.list 别名list-->
<select id="findUserByforeach2" parameterType="list" resultMap="userByresultmap">
select * from user
<where>
<!--
for(item:items){} foreach:表示一个foreach循环
collection:集合参数的名称,如果是直接传入集合参数,则该处只能写[list],相当于上面的items
item:相当于上面的item,元素名称id,每次遍历出来的id
open:开始遍历时拼接的串,
close:结束遍历时拼接的串
separator:遍历出每个对象需要拼接的字符 -->
<if test="list!=null and list.size()>0">
<foreach collection="list" item="id" open="and id in(" close=")" separator=",">
${id}
</foreach>
</if>
</where>
</select>
上一篇:E - BD String


下一篇:python-PyQt-是否可以运行两个应用程序?