MyBatis PRIMARY Key return

Source: Internet
Author: User

In project development, it is sometimes necessary to get the primary key value.

For example, in the relational relationship of the table, after inserting the data into the main table and inserting the child table, we need to insert the primary key of the primary table into the child table as a foreign key.

In this case, you need to get the primary key value.

So the problem involving the primary key is returned.

1. Return of the self-increment primary key

MySQL self-increment primary key, automatically generates a self-increment primary key before performing an insert commit.

Get the self-increment primary key to the record you just inserted by using the MySQL function.

LAST_INSERT_ID (), call this function after INSERT.

<InsertID= "Insertuser"ParameterType= "Com.winner.User">        <!--returns the primary key of the inserted data and inserts it into the user object.            SELECT last_insert_id (): Gets the primary key value that was just INSERT in the record, only applies to the self-increment primary key.            Keyproperty: Sets the primary key value of the query to which property of the specified object ParameterType.            Order:select last_insert_id () executes the order in which it is executed relative to the INSERT statement. Resulttype: Specifies the result type of select LAST_INSERT_ID () -        <SelectkeyKeyproperty= "id"Order= "after"Resulttype= "Java.lang.Integer">SELECT last_insert_id ()</Selectkey>INSERT into User (username,birthday,sex,address) VALUES (#{username},#{birthday},#{sex},#{address})</Insert>
New User (); User.setusername ("Wangxiao"); User.setbirthday(new  Date ()); User.setsex ("1" ); User.setaddress ("Nanjing, Jiangsu"); Sqlsession.insert (user. class. GetName (). Concat (".") + "Insertuser", user);

As you can see, the user object generated here does not have the value of setting the ID, which by default is the setting of the Selectkey in the 0,mapper file so that the value of the ID of the user object is the value of the primary key ID of the corresponding record in the database.

This value is, of course, only after the insert operation is complete .

2. Non-self-increment primary key returned

Using the MySQL UUID () function to generate the primary key, you need to modify the table with the ID field type string and the length set to 35 bits.

Implementation ideas:

First, a primary key is queried through the UUID () and the primary key is entered into the SQL statement.

The execution UUID () statement order is executed in relation to the INSERT statement.

<InsertID= "Insertuser"ParameterType= "Com.winner.User">        <!--use the MySQL uuid () function to generate the primary key and set the primary key to the id attribute of the user object.            Second, when executing the INSERT statement, remove the value of the id attribute from the user object.        Note that in this case the INSERT statement needs to include the ID field and the id attribute.  -        <SelectkeyKeyproperty= "id"Order= "Before"Resulttype= "Java.lang.String">SELECT uuid ()</Selectkey>INSERT into User (id,username,birthday,sex,address) VALUES (#{id},#{username},#{birthday},#{sex},#{address}) </Insert>

Note that in this case, the ID field type of the table must be a varchar type, not self-increment, and the length must be greater than 35.

New User (); User.setusername ("Wangxiao"); User.setbirthday(new  Date ()); User.setsex ("1" ); User.setaddress ("Nanjing, Jiangsu"); Sqlsession.insert (user. class. GetName (). Concat (".") + "Insertuser", user);

As you can see, the generated user object does not have a value for the ID, default is null, and the Selectkey setting in the Mapper file causes the value of the ID of the user object to be the value of the primary key ID of the corresponding record in the database.

However, this value is available before the insert Operation .

MyBatis PRIMARY Key return

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.