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
- < insert = &NBSP; parametertype = &NBSP;&NBSP;&NBSP;
- usegeneratedkeys="true" keyproperty="id" >
- INSERT INTO spares (spares_id,spares_name,
- Spares_type_id,spares_spec)
- values (#{id},#{name},#{typeid},#{spec})
- </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
- /**
- * New Spare parts
- * @author hellostory
- * @param spares
- * @return
- */
- @RequestMapping(value = "/insert")
- @ResponseBody
- Public jsonresponse Insert (Spares spares) {
- int count = Sparesservice.insert (spares);
- System.out.println ("Total Insert" + count + "Record!) "
- + "\ 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