mybatis automatically generates selectkey

When performing data insertion, how to return the primary key of the inserted data. Various databases have different strategies for generating primary keys, such as Oracle and PostgreSQL that use sequences to generate primary keys in advance; MySQL and SQL Server that generate primary keys afterward. The following will introduce the process of unified processing using mybatis
1. Introduction
to selectKey selectKey will generate a primary key for the record to be inserted before executing data insertion. And after inserting, return the primary key.
<selectKey keyProperty="id" resultType="int" order="BEFORE" statementType="PREPARED">

Attribute description
keyProperty The target property to which the result of the selectKey statement should be set. It can also be a comma-separated list of property names if multiple generated columns are desired.
keyColumn Matches the column name in the returned result set of the property. It can also be a comma-separated list of property names if multiple generated columns are desired.
resultType The type of the result. MyBatis can usually be calculated, but there is no problem in writing it for more certainty. MyBatis allows any simple type to be used as a primary key type, including strings. If you want to act on multiple generated columns, you can use an Object or a Map containing the desired properties.
order This can be set to BEFORE or AFTER. If set to BEFORE, then it will first select the primary key, set the keyProperty and then execute the insert statement. If set to AFTER, the insert statement is executed first, followed by the selectKey element - this is similar to databases like Oracle, which may have embedded index calls inside the insert statement.
statementType is the same as before, MyBatis supports the mapping types of STATEMENT, PREPARED and CALLABLE statements, representing PreparedStatement and CallableStatement types respectively.

2. Example
1, oracle example: (STOCKIDSEQUENCE is an established sequence)
<insert id="insertProduct-ORACLE" parameterClass="product"> 
    <selectKey resultClass="int" type="pre" keyProperty="id" > 
        SELECT STOCKIDSEQUENCE.NEXTVAL AS VALUE FROM DUAL 
    </selectKey> 
    insert into PRODUCT (PRD_ID,PRD_DESCRIPTION) values ​​(#id#,#description#) 
</insert>
2. sql-server example:
<insert id="insertProduct-MS- SQL" parameterClass="product"> 
    insert into PRODUCT (PRD_DESCRIPTION) values ​​(#description#) 
    < 
        select @@IDENTITY as value 
    </selectKey> 
</insert>

3、mysql例子:
<insert id="insertProduct-MYSQL" parameterClass="product"> 
    insert into PRODUCT (PRD_DESCRIPTION) values (#description#) 
    <selectKey resultClass="int" type="post" keyProperty="id" > 
        select LAST_INSERT_ID() as value 
    </selectKey> 
</insert>
4、SQLite例子:
<insert id="Create" parameterClass="Subject">
      INSERT INTO SUBJECT
      (SubjectName,QuestionCount,IsNowPaper)
      VALUES(#SubjectName#,#QuestionCount#,#IsNowPaper#)
      <selectKey resultClass="int" type="post" property="SubjectId">
        SELECT seq
        FROM sqlite_sequence
        WHERE (name = 'SUBJECT')
      </selectKey>
注意:name = 'SUBJECT'中SUBJECT为表名称

5、postgresql例子:
<insert id="insertProduct-Postgres"  parameterClass="product"> 
    <selectKey resultClass="int"  type="pre"  keyProperty="id" > 
        SELECT STOCKIDSEQUENCE.NEXTVAL AS VALUE FROM DUAL 
    </selectKey> 
    insert into PRODUCT (PRD_ID,PRD_DESCRIPTION) values (#id#,#description#) 
</insert>

Guess you like

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