mybatis 3.x 升级时遇到的keyProperty问题小坑

背景:
有1个项目,原来是用的mybatis 3.4.6版本,其中有一些插入mapper是这样写的:
Integer insertEntitySelectiveShard(@Param("tableSuffix") String tableSuffix,@Param("entity") XXXEntity entity);

对应的xml片段:

<insert id="insertEntitySelectiveShard" parameterType="com.cnblogs.yjmyzz.dao.entity.XXXEntity" useGeneratedKeys="true" keyProperty="id">

 可能有同学说了,按官网文档的说法,keyProperty这里写法不规范:

既然是对象的属性,正确的写法应该是 keyProperty="entity.id",但该项目id生成方式,后来改用snowflake分布式id算法,在insert前entity.id上已赋值了,也就无需mybatis在insert后自动返回,关键的是3.4.6版本,遇到这种不规范的写法,并不会报错,所以也就一直这样跑着。

当升级到3.5.13后,运行报错:

org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: org.apache.ibatis.executor.ExecutorException: Could not determine which parameter to assign generated keys to. Note that when there are multiple parameters, 'keyProperty' must include the parameter name (e.g. 'param.id'). Specified key properties are [id] and available parameters are [tableSuffix, param1, entity, param2]
 
经过实测,结论如下:
1、只有1个参数时,加不加@Param("entity") 都不会报错
2、大于1个参数时,keyProperty必须写成规范的entity.id,否则报错
3、不管是几个参数,keyColumn=“id" 始终不会报错(也建议用该方式,前提是表上的主键字段名就是id)  
posted @   菩提树下的杨过  阅读(651)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
历史上的今天:
2011-06-17 Silverlight:纠结的快捷键问题
2009-06-17 [转贴]dbcached──“分布式 key-value 数据库内存缓存系统”
2009-06-17 Linq排序效率 Vs 快速排序效率
点击右上角即可分享
微信分享提示