Mybatis: Insert data and return the latest primary key id

insert element attributes                      

  •    parameterType, the fully qualified class name or type alias of the input parameter
  •    keyColumn, set the primary key name automatically generated by the data table. For a specific database (such as PostgreSQL), if the automatically generated primary key is not the first field, it must be set
  •    keyProperty, the default value is unset, which is used to set which property of the domain model the return value of the getGeneratedKeys method or selectKey child element will be assigned to
  •    useGeneratedKeys, the value range is true|false (default value), set whether to use the getGenereatedKeys method of JDBC to get the primary key and assign it to the domain model property set by keyProperty. MySQL and SQLServer execute auto-generated key field, so when the database has set the self-growing primary key, it can be obtained through the getGeneratedKeys method of JDBC. But databases like Oralce that do not support auto-generated key fields cannot use this method to obtain primary keys.
  •    statementType, value range STATEMENT, PREPARED (default value), CALLABLE
  •    flushCache, the value range is true (default)|false, set whether to clear the secondary cache and local cache after performing this operation
  •    timeout, the default is unset (depending on the setting of the jdbc driver), set the maximum time limit for executing the operation, and an exception will be thrown when overtime
  •    databaseId, the value range of oracle|mysql, etc., indicates the database manufacturer, inside the element, you can use `<if test="_databaseId ='oracle'">` to specify different SQL statements for a specific database

General INSERT operation-the return value is the number of records inserted                

Mapper interface code:

/**
 * 添加学生信息
 * @param student 学生实例
 * @return 成功操作的记录数目
 */
int add(EStudent student);

 

mapper.xml:

<insert id="add" parameterType="EStudent">
  insert into TStudent(name, age) values(#{name}, #{age})
</insert

Obtain the primary key of the record after performing the INSERT operation                        

Mapper interface code:

/**
 * 添加学生信息
 * @param student 学生实例
 * @return 成功操作的记录数目
 */
int add(EStudent student);

As for mapper.xml, there are two situations, one is that the database (such as MySQL, SQLServer) supports auto-generated key field, and the other is that the database (such as Oracle) does not support auto-generated key field.

 1. The database (such as MySQL, SQLServer) supports auto-generated key field

    Means (recommended method):

<insert id="add" parameterType="EStudent" useGeneratedKeys="true" keyProperty="id">
  insert into TStudent(name, age) values(#{name}, #{age})
</insert>

    Means ②:

<insert id="add" parameterType="EStudent">
  // 下面是SQLServer获取最近一次插入记录的主键值的方式
  <selectKey resultType="_long" keyProperty="id" order="AFTER">
    select @@IDENTITY as id
  </selectKey>
  insert into TStudent(name, age) values(#{name}, #{age})
</insert>

Since the method ② obtains the primary key depends on the database itself, the method ① is recommended.

 2. The database (such as Oracle) does not support auto-generated key field

<insert id="add" parameterType="EStudent">
  <selectKey keyProperty="id" resultType="_long" order="BEFORE">
    select CAST(RANDOM * 100000 as INTEGER) a FROM SYSTEM.SYSDUMMY1
  </selectKey>
  insert into TStudent(id, name, age) values(#{id}, #{name}, #{age})
</insert>

  Note: The return value of the mapper interface is still the number of records successfully inserted, but the difference is that the primary key value has been assigned to the id of the domain model entity.

 selectKey child element                            

  Role: Insert query statements in insert element and update element.

  Its properties are as follows:

  •      keyProperty, the default value is unset, which is used to set which property of the domain model the return value of the getGeneratedKeys method or selectKey child element will be assigned to
  •      resultType, the fully qualified class name or type alias of the attribute class pointed to by keyPropety
  •      The order attribute, the value range BEFORE|AFTER, specifies whether to perform the selectKey operation before or after the insert statement
  •      statementType, value range STATEMENT, PREPARED (default value), CALLABLE

Note: The selectKey operation will assign the result of the operation query to the corresponding attribute under the input parameter instance of the parameterType of the insert element. And provide it to the insert statement

Guess you like

Origin blog.csdn.net/fly910905/article/details/109228331