zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    iPhone开发之深入浅出 (3) — ARC之前世今生
    iPhone开发之深入浅出 (2) — ARC之@property使用
    iPhone开发之深入浅出 (7) — ARC总结
    理解ARC在ObjectiveC中的应用
    c# ref与out的区别
    c# 变量交换
    javascript AJAX简单原理及什么是ajax
    通用的进程监控重拉起bash脚本
    MOOONagent更新:新增IHeartbeatHook接口
    MOOON分布式消息结构
  • 原文地址:https://www.cnblogs.com/aihuadung/p/10467595.html
Copyright © 2011-2022 走看看