MyBatis Insert return PRIMARY KEY (sqlserver2008)

Source: Internet
Author: User
Tags cdata

MyBatis Insert return primary KEY (sqlserver2008)Mybatisxml configuration, the following two ways to line 1:<insert id= "Insert" parametertype= "Com.user.model.User" > <selectkey resulttype= " Java.lang.Integer "keyproperty=" id "order=" after "> select @ @IDENTITY </selectKey> <! [cdata[INSERT INTOTb_user (Username,password) VALUES (#{username},#{password})]]></insert> mode 2:<insert id= "Insertuser" parametertype= "Com.user.model.User" usegeneratedkeys= "true" keyproperty= "id" ><! [cdata[INSERT INTOTb_user (Username,password) VALUES (#{username},#{password})]]></insert> defines the DAO layer, the service layer's Insert method in action to invoke user user = new user (), User.setusername ("Tom"); User.setpassword ("123456"); int row = Userservice.insert (user); //Note: INSRT no longer returns a primary key in MyBatis, only returns the number of response rows, which is different from IbatisSystem.out.println ("Number of rows in response:" +row); int id = User.getid (); //Gets the value of the identity column ID from the object that has been increasedSYSTEM.OUT.PRINTLN ("ID of the newly inserted data:" +id);

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.