• 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>
  • 相关阅读:
    redis数据结构底层剖析学习笔记2
    redis数据结构底层剖析学习笔记1
    java正则表达式学习笔记
    springmvc sessionfilter 登录过滤器
    Java中JSON字符串与java对象的互换实例详解
    用9种办法解决 JS 闭包经典面试题之 for 循环取 i
    近期流行的JavaScript框架与主题
    JavaScript 中的 this 问题总结 !
    常见前端面试题及答案(下)
    常见前端面试题及答案(上)
  • 原文地址:https://www.cnblogs.com/aihuadung/p/10467595.html
Copyright © 2020-2023  润新知