Mybatista primary key mapping, performing inset return primary key

Source: Internet
Author: User

At some point, our business needs to execute the INSERT statement before returning the primary key, you can use the primary key mapping

First, define SQL, take MySQL as an example

1 <  ID= "AddUser"  usegeneratedkeys= "true"  keyproperty= "User _id "  parametertype=" Com.entity.User ">2    INSERT INTO User (Name,xes,age) VALUES (' Jack ', ' Male ',3</insert>

There are two properties here.

Usegeneratedkeys: Whether to generate a primary key
  the value of Keyproperty corresponds to the user_id property of the Com.entity.User class

Writing DAO

 Public int addUser (user user);

Call Mapper

New Userdao (); int num = userdao.adduser (); SYSTEM.OUT.PRINTLN (num);   // the only number of items that are output here is response // obtained directly from the entity class, Mybatista automatically sets the primary key ID into the user class
USER.GETUSER_ID () Gets the primary key ID value of the map

Mybatista primary key mapping, performing inset return primary key

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.