Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

83页,批量回写主键,keyProperty="id",提示无法识别 #58

Open
E-Kunt opened this issue Aug 6, 2018 · 4 comments
Open

83页,批量回写主键,keyProperty="id",提示无法识别 #58

E-Kunt opened this issue Aug 6, 2018 · 4 comments

Comments

@E-Kunt
Copy link

E-Kunt commented Aug 6, 2018

如果要在 MySQL 中实现批量插入返回自增主键值,只需要在原来代码基础上做如下修改。
<insert id="insertList" useGeneratedKeys="true" keyProperty="id">
和单表一样,增加了useGeneratedKeys和keyProperty两个属性,增加这两个属性后,简单修改测试类,输出 id 值。

//在调用insertList之后
for(SysUser user : userList){
System.out.println(user.getId());
}
执行测试后,可以看到id部分的日志如下。

1023
1024

org.apache.ibatis.exceptions.PersistenceException:

Error updating database. Cause: org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: org.apache.ibatis.binding.BindingException: Parameter 'id' not found. Available parameters are [collection, list]

The error may involve defaultParameterMap

The error occurred while setting parameters

SQL: insert into sys_user( user_name, user_password, user_email, user_info, head_img, create_time ) values ( ?, ?, ?, ?, ?, ? ) , ( ?, ?, ?, ?, ?, ? )

Cause: org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: org.apache.ibatis.binding.BindingException: Parameter 'id' not found. Available parameters are [collection, list]

at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)

……

@E-Kunt
Copy link
Author

E-Kunt commented Aug 6, 2018

keyProperty="id" 是否有误 或 另需配置? 请教一下 谢谢

@abel533
Copy link
Contributor

abel533 commented Aug 14, 2018

用的什么版本?

@E-Kunt
Copy link
Author

E-Kunt commented Aug 19, 2018

mysql 5.7.11 驱动5.1.38 mybatis 3.3.0

@pluone
Copy link

pluone commented Mar 4, 2021

mysql 5.7.11 驱动5.1.38 mybatis 3.3.0

作者提交的批量回写主键的功能在mybatis 3.3.1 才被支持,所以你需要提高mybatis的版本
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants