我有一个带有复合主键的表,我想对所有键进行批量插入(数据迁移/暂存)。
目前看来,如果将集合传递给 MyBatis 插入,它期望所有元素都是 ResultMap 类型。也许我的配置中缺少一些管道,但是是否有可能让 MyBatis 将 Apache Commons Pair (或任何其他 K/V 结构,我不挑剔)理解为集合元素来完成以下工作?
这是映射器的插入内容
<insert id="saveUsers">
INSERT OR UPDATE INTO UserConfig(accountNumber,
userId,
role,
...)
VALUES (
<foreach item="entry" index="index" collection="accountUsers" separator=",">
(#{entry.left, javaType=com.my.company.dataid.account.AccountNumber},
#{entry.right.userId},
#{entry.right.role},
...
)
</foreach>
)
</insert>
这是它的 Java 接口
void saveUsers(@Param("accountUsers") final List<Pair<AccountNumber, User>> accountUsers);
MyBatis 的错误
Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'left' in 'class com.my.company.common.entity.User'
at org.apache.ibatis.reflection.Reflector.getGetInvoker(Reflector.java:385)
at org.apache.ibatis.reflection.MetaClass.getGetInvoker(MetaClass.java:160)
at org.apache.ibatis.reflection.wrapper.BeanWrapper.getBeanProperty(BeanWrapper.java:156)
at org.apache.ibatis.reflection.wrapper.BeanWrapper.get(BeanWrapper.java:50)
at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:115)
at org.apache.ibatis.reflection.wrapper.BaseWrapper.getChildValue(BaseWrapper.java:121)
at org.apache.ibatis.reflection.wrapper.MapWrapper.get(MapWrapper.java:42)
at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:115)
at org.apache.ibatis.mapping.BoundSql.getAdditionalParameter(BoundSql.java:74)
at org.apache.ibatis.scripting.defaults.DefaultParameterHandler.setParameters(DefaultParameterHandler.java:73)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.parameterize(PreparedStatementHandler.java:97)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.parameterize(RoutingStatementHandler.java:65)
感谢上面评论中的@ave。
就我而言,我必须显式设置 foreach 的索引和项目位
<foreach collection="accountUsers" index="index" item="item" separator=",">
(#{index, javaType=com.my.company.dataid.account.AccountNumber},
#{item.userId},
#{item.role},
然后列出