首页 > 百科知识 正文

oracle(操作(MyBatis与OracleMySqlSqlServer插入数据返回主键方式(mybatis)

时间:2023-11-21 06:47:37 阅读:244 作者:泪眼盈眶

MyBatis Oracle MySql SqlServer 插入 返回主键


MyBatis在insert插入数据时,返回一个自增的主键。可以通过XML的配置来实现,而数据库的不同配置有所不同,我们来看看。

Oracle

相对于MySql,Sql Server来说,Oracle插入主键返回自增稍显复杂一点,需要一个Sequence和一个Trigger,接下来看代码。Sequence:序列号,创建一个自增的序列号。

Trigger:当做insert操作的时候,触发插入一个从Sequence中获取序列插入主键中。

  • Sequence:

create sequence Seq_Test

minvalue 1

maxvalue 100000000000000

start with 1

increment by 1

nocache;缓存序列号,防止系统宕机或者其他情况导致不连续,也可以设置nocache

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • Trigger:

create or replace trigger tri_test

before insert on TableName --表名

for each row

declare

nextid number;

begin

IF :new.Id IS NULL or :new.Id=0 THEN --Id是列名,主键

select Seq_Test.nextval --Seq_Test上面创建的序列号

into nextid

from sys.dual;

:new.Id:=nextid;

end if;

end tri_test;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • XML配置

<insert id="add" parameterType="cn.crm.PO">

<selectKey resultType="java.lang.Integer" order="BEFORE(After)" keyProperty="Id">

SELECT Seq_Test.NEXTVAL FROM DUAL

</selectKey>

insert into system_user (name, parent_id,

phone_num, password, description

)

values (#{name,jdbcType=VARCHAR},

#{parent_id,jdbcType=SMALLINT},

#{phone_num,jdbcType=VARCHAR},

#{password,jdbcType=VARCHAR},

#{description,jdbcType=VARCHAR}

)

</insert>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

MySql

Mysql自带自增主键,所以只需要XML配置即可,有两种配置方式都行。

第一种:

<insert id="add" parameterType="cn.crm.PO" useGeneratedKeys="true" keyProperty="id">

insert into system_user (name, parent_id,

phone_num, password, description

)

values (#{name,jdbcType=VARCHAR},

#{parent_id,jdbcType=SMALLINT},

#{phone_num,jdbcType=VARCHAR},

#{password,jdbcType=VARCHAR},

#{description,jdbcType=VARCHAR}

)

</insert>

插入之后自动封装到PO中,直接PO.getId()就可以拿到

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

第二种:

<insert id="add" parameterType="cn.crm.PO">

<selectKey resultType="java.lang.Short" order="AFTER" keyProperty="id">

SELECT LAST_INSERT_ID() AS id

</selectKey>

insert into system_user (name, parent_id,

phone_num, password, description

)

values (#{name,jdbcType=VARCHAR},

#{parent_id,jdbcType=SMALLINT},

#{phone_num,jdbcType=VARCHAR},

#{password,jdbcType=VARCHAR},

#{description,jdbcType=VARCHAR}

)

</insert>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

SqlServer

感觉Mybatis不是很支持SqlServer,配置方式就一种:

<insert id="add" parameterType="cn.crm.PO" useGeneratedKeys="true" keyProperty="id">

insert into system_user (name, parent_id,

phone_num, password, description

)

values (#{name,jdbcType=VARCHAR},

#{parent_id,jdbcType=SMALLINT},

#{phone_num,jdbcType=VARCHAR},

#{password,jdbcType=VARCHAR},

#{description,jdbcType=VARCHAR}

)

</insert>

和MySql配置方式是一样的。

oracle(操作(MyBatis与OracleMySqlSqlServer插入数据返回主键方式(mybatis)-第1张

版权声明:该问答观点仅代表作者本人。如有侵犯您版权权利请告知 cpumjj@hotmail.com,我们将尽快删除相关内容。