前言:
在开发中,我们可能很多的时候可能需要在新增时得到刚才新增的id,后续的逻辑需要用到这个id。
在插入单条记录的情况下,这个是很简单的问题。多条记录时有个坑在里面。
单条记录的代码如下

<insert >
    insert into person 
	  (
	    name,
	    age
	  ) values
	  (
	    #{name},
	    #{age}
	   )
</insert>

这样插入完后,在person类中id就可以得到刚才最新插入的值了。

多条记录的代码如下

<insert >
        insert into person
		(
		  name,
	      age
		) values
		<foreach collection="list" item="item" separator=",">
		(
		#{item.name},
	    #{item.age}
		)
		</foreach> 
</insert>

首先这样的写法是对的,好多的地方也是这样写的。
我当初也是这样写的,但一直不行,最后找了好长时间的资料才发现,是mybatis的版本的问题。
最开始的时候用的是3.2.8的版本,但是这个需要至少升级到3.3.1版本才能支持。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-08
  • 2021-10-16
  • 2021-12-01
  • 2021-06-11
  • 2021-07-23
猜你喜欢
  • 2021-12-28
  • 2022-12-23
  • 2021-07-21
  • 2022-12-23
相关资源
相似解决方案