MyBatis BULK INSERT statement (List )

Source: Internet
Author: User
Tags bulk insert

My interface definition:

int Insertabilitypackageconsumer (Abilitypackageconsumer abilitypackageconsumer);

My entry entity length is this: (To reduce the amount of code, I only put the attribute)

public class Abilitypackageconsumer implements serializable{

	/**
	 * */
	private static final long Serialversionuid = 7922942036926940889L;
	Private long appid;//Application ID private long
	abilitypackageid;//capability package ID
	private long abilityid;//capability ID
	Private Long abilityruleid;//ability fee rule ID
	private long chargeruleid;//billing policy ID
	private Date subscribetime;//order time
	Private Long crtuid;//creator
	private Date crttime;//creation time
	Private list<abilitydetail> abilitylist;

}

So what should be written about MyBatis's BULK INSERT statement?

That's what I wrote in the mapping file:

<insert id= "Insertabilitypackageconsumer" >
		insert INTO Ability_package_consumer (App_id,ability_package_ id,ability_id,
		ability_rule_id,charge_rule_id,subscribe_time,crt_uid,crt_time)
		Select A.* from
		(
		<foreach collection= "abilitylist" item= "ability" index= "index" separator= "UNION" >
			SELECT
			#{appid,jdbctype=bigint} as app_id, 
			#{abilitypackageid,jdbctype=bigint} as ability_package_id,
			#{ability.abilityid,jdbctype=bigint} as ability_id,
			#{ability.abilityruleid,jdbctype=bigint} as Ability_rule_ ID,
			#{ability.chargeruleid,jdbctype=bigint} as charge_rule_id, now
			() as Subscribe_time,
			#{crtuid, Jdbctype=bigint} as Crt_uid, now
			() as Crt_time from
			dual
		</foreach>
		) A
	</insert >

Through a foreach traversal, a combined view of multiple records is completed, collection attribute values are consistent with the entity, item is customized, and then passed. accesses its property values.

This is primarily based on the INSERT into table name (?,?,?) in SQL (?,?,?) Union (?,?,?) The use of this statement, after the table name. Represents a field name, followed by a property value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.