Mybatis+mysql Insert database Three ways to return the value of a self-key ID

Source: Internet
Author: User
Tags mysql insert uuid

First, the scene:

Inserting a database value requires immediate return of the primary key ID for next procedure operation

Second, the solution: the first: The use of universal Mapper insertion method

Mapper.insertselective (record);

This method: Inserts a single piece of data, inserts only non-null fields, does not affect fields with default values
Supports Oracle sequence, UUID, mysql-like indentity auto-grow (auto write-back)
Use sequence, UUID, auto-grow when parameter values are empty, using the passed parameter value preferentially


The actual application of the controller: using the method ID will get the value directly using the entity get of the parameter when used on the entity mapped to the parameter

General Mapper related configuration see other articles http://blog.csdn.net/isea533/article/details/41457529

The second type: Writing SQL statements

DAO Layer Method:

[Java]View PlainCopy
    1. /**
    2. * Insert database and return primary key ID
    3. * @param Batch
    4. * @return
    5. */
    6. Integer Insertbatchreturnid (batch batch);

SQL statement notation FOR XML

Remember to add Usegeneratedkeys and Keyproperty configuration, which refers to setting whether to use JDBC's Getgenereatedkeys method to get the primary key and assign a value to the properties of the Keyproperty setting. The latter is the entity Class primary key field (and the case should correspond)

[HTML]View PlainCopy
  1. <insert id="Insertbatchreturnid" usegeneratedkeys="true" keyproperty="id" Parametertype="Org.uz.dxt.model.bankbase.Batch" >
  2. INSERT INTO T_batch (
  3. Batchcode,
  4. Bankcode,
  5. BizType,
  6. Companycode,
  7. Wtenddate,
  8. Wtdate,
  9. Contractid,
  10. TotalCount,
  11. Totalbase,
  12. Handcode)
  13. Values
  14. (#{batchcode},
  15. #{bankcode},
  16. #{biztype},
  17. #{companycode},
  18. #{wtenddate},
  19. #{wtdate},
  20. #{contractid},
  21. #{totalcount},
  22. #{totalbase},
  23. #{handcode})
  24. </Insert>

The actual application of the controller: using the method ID will get the value directly using the entity get of the parameter when used on the entity mapped to the parameter

[Java]View PlainCopy
    1. Batchservice.insertbatch (param); //entity
    2. Idlist.add (Param.getid ());

Third: SQL statements use <selectKey> get self-increment gradual ID

[HTML]View PlainCopy
  1. <insert id="Add" parametertype="estudent">
  2. Here's how SQL Server gets the primary key value of the last inserted record
  3. <selectkey resulttype="_long" keyproperty="id" order="after">
  4. SELECT @ @IDENTITY as ID
  5. </selectkey>
  6. Insert into Tstudent (name, age) VALUES (#{name}, #{age})
  7. </Insert>

Use ditto

It is recommended to use the first and second Chinese laws

The third approach to oracl additional configuration

The actual application of the controller: using the method ID will get the value directly using the entity get of the parameter when used on the entity mapped to the parameter

Mybatis+mysql Insert database Three ways to return the value of a self-key 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.