mybatis和ibatis插入oracle数据库并返回主键id

时间:2021-09-14 23:26:43

一、假设

DB:Oracle

Table:PRODUCT

        Sequences:SEQ_PRODUCT

实体类:Product.java


二、demo

1、mybatis插入oracle返回主键id

<!-- mybatis插入oracle,返回id -->
<insert id="saveProductInfo" parameterType="Product" flushCache="true">
insert into PRODUCT
(
id, pro_name, pro_price
)
values
(
SEQ_PRODUCT.nextval, #{proName}, #{proPrice}
)
<selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER">
select SEQ_PRODUCT.currval as id from dual
</selectKey>
</insert>



2、ibatis插入oracle返回主键id

<!-- ibatis插入oracle,返回id -->
<insert id="saveProductInfo" parameterClass="com.test.Product" >
<selectKey keyProperty="id" resultClass="int" type="pre">
select SEQ_PRODUCT.nextval as id from dual
</selectKey>
insert into TB_CONCERT_LETTER
(
id, pro_name, pro_price
)
values
(
#id#, #proName#, #proPrice#
)
</insert>