MyBatis + MySQL returns the inserted primary key ID

Source: Internet
Author: User

Requirement: after a record is inserted into the MySQL database using MyBatis, the auto-increment primary key value of the record needs to be returned.


Method: Specify the keyProperty in mapper, for example:

Xml Code 650) this. width = 650; "class =" star "src =" http://www.bkjia.com/uploads/allimg/131228/16042631Y-0.png "alt =" add to favorites code "style =" border: 0px; "/>
  1. <Insertid = "insertAndGetId" useGeneratedKeys = "true" keyProperty = "userId" parameterType = "com. chenzhou. mybatis. User">

  2. Insert into user (userName, password, comment)

  3. Values (# {userName}, # {password}, # {comment })

  4. </Insert>

As shown above, keyProperty = "userId" is specified in insert, where userId represents the primary key attribute of the inserted User object.


User. java

Java code 650) this. width = 650; "class =" star "src =" http://www.bkjia.com/uploads/allimg/131228/16042631Y-0.png "alt =" add to favorites code "style =" border: 0px; "/>
  1. Publicclass User {

  2. Privateint userId;

  3. Private String userName;

  4. Private String password;

  5. Private String comment;

  6. // Setter and getter

  7. }

UserDao. java

Java code 650) this. width = 650; "class =" star "src =" http://www.bkjia.com/uploads/allimg/131228/16042631Y-0.png "alt =" add to favorites code "style =" border: 0px; "/>
  1. Publicinterface UserDao {

  2. Publicint insertAndGetId (User user User );

  3. }

Test:

Java code 650) this. width = 650; "class =" star "src =" http://www.bkjia.com/uploads/allimg/131228/16042631Y-0.png "alt =" add to favorites code "style =" border: 0px; "/>
  1. User user = new User ();

  2. User. setUserName ("chenzhou ");

  3. User. setPassword ("xxxx ");

  4. User. setComment ("test the primary key function for data insertion ");

  5. System. out. println ("primary key before insertion:" + user. getUserId ());

  6. UserDao. insertAndGetId (user); // insert operation

  7. System. out. println ("primary key after insertion:" + user. getUserId ());

Output:

Shell code 650) this. width = 650; "class =" star "src =" http://www.bkjia.com/uploads/allimg/131228/16042631Y-0.png "alt =" add to favorites code "style =" border: 0px; "/>
  1. Primary Key before insertion: 0

  2. Primary Key after insertion: 15

Query the database:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/16042A521-5.jpg "style =" border: 0px; "height =" 61 "width =" 616 "/>


As shown above, the primary key id of the newly inserted record is 15


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.