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
- /**
- * Insert database and return primary key ID
- * @param Batch
- * @return
- */
- 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
- <insert id="Insertbatchreturnid" usegeneratedkeys="true" keyproperty="id" Parametertype="Org.uz.dxt.model.bankbase.Batch" >
- INSERT INTO T_batch (
- Batchcode,
- Bankcode,
- BizType,
- Companycode,
- Wtenddate,
- Wtdate,
- Contractid,
- TotalCount,
- Totalbase,
- Handcode)
- Values
- (#{batchcode},
- #{bankcode},
- #{biztype},
- #{companycode},
- #{wtenddate},
- #{wtdate},
- #{contractid},
- #{totalcount},
- #{totalbase},
- #{handcode})
- </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
- Batchservice.insertbatch (param); //entity
- Idlist.add (Param.getid ());
Third: SQL statements use <selectKey> get self-increment gradual ID
[HTML]View PlainCopy
- <insert id="Add" parametertype="estudent">
- Here's how SQL Server gets the primary key value of the last inserted record
- <selectkey resulttype="_long" keyproperty="id" order="after">
- SELECT @ @IDENTITY as ID
- </selectkey>
- Insert into Tstudent (name, age) VALUES (#{name}, #{age})
- </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