Record the learning bit (insert operation returns primary key in MyBatis)

Source: Internet
Author: User
Tags mysql database oracle database


Entity class UserInfo, attribute String username,string password,int usercode,int userId

1.ORACLE Database

1-1 self-increment primary key

To implement a self-increment primary key in Oracle

CREATE SEQUENCE If_kikaku. Seq_user_id_wing 
	INCREMENT by 1 
	START with 1 
	MAXVALUE 999999999 
	nominvalue 
	nocycle 
	Noorder 
	NOCACHE 
/

Mapper.xml

Use If_kikaku. Seq_user_id_wing. Nextval gets the primary key to insert data into.

	<insert id= "GetPrimaryKey" parametertype= "Com.test.model.UserInfo" >
		<selectkey keyproperty= "UserId" Order= "Before" resulttype= "Java.lang.Integer" >
			select If_kikaku. Seq_user_id_wing. Nextval from DUAL
		</selectKey>
		inserts into If_kikaku.test_user_m (
			user_id,
			user_cd, 
			Password
		) VALUES (
			${userid},
			${usercode},
			#{password}
		)
	</insert>

ParameterType: The parameter type of the SQL statement that performs the insert operation.

Keyproperty: Sets the queried primary key value to parametertype which property of the specified object.

Order: The sequence in which the SQL statements inside the label <selectKey> are executed relative to the INSERT statement. Before the Before:insert statement executes. After the After:insert statement executes.

1-2uuid

Get the random GUID as the primary key using the Oracle Sys_guid () method.

	<insert id= "method name" Parametertype= "entity class" >
		<selectkey keyproperty= "entity class Properties" order= "before" resulttype= " Java.lang.Integer ">
			Select Sys_guid () from DUAL
		</selectKey>
		INSERT to If_kikaku.test_user_m (
			primary key,
			...
		) VALUES (
			${entity class properties},
			...
		)
	</insert>

2.MYSQL Database

2-1 self-increment primary key

USER_ID NOT NULL Auto_increment

Mapper.xml

Using the MySQL last_insert_id () method to get the primary key after the INSERT operation, which is not the same as Oracle, Oracle takes the primary key value first and then performs the insert operation.

Simply executing the Select LAST_INSERT_ID () statement returns only 0.

	<insert id= "GetPrimaryKey" parametertype= "Com.test.model.UserInfo" >
		<selectkey keyproperty= "UserId" Order= "after" resulttype= "Java.lang.Integer" >
			Select last_insert_id ()
		</selectKey>
		INSERT Into If_kikaku.test_user_m (
			user_cd, 
			password,
			user_nm,
			upd_time,
			ins_time
		) VALUES (
			${usercode},
			#{password},
			#{username},
			sysdate (),
			sysdate ()
		)
	</insert >

2-2uuid

	<insert id= "method name" Parametertype= "entity class" >
		<selectkey keyproperty= "entity class Properties" order= "before" resulttype= " Java.lang.Integer ">
			Select UUID ()
		</selectKey>
		INSERT into if_kikaku.test_user_m (
			primary key ,
			...
		) VALUES (
			${entity class properties},
			...
		)
	</insert>

Gets the call of the primary key

	public void GetPrimaryKey () {
		UserInfo user = new UserInfo ("name", "Pawd", 4536);
		Mytestservice.getprimarykey (user);
		System.out.println ("The primary key for the newly inserted record is:" + User.getuserid ());
	}






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.