当前位置:   article > 正文

Mybatis标签详解_mybatis foreach

mybatis foreach

接到一个批量更新表数据的需求,深入学习了<foreach>标签,下面做一个总结。

目录

一、的六个属性

二、的应用环境

1、批量插入表数据

2、批量更新表数据

(1)单条件更新

(2)多条件更新

3、批量查询表数据

(1)list单条件查询

(2)list多条件查询

(3)map多条件查询


 

一、<foreach>的六个属性

<foreach>属性介绍
属性介绍
collection有三种格式:list,array,map。按照传递参数的类型填写对应格式。
item标签内对象集的参数名
separator每个子循环结束后的分隔符,常用","
open开始符,常用"("
close结束符,常用")"
index在list和array中,index指元素序号;在map中,index指元素key。从0开始自增(相当于数组下标)

*open和close指在foreach标签头和尾添加指定内容,常见于IN和VALUES函数

二、<foreach>的应用环境

1、批量插入表数据

  1. INSERT INTO table (a, b, c, d) VALUES
  2. <foreach collection = "list" item = "item" separatior = ",">
  3. (#{item.a}, #{item.b}, #{item.c}, #{item.d})
  4. </foreach>
  5. /*执行Mybatis后的SQL语句示例
  6. *<foreach>在每个数据集间添加了','
  7. */
  8. INSERT INTO table (a, b, c, d) VALUES
  9. ('a',123,true,'112456789'),
  10. ('b',124,true,'112455789'),
  11. ('c',125,false,'112466789'),
  12. ('d',126,true,'112451789')

2、批量更新表数据

(1)单条件更新

  1. UPDATE table SET b = '10'
  2. WHERE a IN
  3. <foreach collection = "list" item = "item" open = "(" close = ")" separator = ",">
  4. #{item.a}
  5. </foreach>
  6. /*执行Mybatis后的SQL语句示例
  7. *单条件用到了IN函数
  8. *<foreach>在标签头尾添加了'(' ')',并在每个数据间添加了','
  9. */
  10. UPDATE table SET b = '10'
  11. WHERE a IN ('ZBD12131','ZBD12132','ZBD12133')

 

(2)多条件更新

  1. UPDATE table
  2. SET a =
  3. CASE
  4. <foreach collection="list" item="item">
  5. WHEN b=#{item.b} AND c = #{item.c} THEN '10'
  6. </foreach>
  7. ELSE a END;
  8. /*执行Mybatis后的SQL语句示例
  9. *多条件批量更新用到了SQL语句中的CASE WHEN函数
  10. */
  11. UPDATE table
  12. SET d =
  13. CASE
  14. WHEN b=123 AND c = true THEN '10'
  15. WHEN b=124 AND c = true THEN '10'
  16. WHEN b=125 AND c = false THEN '10'
  17. WHEN b=126 AND c = true THEN '10'
  18. ELSE d END;

3、批量查询表数据

(1)list单条件查询

  1. SELECT * FROM table
  2. WHERE a IN
  3. <foreach collection = "list" item = "item" open = '(' close = ')' separator = ','>
  4. #{item.a}
  5. </foreach>
  6. //执行Mybatis后的SQL语句示例
  7. SELECT * FROM table
  8. WHERE a IN ('ZBD123','ZBD124','ZBD125','ZBD126')

(2)list多条件查询

  1. SELECT * FROM table
  2. WHERE a = #{a}
  3. AND
  4. <foreach collection = "list" item = "item" open = '(' close = ')' separator = 'OR'>
  5. b = #{item.b}
  6. AND c = #{item.c}
  7. AND d = #{item.d}
  8. </foreach>
  9. /*执行Mybatis后的SQL语句示例
  10. *多条件批量查询用到了SQL语句OR函数,视实际应用环境修改separator参数
  11. */
  12. SELECT * FROM table
  13. WHERE a = '123'
  14. AND (
  15. b=123 AND c = true AND d = 'ZBD123' OR
  16. b=124 AND c = true AND d = 'ZBD124' OR
  17. b=125 AND c = false AND d = 'ZBD125' OR
  18. b=126 AND c = true AND d = 'ZBD126'
  19. )

(3)map多条件查询

昨天接到了一个新的查询匹配需求,前端传给我包含订单委托明细集合的订单委托实体类,我需要在匹配主委托条件的同时匹配委托明细的各项条件。之前一直没有涉及<foreach>collection的map参数,所以没有让前端传给我委托明细集,我根据主子关系left join。但这个查询匹配如果关联委托主子表的话,就会涉及到跨库left join,容易出现问题,今天开始翻看公司源码库,找有没有更保险的操作,然后就第一次接触到了map的相关用法,做个记录。

  1. SELECT
  2. tp.aggregate_no tender_no,
  3. tp.company_id
  4. FROM db_tender.t_tender_price tp
  5. LEFT JOIN db_tender.t_pallet_item_aggregate pia ON tp.aggregate_no = pia.aggregate_no
  6. LEFT JOIN db_tender.t_pallet_item pi ON pi.company_id = pia.company_id AND find_in_set(pi.pallet_item_no, pia.pallet_item_nos)
  7. where tp.company_id = #{consignorCompanyId}
  8. AND tp.start_date &lt;= #{executionTime}
  9. AND tp.end_date >= #{executionTime}
  10. AND tp.business_type = #{businessModuleId}
  11. AND tp.origin_district_id = #{originDistrictId}
  12. AND tp.destination_district_id = #{destinationDistrictId}
  13. AND
  14. <foreach collection="tOrderWTItemModels" open="(" close=")" item="item" separator="or">
  15. ( pi.contract_no = #{item.pickNo} AND pi.product_name = #{item.productName}
  16. AND IF (pi.wl_relationship = '10',
  17. pi.min_length = #{item.minLength} AND pi.max_length = #{item.maxLength} AND pi.min_width = #{item.minWidth} AND pi.max_width = #{item.maxWidth},
  18. (pi.min_length = #{item.minLength} AND pi.max_length = #{item.maxLength}) OR (pi.min_width = #{item.minWidth} AND pi.max_width = #{item.maxWidth}))
  19. )
  20. </foreach>
  21. GROUP BY tp.aggregate_no, tp.company_id
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/749047
推荐阅读
相关标签
  

闽ICP备14008679号