In Mobile, there are three locations where the parameter "useGeneratedKeys" can be set:
1. Set the useGeneratedKeys parameter in the setting element. For databases that support automatic generation of primary keys, such as mysql, sql server, set useGeneratedKeys to true at this time. After inserting records, the primary key ID automatically generated by the database can be read.
The useGeneratedKeys set in the setting element is a global parameter, but it only affects the interface mapper and is invalid for the xml mapper.
<settings> <!-- Allowing JDBC to support automatic generation of primary keys requires driver compatibility. If set to true, this setting forces the use of auto-generated primary keys, which work well even though some drivers are incompatible (such as Derby). --> <setting name="useGeneratedKeys" value="true" /> </settings>
At this point, the primary key ID is returned after adding records to the interface map.
public interface TestMapper { // Controlled by the global useGeneratedKeys parameter, the primary key id is returned after adding the record @Insert("insert into test(name,descr,url,create_time,update_time) values(#{name},#{descr},#{url},now(),now())") Integer insertOneTest(Test test); }
At this point, if useGeneratedKeys = false is set in the interface mapper, adding records will no longer return the record ID, which has a high priority.
// The useGeneratedKeys parameter values set in the interface mapper will override the global useGeneratedKeys parameter values set in the settings element @Options(useGeneratedKeys = false, keyProperty = "id", keyColumn = "id") @Insert("insert into test(name,descr,url,create_time,update_time) values(#{name},#{descr},#{url},now(),now())") Integer insertOneTest(Test test);
2. Configuring the useGeneratedKeys parameter in the XML mapper
<!-- insert data:Returning record id value --> <insert id="insertOneTest" parameterType="org.chench.test.mybatis.model.Test" useGeneratedKeys="true" keyProperty="id" keyColumn="id"> insert into test(name,descr,url,create_time,update_time) values(#{name},#{descr},#{url},now(),now()) </insert>
The useGeneratedKeys parameter configured in the xml mapper will only affect the xml mapper, and the global useGeneratedKeys parameter value set in the settings element will have no effect on the xml mapper.
3. Setting the useGeneratedKeys parameter in the interface mapper
// Set useGeneratedKeys to true and return the record primary key id automatically generated by the database @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") @Insert("insert into test(name,descr,url,create_time,update_time) values(#{name},#{descr},#{url},now(),now())") Integer insertOneTest(Test test);
Attachment: The oracle database does not support the AUTO_INCREMENT column. She uses sequences to generate self-increasing primary keys.
When oracle inserts a record, if it also wants to return the id of the current record, it can write as follows:
<insert id="insertStudent" parameterType="Student"> <selectKey keyProperty="studId" resultType="int" order="BEFORE"> SELECT my_seq.nextval FROM DUAL </selectKey> INSERT INTO STUDENTS(STUD_ID,NAME,EMAIL, PHONE) VALUES(#{studId},#{name},#{email},#{phone}) </insert>
Here we use the selectKey subelement to generate the primary key value and save the value to the studId attribute of the Student object. The attribute order= "before" indicates that MyBatis will take the next value of the sequence as the primary key value and set the value to the studId property before executing the INSERT statement.
Note: SelectKey needs to pay attention to the order attribute. In databases such as MySQL and SQL Server that support automatic growth, order needs to be set to after to get the correct value.
In order to take a sequence like Oracle, you need to set it to before, otherwise you will report an error.
Reference resources: In-depth discussion on the use GeneratedKeys parameter usage of mybatis