深入分析Mybatis 使用useGeneratedKeys获取自增主键
摘要
我们经常使用useGenerateKeys来返回自增主键,避免多一次查询。也会经常使用on duplicate key update,来进行insertOrUpdate,来避免先query 在insert/update。用起来很爽,但是经常踩坑,还不知为何。本篇就是深入分析获取自增主键的原理。
问题
首先摘两段我司一些老代码的bug
批量插入用户收藏
for (tries = 0; tries < MAX_RETRY; tries++) {
final int result = collectionMapper.insertCollections(collections);
if (result == collections.size()) {
break;
}
}
if (tries == MAX_RETRY) {
throw new RuntimeSqlException("Insert collections error");
}
// 依赖数据库生成的collectionid
return collections;
collectionMapper.insertCollections 方法
<insert id="insertCollections" parameterType="list" useGeneratedKeys="true"
keyProperty="collectionId">
INSERT INTO collection(
userid, item
)
VALUES
<foreach collection="list" item="collection" separator=",">
(#{collection.userId}, #{collection.item})
</foreach>
ON DUPLICATE KEY UPDATE
status = 0
</insert>
不知道大家能不能发现其中的问题
分析
问题有两个
返回值result的判断错误
使用on duplicate key 批量update返回影响的行数是和插入的数不一样的。犯这种错主要在于想当然,不看文档
看下官网文档
写的很清楚
With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values. If you specify the CLIENT_FOUND_ROWS flag to the mysql_real_connect() C API function when connecting to mysqld, the affected-rows value is 1 (not 0) if an existing row is set to its current values.
返回值有三种
0: 没有更新 1 :insert 2. update
还有一个特殊情况,update 一个相同值到原来的值,这个根据客户端配置,可能为0,可能为1。
所以这个判断明显错误
利用批量InsertOrUpdate的userGeneratedKey来返回自增主键
这个问题批量插入时有update语句时,就会发现有问题。返回的自增主键都是错的,这是为什么呢?
1. 首先我们看下mybatis对于useGeneratedKey的描述
>This tells MyBatis to use the JDBC getGeneratedKeys method to retrieve keys generated internally by the database (e.g. auto increment fields in RDBMS like MySQL or SQL Server). Default: false.
就是使用JDBC的getGeneratedKeys的方法来获取的。
2. 我们再找下JDBC的规范
Before version 3.0 of the JDBC API, there was no standard way of retrieving key values from databases that supported auto increment or identity columns. With older JDBC drivers for MySQL, you could always use a MySQL-specific method on the Statement interface, or issue the query SELECT LAST_INSERT_ID() after issuing an INSERT to a table that had an AUTO_INCREMENT key. Using the MySQL-specific method call isn't portable, and issuing a SELECT to get the AUTO_INCREMENT key's value requires another round-trip to the database, which isn't as efficient as possible. The following code snippets demonstrate the three different ways to retrieve AUTO_INCREMENT values. First, we demonstrate the use of the new JDBC 3.0 method getGeneratedKeys() which is now the preferred method to use if you need to retrieve AUTO_INCREMENT keys and have access to JDBC 3.0. The second example shows how you can retrieve the same value using a standard SELECT LAST_INSERT_ID() query. The final example shows how updatable result sets can retrieve the AUTO_INCREMENT value when using the insertRow() method.
意思就是JDBC3.0以前,有些乱七八糟的定义的,没有统一,之后统一成了getGeneratedKeys()方法。两边是一致的。实现的原理主要就是数据库端返回一个LAST_INSERT_ID。这个跟auto_increment_id强相关。
我们看下auto_increment_id的定义。重点关注批量插入
For a multiple-row insert, LAST_INSERT_ID() and mysql_insert_id() actually return the AUTO_INCREMENT key from the first of the inserted rows. This enables multiple-row inserts to be reproduced correctly on other servers in a replication setup.
批量插入的时候只会返回一个id,这个id值是第一个插入行的AUTO_INCREMENT值。至于为什么这么干,能够使得mysql-server在master-slave架构下也能保证id值统一的原因可以看下这篇。本篇文章就不展开了。
那么mysql server只返回一个id,客户端批量插入的时候怎么能实现获取全部的id呢
3. 客户端的实现
我们看下客户端getGeneratedKeys的实现。
JDBC com.mysql.jdbc.StatementImpl
public synchronized ResultSet getGeneratedKeys() throws SQLException {
if (!this.retrieveGeneratedKeys) {
throw SQLError.createSQLException(Messages.getString("Statement.GeneratedKeysNotRequested"), "S1009", this.getExceptionInterceptor());
} else if (this.batchedGeneratedKeys == null) {
// 批量走这边的逻辑
return this.lastQueryIsOnDupKeyUpdate ? this.getGeneratedKeysInternal(1) : this.getGeneratedKeysInternal();
} else {
Field[] fields = new Field[]{new Field("", "GENERATED_KEY", -5, 17)};
fields[0].setConnection(this.connection);
return ResultSetImpl.getInstance(this.currentCatalog, fields, new RowDataStatic(this.batchedGeneratedKeys), this.connection, this, false);
}
}
看下调用的方法 this.getGeneratedKeysInternal()
protected ResultSet getGeneratedKeysInternal() throws SQLException {
// 获取影响的行数
int numKeys = this.getUpdateCount();
return this.getGeneratedKeysInternal(numKeys);
}
这里有个重要知识点了,首先获取本次批量插入的影响行数,然后再执行具体的获取id操作。
getGeneratedKeysInternal方法
protected synchronized ResultSet getGeneratedKeysInternal(int numKeys) throws SQLException {
Field[] fields = new Field[]{new Field("", "GENERATED_KEY", -5, 17)};
fields[0].setConnection(this.connection);
fields[0].setUseOldNameMetadata(true);
ArrayList rowSet = new ArrayList();
long beginAt = this.getLastInsertID();
// 按照受影响的范围+递增步长
for(int i = 0; i < numKeys; ++i) {
if (beginAt > 0L) {
// 值塞进去
row[0] = StringUtils.getBytes(Long.toString(beginAt));
}
beginAt += (long)this.connection.getAutoIncrementIncrement();
}
}
迭代影响的行数,然后依次获取id。
所以批量insert是正确可以返回的。
但是批量insertOrUpdate就有问题了,批量insertOrUpdate的影响行数不是插入的数据行数,可能是0,1,2这样就导致了自增id有问题了。
比如插入3条数据,2条会update,1条会insert,这时候updateCount就是5,generateid就会5个了,mybatis然后取前3个塞到数据里,显然是错的。
以上是原理分析,如果想了解更详细的实验结果,可以看下实验
总结
批量insert
<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>
来自官网的例子,mapper中不能指定@Param参数,否则会有问题
批量insertOrUpdate
不能依赖useGeneratedKey返回主键。
关注公众号【方丈的寺院】,第一时间收到文章的更新,与方丈一起开始技术修行之路
参考
https://blog.csdn.net/slvher/article/details/42298355
https://blog.csdn.net/qq_27680317/article/details/81118070#(3)最佳实践
https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-usagenotes-last-insert-id.html
https://dev.mysql.com/doc/refman/5.5/en/innodb-auto-increment-handling.html
https://dev.mysql.com/doc/refman/5.5/en/example-auto-increment.html
深入分析Mybatis 使用useGeneratedKeys获取自增主键的更多相关文章
- MyBatis插入时候获取自增主键方法
方法一:(Oralce不支持这种写法) useGeneratedkeys 默认值为 false,含义:设置是否使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty ...
- mybatis查询语句获取自增主键
第一种方式: 主键回填useGeneratedKeys 代表采用JDBC的Statment对象的getGeneratedKeys方法返回主键keyProperty 代表将用哪个POJO的属性去匹配这个 ...
- mybatis框架(6)---mybatis插入数据后获取自增主键
mybatis插入数据后获取自增主键 首先理解这就话的意思:就是在往数据库表中插入一条数据的同时,返回该条数据在数据库表中的自增主键值. 有什么用呢,举个例子: 你编辑一条新闻,同时需要给该新闻打上标 ...
- Mybatis useGeneratedKeys 填充自增主键值(使用Mysql)的原理分析
一.Mybatis配置 <insert id="insert" parameterType="com.test.TestDO" keyProperty=& ...
- MyBatis映射文件1(增删改、insert获取自增主键值)
增删改 Mybatis为我们提供了<insert>.<update>.<delete>标签来对应增删改操作 在接口中写增删改的抽象方法 void addEmp(Em ...
- MyBatis 3.2.6插入时候获取自增主键方法
MyBatis 3.2.6插入时候获取自增主键方法有二 以MySQL5.5为例: 方法1: <insert id="insert" parameterType="P ...
- Mybatis学习笔记4 - 获取自增主键的值
获取自增主键的值:mysql支持自增主键,自增主键值的获取,mybatis也是利用statement.getGenreatedKeys():useGeneratedKeys="true&qu ...
- mybatis执行insert后马上能获取自增主键的语句写法
<!--keyColumn keyProperty useGeneratedKeys 用于在插入数据后,能直接使用user.getId()获取主键--> <insert id=&qu ...
- Mybatis获取自增主键的值
pojo: public class User { private Integer id; private String name; private String pwd; setter和getter ...
随机推荐
- Sring 的 @AliasFor 使用规则
一.该标签存在的意义 顾名思义 @AliasFor 表示别名,它可以注解到自定义注解的两个属性上,表示这两个互为别名,也就是说这两个属性其实同一个含义.该标签存在的含义,从网上查发现有个点, 若 自 ...
- 在 树莓派(Raspberry PI) 中使用 Docker 运行 MySQL
在 树莓派(Raspberry PI) 中使用 Docker 运行 MySQL 本文主要利用 biarms 提供的 Dockerfile 进行安装. 笔者最新发现! MySQL 5.7 Docker ...
- Docker最简单入门之(一)——介绍和配置Docker
0. 前言 最近学完了Dokcer,特别记录一下,算是对自己学习成果的一个总结.以便自己能够更好的理解Docker.粗略估计了一下,我大概会分成4个部分,只记录一下常用的操作,至于一些比较难的操作或者 ...
- 值得花费一周研究的算法 -- KMP算法(indexOf)
KMP算法是由三个科学家(kmp分别是他们名字的首字母)创造出来的一种字符串匹配算法. 所解决的问题: 求文本字符串text内寻找第一次出现字符串s的下标,若未出现返回-1. 例如 text : &q ...
- JavaWeb——JSP开发1
1.什么是jsp,为什么要使用jsp. 再使用idea创建完一个web工程后,在webapp目录下会生成一个index.jsp 直接编译运行,网站将自动打开这样一个网页: 所以我们可以推测这个inde ...
- eclipse项目导入idea jdk版本不一致😵
在导入eclipse项目到idea过程中遇到 Imported project refers to unkonwn jdks JavaSE-1.8 解决方法: file --> Project ...
- Python3基本数据类型之列表
1.初识列表 列表(List)是Python3中的"容器型"数据类型. 列表通过中括号把一堆数据括起来的方式形成,列表的长度不限. 列表里面的元素可以是不同的数据类型,但是一般是相 ...
- (二十一)c#Winform自定义控件-气泡提示
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- Sqlserver 使用.net查询被事务锁住处理
在有些应用场景中.事务开了后或能需要再去调用某个DAL读取表中的数据.但DAL里又是新开的链接,由于外面有事务所以这里读的时候就读不到.这种情况下可以在这个DAL的查询里开一个新的事务级别设为允许脏读 ...
- 浏览器DOM渲染及阻塞问题
在准备面试,然后复习到了计网的知识点,紧接着又扯到了url从输入到浏览器渲染的那个问题,这里来顺便完善补充一下,本文的重点在渲染 上面的图就是浏览器从服务器请求来页面后渲染的全过程 这里我们分开来看: ...