MyBatis insert into MySQL with Auto_increment key

Source: Internet
Author: User

MyBatis Version: 3.2.8

Actually very simple, before want to complicate ...

MyBatis after inserting a record of the self-increment ID, the incoming Pojo automatically updates the value of the ID to the inserted result

(currently only the ID is measured, other fields if not given the value, do not know whether updates, pending testing)

The records are as follows:

User.xml

<insert id= "Create" parametertype= "User" usegeneratedkeys= "true" keyproperty= "id" > INSERT into T_user (< Include refid= "All_property"/>) VALUES (#{createtime}, #{mobile}, #{email},#{qq}) </insert>

Iusermapper.java

void Create (user user);

Test Code

@Test public void Testcreate () {sqlsession session = Dbutil.getsqlsession ();      Iusermapper usermapper = Session.getmapper (Iusermapper.class);      User user = Usermapper.find ("18910000012");//User.setid (0L);      User.setmobile ("18900121033");      Usermapper.create (user);      System.out.println (User.getid ());      Session.commit ();   Dbutil.close (session); }

Result (ID returned):

Other circumstances how to get the ID is still to be experimented with, this kind of incoming Pojo is very simple, do not need <selectkey/> (I did not get ID through this tag)


MyBatis insert into MySQL with Auto_increment key

Related Article

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.