MyBatis gets the self-growing field value of the inserted record

Source: Internet
Author: User

The first step:

Add the attribute "Usegeneratedkeys" and "Keyproperty" in the MyBatis mapper file, where Keyproperty is the property name of the Java Object!

[HTML]View Plain Copy
  1. < insert   = &NBSP; parametertype = &NBSP;&NBSP;&NBSP;
  2. usegeneratedkeys="true" keyproperty="id" >
  3. INSERT INTO spares (spares_id,spares_name,
  4. Spares_type_id,spares_spec)
  5. values (#{id},#{name},#{typeid},#{spec})
  6. </insert >

Step Two:

MyBatis automatically assigns the self-growth value to the property ID of the object spares after the INSERT statement is executed. Therefore, it can be obtained by spares the corresponding Getter Method!

[Java]View Plain Copy
  1. /** 
  2. * New Spare parts
  3. * @author hellostory
  4. * @param spares
  5. * @return
  6.  */  
  7. @RequestMapping(value = "/insert")
  8. @ResponseBody
  9. Public jsonresponse Insert (Spares spares) {
  10. int count = Sparesservice.insert (spares);
  11. System.out.println ("Total Insert" + count + "Record!) "  
  12. + "\ n the primary key self-growth value of the record just inserted is:" + Spares.getid ());

MyBatis gets the self-growing field value of the inserted record

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.