• Mapper.xml文件如何写集合遍历语句


    List<类名> selectListResult(@Param(“参数”) List<类名> 参数);

    
    <select id="方法名"  resultMap="返回数据匹配类">
      		select * from table_name where  1=1      <!-- 使用1=1防止list为null时sql语句报错-->
      <!-- 判断-->
      <if test="list!= null">
        and id in
        <!-- for循环, item:循环后的值, index:循环下标列式for循环的 i ,collection:参数名-->
        <!-- open="(" close=")" separator="," 就是把循环的值组成 (item1,item2,item3)的格式--> <foreach item="item" index="index" collection="list" open="(" close=")" separator=",">
    	 #{item}
    </foreach>
      </if>
    </select>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    <foreach collection="dto.orderStatusList" item="item" index="index" open="(" close=")" separator=",">
    	#{item}
    </foreach>
    
    
    • 1
    • 2
    • 3
    • 4
    collection:要做foreach的对象,作为入参时,List<?>对象默认用list代替作为键,数组对象有array代替作为键,Map对象用map代替作为键,该参数必选;
    
    item:循环体中的具体对象,支持属性的点路径访问,该参数必选;
    
    index:在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选;
    
    open:foreach代码的开始符号,一般是(和close=")"合用,该参数可选;
    
    close:foreach代码的关闭符号,一般是)和open="("合用,该参数可选;
    
    separator:元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,该参数可选;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  • 相关阅读:
    平衡二叉树之红黑树
    Java之LinkedList()
    JVM 面试题
    A_A02_001 CH340驱动安装
    2-3.spring源码--BeanFactoryPostProcessor
    Java知识点一
    4面全过之后还有交叉面,阿里新零售Java岗面试也太“刺激”了
    20231012_python练习_服务端与客户端数据交互v2_增加xlsx表格数据批量导入数据库
    JAVA- Acwing -求 1+2+...+n
    RHEL 8.6 NFS 服务端安装配置
  • 原文地址:https://blog.csdn.net/beiback/article/details/125456813