Mybatis automatically generates key values (selectKey and useGeneratedKeys)

The problem of automatically generating primary keys is often used in insert and update operations.

  1. selectKey and useGeneratedKeys properties 
    useGeneratedKeys  (insert and update only) This tells MyBatis to use the JDBC getGeneratedKeys method to retrieve keys generated internally by the database (eg auto increment fields in RDBMS like MySQL or SQL Server). Default: false 
    ((only for insert and update is useful) This tells MyBatis to use JDBC's getGeneratedKeys method to fetch primary keys internally generated by data (eg: auto-increment fields in database management systems like MySQL and SQL Server). Default: false.) 
    keyProperty 
    (insert and update only) Identifies a property into which MyBatis will set the key value returned by 
    getGeneratedKeys , or by a selectKey child element of the insert statement. Default: unset . 
    Can be a comma separated list of property names if multiple generated columns are expected. 
    ((insert and update only) Mark a property, MyBatis will set its value through getGeneratedKeys or through the selectKey sub-element of the insert statement. Default: not set.) 
    keyColumn 
    (insert and update only) Sets the name of the column in the table with a generated key. This is only required 
    in certain databases (like PostgreSQL) when the key column is not the first column in the table. Can be a 
    comma separated list of columns names if multiple generated columns are expected.
  2. selectKey and useGeneratedKeys use
<insert id="insert">
 <selectKey keyProperty="id" resultType="int" order="BEFORE">
  <if test="_databaseId == 'oracle'">
   select seq_users.nextval from dual
  </if>
  <if test="_databaseId == 'db2'">
   select nextval for seq_users from sysibm.sysdummy1"
  </if>
 </selectKey>
 insert into users values (#{id}, #{name})
</insert>

Obtain the key value before or after the insert operation through selectKey, and insert or return the field as a field. (The serial value id obtained by this code is inserted into the users table as a field value)

<insert id="insertAuthor" useGeneratedKeys="true" keyProperty="id">
    insert into Author (username,password,email,bio)
    values (#{username},#{password},#{email},#{bio})
</insert>

If the database supports self-increasing primary key fields (such as mysql, sql server), set useGeneratedKeys=”true” and keyProperty, so that the primary key id value can be inserted 
oracle does not support self-increasing id, set useGeneratedKey=”false”, if set true, it will There is an error message. Generate id through nextval function, such as SEQ_table.Nextval

3. When inserting and updating a piece of data, you can use selectKey to get the id operation. When multiple pieces of data are inserted and updated, and selectKey can only be used once, the useGeneratedKeys operation should be used.

Analyze the use of selectKey and useGeneratedKeys attributes from Mybatis source code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324926817&siteId=291194637