1. 程式人生 > >MyBatis批量插入資料

MyBatis批量插入資料

1.Service
int add(List<BillManagement> billManagement);

2.ServiceImpl
public int add(List<BillManagement> billManagement) {
    return billManagementMapper.insertBatch(billManagement);
}
  
3.Mapper 
int insertBatch(List<BillManagement> billManagement);

4.xml檔案
<!-- 批量插入 -->
<insert id="insertBatch"  parameterType="java.util.List">
insert into p_bill_management (
  belonging_organization,
  bill_types,
  bill_number,
  create_by,
  create_date)
values
<foreach collection ="list" item="item" index="index" separator =",">
  (#{item.belongingOrganization,jdbcType=VARCHAR},
  #{item.billTypes,jdbcType=INTEGER},
  #{item.billNumber,jdbcType=VARCHAR},
  #{item.createBy,jdbcType=VARCHAR},
  #{item.createDate,jdbcType=TIMESTAMP})
</foreach >
</insert>