MyBatis acquired primary key is inserted while recording

MyBatis acquired primary key is inserted while recording

MyBatis inserted simultaneously record the primary key acquisition system interface

useGeneratedKeys Attributes

keyProperty Attributes

keyColumn Attributes

selectKey element

Precautions: acquired primary key is set to the primary key attribute of the object, rather than as a return value.
  1. useGeneratedKeysProperty implementation, specify a value of true, then automatically inserts MyBatis generated key record into the object.

useGeneratedKeysProperty, this method applies only to support the auto-increment primary key database. Such MySQL, does not apply to the primary key of the sequence Oracle. Mybatis uses the JDBC getGeneratedKeys method to obtain primary key generated by the internal database. After obtaining the values assigned to the primary key keyProperty attributes configuration, when it is necessary to use a comma plurality of attributes, a plurality of main keys also need to set keyColumn properties, in the order specified database column, where the column values keyProperty configuration properties one by one.

<insert id="insert" parameterType="com.pinyougou.pojo.TbSpecification" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
    insert into tb_specification (id, spec_name)
    values (#{id,jdbcType=BIGINT}, #{specName,jdbcType=VARCHAR})
</insert>
  1. selectKeyElements to achieve.

The way to support all databases, whether it is incremented primary key or sequence primary key.

In the MySQL database, order attribute is set to AFTER, because the primary key value of the current record in order to get to the insert statement executed successfully. In oracle database, order attribute is set to BEFORE, Oracle then need to get the value of the sequence start value into the database.

<!-- 演示 -->
<selectKey resultType="Integer" order="AFTER" keyProperty="user.userId">
    SELECT LAST_INSERT_ID() AS userId
</selectKey>

<!-- 实例 -->
<insert id="insert" parameterType="com.pinyougou.pojo.TbGoods" >
    <selectKey resultType="Long" keyProperty="id" keyColumn="goodsId" order="AFTER">
        SELECT LAST_INSERT_ID() AS goodsId
    </selectKey>
    insert into tb_goods (
        id, 
        seller_id, 
        goods_name, 
        default_item_id, 
        audit_status, 
        is_marketable, 
        brand_id, 
        caption, 
        category1_id, 
        category2_id, 
        category3_id, 
        small_pic, 
        price, 
        type_template_id, 
        is_enable_spec, 
        is_delete
    )
    values (
        #{id,jdbcType=BIGINT}, 
        #{sellerId,jdbcType=VARCHAR}, 
        #{goodsName,jdbcType=VARCHAR}, 
        #{defaultItemId,jdbcType=BIGINT}, 
        #{auditStatus,jdbcType=VARCHAR}, 
        #{isMarketable,jdbcType=VARCHAR}, 
        #{brandId,jdbcType=BIGINT}, 
        #{caption,jdbcType=VARCHAR}, 
        #{category1Id,jdbcType=BIGINT}, 
        #{category2Id,jdbcType=BIGINT}, 
        #{category3Id,jdbcType=BIGINT}, 
        #{smallPic,jdbcType=VARCHAR}, 
        #{price,jdbcType=DECIMAL}, 
        #{typeTemplateId,jdbcType=BIGINT}, 
        #{isEnableSpec,jdbcType=VARCHAR}, 
        #{isDelete,jdbcType=VARCHAR}
    )
</insert>

MySQL database test

SELECT LAST_INSERT_ID() AS userId;

insert into tb_brand values(null, '1', '2');

LAST_INSERT_ID () can get inserted record id, but it should be open affairs. Since the implementation of the two statements.

The following are other support auto-increment primary keys of database configuration selectKey return the primary key SQL

DB2 VALUES IDENTITY_VAL_LOCAL()
MYSQL SELECT LAST_INSERT_ID()
SQLSERVER SELECT SCOPE_IDENTITY()
CLOUDSCAPE VALUES IDENTITY_VAL_LOCAL()
DERBY VALUES IDENTITY_VAL_LOCAL()
HSQLDB CALL IDENTITY()
SYBASE SELECT @@IDENTITY
DB2_MF SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1
INFORMIX select dbinfo(‘sqlca.sqlerrd1’) from systables where tabid=1

Guess you like

Origin www.cnblogs.com/mozq/p/11479529.html