MyBatis foreach语句批量插入数据

本例技术:Spring+SpringMVC+MyBatis+Oracle


问题描述:需要将程序里的一个集合保存到数据库里,集合的类型对应数据库的一个实体,若在程序里遍历集合再一条条保存到数据库表中有点麻烦,这里可以利用MyBatis 的 foreach语句实现批量插入数据。


核心代码清单:

Item(实体类):

[java] view plain copy
  1. public class Item {  
  2.     private String itemCode;//项目代码  
  3.     private String itemName;//项目名称  
  4.     private String itemValue;//项目值(多个值用逗号隔开)  
  5.     private String itemCategory;//项目所属类别  
  6.   
  7.     public String getItemCode() {  
  8.         return itemCode;  
  9.     }  
  10.   
  11.     public void setItemCode(String itemCode) {  
  12.         this.itemCode = itemCode;  
  13.     }  
  14.   
  15.     public String getItemName() {  
  16.         return itemName;  
  17.     }  
  18.   
  19.     public void setItemName(String itemName) {  
  20.         this.itemName = itemName;  
  21.     }  
  22.   
  23.     public String getItemValue() {  
  24.         return itemValue;  
  25.     }  
  26.   
  27.     public void setItemValue(String itemValue) {  
  28.         this.itemValue = itemValue;  
  29.     }  
  30.   
  31.     public String getItemCategory() {  
  32.         return itemCategory;  
  33.     }  
  34.   
  35.     public void setItemCategory(String itemCategory) {  
  36.         this.itemCategory = itemCategory;  
  37.     }  
  38. }  

Service实现层方法:
[java] view plain copy
  1. public Integer submitItem(List<Item> list ){  
  2.     return researchMapper.submitItem(list);  
  3. }  

MyBatis的mapper配置文件的语句(在Oracle数据中,多条数据之间用union all 连接,MySQL数据库用,):
[java] view plain copy
  1. <insert id="submitItem"  parameterType="java.util.List">  
  2.        insert into ITEM (  
  3.        ITEM_CODE,  
  4.        ITEM_NAME,  
  5.        ITEM_VALUE,  
  6.        ITEM_CATAGORY  
  7.        )  
  8.        select  item.* from  
  9.        (  
  10.        <foreach collection="list" item="item" index="index" separator="UNION ALL" >  
  11.            select  
  12.            #{item.itemCode,jdbcType=VARCHAR},  
  13.            #{item.itemName,jdbcType=VARCHAR},  
  14.            #{item.itemValue,jdbcType=VARCHAR},  
  15.            #{item.itemCategory,jdbcType=VARCHAR}  
  16.            from dual  
  17.        </foreach>  
  18.        ) item  
  19.    </insert>  

[java] view plain copy
  1. <!--MySql写法-->  
  2. <insert id="submitItem"  parameterType="java.util.List">  
  3.     insert into ITEM (  
  4.     ITEM_CODE,  
  5.     ITEM_NAME,  
  6.     ITEM_VALUE,  
  7.     ITEM_CATAGORY  
  8.     )  
  9.     values  
  10.     <foreach collection="list" item="item" index="index" separator="," >  
  11.       (  
  12.         #{item.itemCode,jdbcType=VARCHAR},  
  13.         #{item.itemName,jdbcType=VARCHAR},  
  14.         #{item.itemValue,jdbcType=VARCHAR},  
  15.         #{item.itemCategory,jdbcType=VARCHAR}  
  16.      )  
  17.     </foreach>  
  18. </insert>  

foreach元素解析:

foreach元素是一个遍历集合的循环语句,它支持遍历数组,List和Set接口的集合。

foreach元素中,collection是传进来的参数名称,可以是一个数组或者List、Set等集合;

                             item是循环中当前的元素(配置的item的名字随意取,类似于iterator);

                             index是当前元素在集合中的位置下标;

                             seperator是各个元素的间隔符;

                             ()分别是open和close元素,表示用什么符号将这些集合元素包装起来。 


注意:由于一些数据库的SQL对执行的SQL长度有限制,所以使用foreach元素的时候需要预估collection对象的长度;foreach除了用于本示例的循环插入,亦可用于构建in条件中(可自行尝试)。





猜你喜欢

转载自blog.csdn.net/weixin_39816740/article/details/80512645