Mybatis批量插入返回插入成功后的主键id操作

网友投稿 235 2023-01-24

Mybatis批量插入返回插入成功后的主键id操作

我们都知道Mybatis在插入单条数据的时候有两种方式返回自增主键:

1、对于支持生成自增主键的数据库:增加 useGenerateKeys和keyProperty ,标签属性。

2、不支持生成自增主键的数据库:使用

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

Mybatis官网资料提供如下:

First, if your database supports auto-generated key fields (e.g. mysql and SQL Server), then you can simply set useGeneratedKeys="true"http:// 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:

keyProperty="id">

insert into Author (username,passwhttp://ord,email,bio)

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

keyProperty="id">

insert into Author (username,passwhttp://ord,email,bio)

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

id="insertAuthor" useGeneratedKeys="trbockROrue"

keyProperty="id">

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

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

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

keyProperty="id">

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

(#{item.username}, #{item.password}, #{item.email}, #{item.bio})

keyProperty="id">

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

(#{item.username}, #{item.password}, #{item.email}, #{item.bio})

id="insertAuthor" useGeneratedKeys="true"

keyProperty="id">

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

(#{item.username}, #{item.password}, #{item.email}, #{item.bio})

从官网资料可以看出Mybatis是支持批量插入时返回自增主键的。

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

解决办法:

1、升级Mybatis版本到3.3.1。官方在这个版本中加入了批量新增返回主键id的功能

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

3、Mapper.xml中使用list变量(parameterType="java.util.List")接受Dao中的参数集合。

下面是具体代码过程,可供参考

mapper.xml层代码

INSERT INTO

(relation_id, summary_id, relation_type)

VALUES

(

#{shopResource.relationId}, #{shopResource.summaryId}, #{shopResource.relationType}

)

dao实现层代码

public List batchinsertCallId(List shopResourceList)

{

this.getSqlSession().insert(getStatement(SQL_BATCH_INSERT_CALL_ID), shopResourceList);

return shopResourceList;// 重点介绍

}

补充:MyBatis 插入的同时获取主键id

有时候进行一些多步操作的时候就需要得到最新插入一条记录的id号,那么如何在插入的同时返回id号

Mapper代码:

ihttp://nsert into feeds (id, title, content,

pic, video, auther_id,

comments, favours, likes,

cover_select, views, set_time,

kind)

values (#{id,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{content,jdbcType=VARCHAR},

#{pic,jdbcType=VARCHAR}, #{video,jdbcType=VARCHAR}, #{autherId,jdbcType=VARCHAR},

#{comments,jdbcType=INTEGER}, #{favours,jdbcType=INTEGER}, #{likes,jdbcType=INTEGER},

#{coverSelect,jdbcType=INTEGER}, #{views,jdbcType=INTEGER}, #{setTime,jdbcType=VARCHAR},

#{kind,jdbcType=VARCHAR})

service层:

当设置了useGeneratedKeys="true" keyProperty="id"后,它会在你插入数据库的同时,将这个对象的id值改为最新的那个id,然后我们只需要取出他就可以了

最终效果:

数据库

输出

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:免费API接口接口源码(开发api接口教程)
下一篇:豆瓣电影api打开104(豆瓣电影怎么打开电影)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~