mybatis批量插入,批量更新

批量插入

<!--批量插入-->
	<insert id="batchInsert">
		insert into t_ingco_trade_lithium_electric_product
		(
			product_no,
			li_e_product_no,
			transpor_report_number,
			msds,
			transpor_report_number_path,
			msds_path,
			un_test_report_path,
			drop_test_report_path,
			certificate_identification_number,
			certificate_identification_name,
			certificate_identification_name_en,
			lithium_content,
			rated_power,
			version_no,
			del_flag,
			create_by,
			create_date,
			update_by,
			update_date
		) VALUES
		<foreach collection="list" item="item" separator=",">
		(
			#{item.productNo},
			#{item.liEProductNo},
			#{item.transporReportNumber},
			#{item.msds},
			#{item.transporReportNumberPath},
			#{item.msdsPath},
			#{item.unTestReportPath},
			#{item.dropTestReportPath},
			#{item.certificateIdentificationNumber},
			#{item.certificateIdentificationName},
			#{item.certificateIdentificationNameEn},
			#{item.lithiumContent},
			#{item.ratedPower},
			1,
			0,
			#{item.createBy},
			#{item.createDate},
			#{item.updateBy},
			#{item.updateDate}
		)
		</foreach>
	</insert>

批量插入只需要在values后面使用"foreach"标签遍历要插入的数据

批量更新

<!--批量更新报告路径-->
	<update id="UpdateTransporReportNumberPath">
		update t_ingco_trade_lithium_electric_product set
		transpor_report_number_path =
		<foreach collection="list" item="item" open="case transpor_report_number" close="else transpor_report_number end">
			when #{item.transporReportNumber}  then #{item.transporReportNumberPath}
		</foreach>
		,version_no = version_no + 1,
		update_by =
		<foreach collection="list" item="item" open="case transpor_report_number" close="else update_by end">
			when #{item.transporReportNumber} then #{item.updateBy}
		</foreach>
		,update_date =
		<foreach collection="list" item="item" open="case transpor_report_number" close="else update_date end">
			when #{item.transporReportNumber} then #{item.updateDate}
		</foreach>
		where
		<foreach collection="list" item="item" open="(" close=")" separator="or">
			transpor_report_number = #{item.transporReportNumber}
		</foreach>
	</update>

使用批量更新数据时,要依次对应更新每一个item对应的数据,使用case when判断对应id。

posted @ 2020-12-24 13:41  小白白白白白白白白白  阅读(106)  评论(0编辑  收藏  举报