好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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:

?

1

2

3

4

5

6

7

8

9

10

11

12

<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.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<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层代码

?

1

2

3

4

5

6

7

8

9

10

11

12

<!-- 批量新增 -->

  <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实现层代码

?

1

2

3

4

5

public list<shopresource> batchinsertcallid(list<shopresource> shopresourcelist)

  {

  this .getsqlsession().insert(getstatement(sql_batch_insert_call_id), shopresourcelist);

  return shopresourcelist; // 重点介绍

  }

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

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

mapper代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

<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,然后我们只需要取出他就可以了

最终效果:

数据库

输出

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

原文链接:https://blog.csdn.net/jiangeeq/article/details/55047116

查看更多关于Mybatis批量插入返回插入成功后的主键id操作的详细内容...

  阅读:28次