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

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

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

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

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

Mybatis官网资料提供如下:

First, if your database supports auto-generated key fields (e.g. 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> 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=","> (#{item.username}, #{item.password}, #{item.email}, #{item.bio}) </foreach></insert> id="insertAuthor" useGeneratedKeys="true" keyProperty="id"> insert into Author (username, password, email, bio) values <foreach item="item" collection="list" separator=","> (#{item.username}, #{item.password}, #{item.email}, #{item.bio}) </foreach></insert>

从官网资料可以看出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 id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id" > INSERT INTO <include refid="t_shop_resource" /> (relation_id, summary_id, relation_type) VALUES <foreach collection="list" index="index" item="shopResource" separator=","> ( #{shopResource.relationId}, #{shopResource.summaryId}, #{shopResource.relationType} ) </foreach> </insert>

dao实现层代码

public List<ShopResource> batchinsertCallId(List<ShopResource> shopResourceList) { this.getSqlSession().insert(getStatement(SQL_BATCH_INSERT_CALL_ID), shopResourceList); return shopResourceList;// 重点介绍 } 

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

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

Mapper代码:

<insert id="insertFeeds" parameterType="com.yj.pojo.Feeds" useGeneratedKeys="true" keyProperty="id"> insert 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}) </insert>

service层:

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

最终效果:

数据库

输出

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

穿别人的鞋,走自己的路,让别追去吧

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

相关文章:

你感兴趣的文章:

标签云: