keyProperty相关
bug:
//原因:org.apache.ibatis.executor.ExecutorException:无法确定将生成的键分配给哪个参数。 //请注意,当有多个参数时,“ keyProperty”必须包含参数名称(例如“ param.id”)。 //指定的键属性为[id],可用参数为[menuId,userId,param1,param2]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 [menuId, userId, param1, param2]
相关代码:
<insert id="create" keyProperty="id" useGeneratedKeys="true" parameterType="com.nuc.entity.UserMenu">insert into tb_user_menu(user_id,menu_id)values(#{userId},#{menuId})
</insert>
public int create(@Param("userId") Integer userId,@Param("menuId") Integer menuId);
原因:
create方法中有两个参数,mybatis并不知道keyProperty = "id"
这个的id要赋值给哪个参数的属性,所以需改为:
<insert id="create" keyProperty="userId.id" useGeneratedKeys="true" parameterType="com.nuc.entity.UserMenu">insert into tb_user_menu(user_id,menu_id)values(#{userId},#{menuId})
</insert>
如果传参只有一个对象的参数,去使用对象名.属性的方式也会报错,一个参数时,只需要为keyProperty="id"
总结
useGeneratedKeys
的意思是向传入的Model赋值自增长id 。而keyProperty
表示将自增长id赋值给哪个字段。所以当传入两个参数时,会报如上错误;
可参照getAssignerForParamMap
源码:
private Entry<String, KeyAssigner> getAssignerForParamMap(Configuration config, ResultSetMetaData rsmd,int columnPosition, Map<String, ?> paramMap, String keyProperty, String[] keyProperties, boolean omitParamName) {//判断paramMap是否只有一个唯一keyboolean singleParam = paramMap.values().stream().distinct().count() == 1;//获取keyProperty的.的位置int firstDot = keyProperty.indexOf('.');//如果是多个参数,但是keyProperty又不包含.,会抛出错误if (firstDot == -1) {if (singleParam) {return getAssignerForSingleParam(config, rsmd, columnPosition, paramMap, keyProperty, omitParamName);}throw new 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 " + ArrayUtil.toString(keyProperties) + " and available parameters are "+ paramMap.keySet());}//多参数,需要截取keyProperty的.之前部分,去paramMap拿对应值String paramName = keyProperty.substring(0, firstDot);if (paramMap.containsKey(paramName)) {String argParamName = omitParamName ? null : paramName;String argKeyProperty = keyProperty.substring(firstDot + 1);return entry(paramName, new KeyAssigner(config, rsmd, columnPosition, argParamName, argKeyProperty));} else if (singleParam) {return getAssignerForSingleParam(config, rsmd, columnPosition, paramMap, keyProperty, omitParamName);} else {throw new ExecutorException("Could not find parameter '" + paramName + "'. "+ "Note that when there are multiple parameters, 'keyProperty' must include the parameter name (e.g. 'param.id'). "+ "Specified key properties are " + ArrayUtil.toString(keyProperties) + " and available parameters are "+ paramMap.keySet());}}
keyProperty相关相关推荐
- Java Web 相关面试题总结
为什么80%的码农都做不了架构师?>>> Spring 相关的的面试题 1.什么是 IoC 容器? 2.解释下 Spring Bean 的生命周期 IoC 容器是 Spring ...
- 【高校宿舍管理系统】第二章 整合Mybatis和写CRUD的基本流程以及使用代码生成器生成Mapper等相关代码
第二章 整合Mybatis和写CRUD的基本流程以及使用代码生成器生成Mapper等相关代码 提示:本博客个为人独立博客,不是权威,仅供参考!所有思路只做交流之用!如有不足之处,望各位在评论区友善指正 ...
- hibernate相关知识
hibernate多对一关联映射 关联映射的本质:将关联关系映射到数据库,关联关系在对象模型域中体现为一个或多个引用 <many-to-one>标签会在"多"的一端添加 ...
- Oracle序列相关命令
Oracle序列相关命令 1.Oracle创建序列命令 2.Oracle序列查询命令 3.Oracle序列删除命令 4.Oracle序列实际应用 5.注意事项 1.Oracle创建序列命令 CREAT ...
- spring 配置相关
1.spring配置相关 初始化配置文件 web.xml <context-param> <param-name>contextConfigLocation&l ...
- Java相关面试题总结及答案
Java ArrayList Vector LinkedList Set区别 - ArrayList: 底层数组实现,通过索引访问,o(1)查询时间复杂度,插入删除性能不佳,线程不安全,需要扩容时扩大 ...
- 电商项目实战--用户相关
1. 项目分析 首先应该分析该项目中需要处理哪些种类的数据,在本项目中有:商品.商品类别.用户.收货地址.购物车.收藏.订单-- 然后,确定以上这些数据的开发顺序,原则上应该先开发基础数据和简单的数据 ...
- Mybatis相关知识汇总
1.#{}和${} 在mybatis中#{}与${}的区别 先来看两个例子,假设在数据库中有一个name字段,它的类型是varchar,当通过这个字段进行查询语句时,在xml文件中可以使用where ...
- Redis 笔记(02)— keys 键相关命令(查询数据库key数量、判断key是否存在、指定key过期时间、查看key类型、查看key剩余秒数、选择数据库、删除key、删除数据库)
1. keys 键相关命令 命令 说明 dbsize 返回当前数据路的key数量 exists key 测试指定key是否存在 expire key seconds 为key指定过期时间 type k ...
最新文章
- SVN之版本控制系统
- 简单介绍4种限流算法!(固定窗口计数器算法、滑动窗口计数器算法、漏桶算法、令牌桶算法)...
- jetson nano 人脸
- 一行 Python 代码制作七夕节爱心
- linux是基于什么的软件模式进行发布的,《Linux操作系统与应用项目教程》习题.doc...
- 【Socket】linux网络多路复用IO技术
- Java加载sklearn训练好的模型进行预测(无法搞定)
- ireport 循环_ireport5.6.0分组显示
- php7 mcrypt模块_Linux下PHP安装mcrypt扩展模块笔记
- python 进位_蓝桥杯-Python-高精度加法
- MISRA C编程规范标准
- 图片像素转换工具(pixelmash 2020) v2020.1.0
- 【Unity游戏开发】动画系统(二)2D动画
- sql语句练习题或面试题
- ESP32 入门笔记01:乐鑫ESP32-DevKitC开发板信息、开发环境搭建以及学资料准备
- 外交学院计算机考研,外交学院考研经验
- 美柚社区精选:贴心宝妈的八大育儿经验
- 29. Compose实现瀑布流 StaggeredGrid
- 虚拟机ping外网连接失败解决方法
- PMarca的创业指导