Mybaits INSERT into MySQL database automatically return ID primary key

Source: Internet
Author: User

When doing an e-commerce project, when adding goods, add the product involves the picture table and the Minimum Sales Unit table, the two tables have the Product ID, add the picture and the smallest sales unit, to first return the product ID. The practice is as follows

 

<insert id= "addproduct" parametertype= "Product" usegeneratedkeys= "true" keyproperty= "id" > 
INSERT statement

</insert>

Where ID represents the method of addition in DAO,

ParameterType represents the type of argument in DAO,

Usegeneratedkeys represents the JDBC Getgeneratedkeys method that gets the primary key and assigns a value to the model set in Keyproperty. The default is False, which is set to true to execute. Support for self-growing IDs in SQL Server and MySQL, which can be used, is not supported for self-growth in Oracle, so it cannot be used.

The keyproperty represents the primary key to return.

DAO in methods:

 Public Integer addproduct (product product);

Properties of product in JavaBean

1  Public classProductImplementsserializable{2 
3     PrivateInteger ID;4     
5         PublicInteger getId () {6         returnID;7     }
8      Public voidsetId (Integer id) {9          This. ID =ID;Ten     }
 One}

Mybaits INSERT into MySQL database automatically return ID primary 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.