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>
  • 相关阅读:
    Linux之基础系统优化
    Linux之shell命令
    Django解决跨域问题
    Django中使用geetest验证
    python2与python3的区别
    一个长得很丑的登录和注册
    Django组件-forms组件
    Django组件-中间件
    cookie、session与用户认证组件
    jquery练习
  • 原文地址:https://www.cnblogs.com/aihuadung/p/10467595.html
Copyright © 2011-2022 走看看