项目场景:
在开发中,需要通过批量插入,生成主键id,然后进行数据关联操作
问题描述
Caused by: 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 [forlonList, param1]
at org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator.processBatch(Jdbc3KeyGenerator.java:71)
at org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator.processAfter(Jdbc3KeyGenerator.java:45)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:50)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)
at com.sun.proxy.$Proxy371.update(Unknown Source)
at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50)
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)
at com.sun.proxy.$Proxy370.update(Unknown Source)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:198)
at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:185)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
... 68 more
Caused by: org.apache.ibatis.binding.BindingException: Parameter 'id' not found. Available parameters are [forlonList, param1]
at org.apache.ibatis.binding.MapperMethod$ParamMap.get(MapperMethod.java:186)
at org.apache.ibatis.reflection.wrapper.MapWrapper.getSetterType(MapWrapper.java:85)
at org.apache.ibatis.reflection.MetaObject.getSetterType(MetaObject.java:97)
at org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator.getTypeHandlers(Jdbc3KeyGenerator.java:108)
at org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator.processBatch(Jdbc3KeyGenerator.java:65)
... 93 more
批量插入后,生成主键失败,说是没有匹配到这个参数
ForlanDao.java
int batchInsert(@Param("forlonList") List<Forlan> forlonList);
ForlanMapper.xml
<insert id="batchInsert" useGeneratedKeys="true" keyProperty="id">
INSERT INTO
Forlan(name,remark)
VALUES
<foreach collection="forlonList" item="item" index="index" separator=",">
(#{item.name},#{item.remark})
</foreach>
</insert>
原因分析:
这里使用的是1.3.1版本,目前发现是版本问题,其它版本是正常的
解决方案:
本质就是,需要把foreach标签中的collection属性值改为list
-
修改dao和mapper里面的forlonList为list
ForlanDao.javaint batchInsert(@Param("list") List<Forlan> forlonList);
ForlanMapper.xml
<insert id="batchInsert" useGeneratedKeys="true" keyProperty="id"> INSERT INTO Forlan(name,remark) VALUES <foreach collection="list" item="item" index="index" separator=","> (#{item.name},#{item.remark}) </foreach> </insert>
-
去掉@Param注解,修改collection=“list”
ForlanDao.javaint batchInsert(List<Forlan> forlonList);
ForlanMapper.xml
<insert id="batchInsert" useGeneratedKeys="true" keyProperty="id"> INSERT INTO Forlan(name,remark) VALUES <foreach collection="list" item="item" index="index" separator=","> (#{item.name},#{item.remark}) </foreach> </insert>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/88946.html