mybatis批量插入,怎么返回生成的自增主键

mybatis批量插入,怎么返回生成的自增主键,第1张

我们项目组正好用到了这个,SEQ_ZONE为sequence,则mybatis配置文件如下: SELECT SEQ_ZONECURRVAL AS id from dual insert into TBL_ZONE (ID, NAME ) values (SEQ_ZONENEXTVAL, #{name,jdbcType=VARCHAR} )

案例:

方法

<insert

id="add"

parameterType="EStudent"

useGeneratedKeys="true"

keyProperty="id">

insert

into

TStudent(name,

age)

values(#{name},

#{age})

</insert>

useGeneratedKeys="true"

:设置是否使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。

(适用于mysql、sqlserver数据库,oracle不能使用,使用selectkey子节点做)

keyProperty:赋值的对象的属性名称。

方法二

<insert

id="add"

parameterType="EStudent">

//

下面是SQLServer获取最近一次插入记录的主键值的方式

<selectKey

resultType="int"

keyProperty="id"

order="AFTER">

SELECT

LAST_INSERT_ID()

AS

id

</selectKey>

insert

into

TStudent(name,

age)

values(#{name},

#{age})

</insert>

1、对于支持生成自增主键的数据库:useGenerateKeys和keyProperty。

2、不支持生成自增主键的数据库:<selectKey>。

但是怎对批量插入数据返回自增主键的解决方式网上看到的还是比较少,至少百度的结果比较少。

Mybatis官网资料提供如下:

First, if your database supports auto-generated key fields (eg MySQL and SQL Server), then you can simply set useGeneratedKeys="true" and set the keyProperty to the target property and you're done For example, if the Authortable above had used an auto-generated column type for the id, the statement would be modified as follows:

<insert id="insertAuthor" useGeneratedKeys="true"

keyProperty="id">

insert into Author (username,password,email,bio)

values (#{username},#{password},#{email},#{bio})

</insert>

If your database also supports multi-row insert, you can pass a list or an array of Authors and retrieve the auto-generated keys

<insert id="insertAuthor" useGeneratedKeys="true"

keyProperty="id">

insert into Author (username, password, email, bio) values

<foreach item="item" collection="list" separator=",">

(#{itemusername}, #{itempassword}, #{itememail}, #{itembio})

</foreach>

</insert>

从官网资料可以看出Mybatis是支持批量插入时返回自增主键的。(百度上说不支持的,多打脸 开玩笑的)

但是在本地测试的时候使用上述方式确实不能返回自增id,而且还报错(不认识keyProperty中指定的Id属性),然后在网上找相关资料。终于在Stackoverflow上面找到了一些信息。

解决办法:

1、升级Mybatis版本到331。

2、在Dao中不能使用@param注解。

3、Mapperxml中使用list变量接受Dao中的集合。

参考地址:

>

一、首先我们看对于同一张student表,对于mysql,sql server,oracle中它们都是怎样创建主键的

1、在mysql中

create table Student(

Student_ID int(6) NOT NULL PRIMARY KEY AUTO_INCREMENT,

Student_Name varchar(10) NOT NULL,

Student_Age int(2) NOT NULL

);

insert into student(student_name,student_age) values('zhangsan',20);

2、在sql server中

create table Student(

Student_ID int primary key identity(1,1),

Student_Name varchar2(10) NOT NULL,

Student_Age number(2) NOT NULL

);

insert into student(student_name,student_age) values('zhangsan',20);

3、在oracle中

create table Student(

Student_ID number(6) NOT NULL PRIMARY KEY,

Student_Name varchar2(10) NOT NULL,

Student_Age number(2) NOT NULL

);

而oracle如果想设置主键自增长,则需要创建序列

CREATE SEQUENCE student_sequence

INCREMENT BY 1

NOMAXVALUE

NOCYCLE

CACHE 10;

insert into Student values(student_sequencenextval,'aa',20);

如果使用了触发器的话,就更简单了

create or replace trigger student_trigger

before insert on student

for each row

begin

select student_sequencenextval into :newstudent_id from dual;

end student_trigger;

此时插入的时候触发器会帮你插入id

insert into student(student_name,student_age) values('wangwu',20);

至此,mysql,sql server,oracle中怎样创建表中的自增长主键都已完成。

看一看出oracle的主键自增较mysql和sql sever要复杂些,mysql,sqlserver配置好主键之后,插入时,字段和值一一对应即可,数据库就会完成你想做的,但是在oracle由于多了序列的概念,如果不使用触发器,oracle怎样实现主键自增呢?

<insert id="add" parameterType="Student">

<selectKey keyProperty="student_id" resultType="int" order="BEFORE">

select student_sequencenextval from dual

</selectKey>

insert into student(student_id,student_name,student_age) values(#{student_id},#{student_name},#{student_age})

</insert>

或者

<insert id="save" parameterType="comthreetitoZoneTO" >

<selectKey resultType="javalangLong" keyProperty="id" order="AFTER" >

SELECT SEQ_ZONECURRVAL AS id from dual

</selectKey>

insert into TBL_ZONE (ID, NAME ) values (SEQ_ZONENEXTVAL, #{name,jdbcType=VARCHAR})

</insert>

二、MyBatis 插入时候获取自增主键方法有二

以MySQL55为例:

方法1:

<insert id="insert" parameterType="Person" useGeneratedKeys="true" keyProperty="id">

insert into person(name,pswd) values(#{name},#{pswd})

</insert> 

方法2:

<insert id="insert" parameterType="Person">

<selectKey keyProperty="id" resultType="long">

select LAST_INSERT_ID()

</selectKey>

insert into person(name,pswd) values(#{name},#{pswd})

</insert>

插入前实体id属性为0;

插入后实体id属性为保存后自增的id;

以上就是关于mybatis批量插入,怎么返回生成的自增主键全部的内容,包括:mybatis批量插入,怎么返回生成的自增主键、通过mybatis添加数据记录时,如何返回主键、mybaitis批量插入怎么得到批量返回的自增ID等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9599512.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存