我正在尝试在mybatis foreach中绑定多个参数,这在昨晚运行得很好。
以下是我的代码
<insert id="insertWbs" parameterType="HashMap">
<if test="paramList.size != 0">
BEGIN
<foreach collection="paramList" item="item" separator=";">
insert into wbs(proj_no, task_name, wbs_worker, wbs_tester, wbs_ex_start, wbs_ex_end, wbs_unique, task_status, wbs_parent)
select #{item.proj_no}, #{item.task_name}, #{item.wbs_worker}, #{item.wbs_tester}, to_date(#{item.wbs_ex_start}, 'yyyy-mm-dd'), to_date(#{item.wbs_ex_end}, 'yyyy-mm-dd'), #{wbs_unique}, #{item.task_status}, #{item.wbs_parent} from dual where not exists
(select wbs_unique from wbs where wbs_unique = #{wbs_unique})
</foreach>;
END;
</if>
</insert>正如你们所看到的,参数类型是HashMap,集合paramList是映射中的键之一。
现在这段代码抛出的错误如下
There is no getter for property named 'proj_no' in 'class java.lang.String'奇怪的是,它昨晚运行得很好。即使是运行在相同逻辑上的代码也能正常工作。
<update id="updateWbs" parameterType="HashMap">
<if test="paramList.size != 0">
BEGIN
<foreach collection="paramList" item="item" separator=";">
<if test="#{item.wbs_start} != null and #{item.wbs_end} != null">
update wbs set task_name = #{item.task_name}, wbs_worker = #{item.wbs_worker}, wbs_tester = #{item.wbs_tester}, task_status = #{item.task_status}, wbs_start = to_date(#{item.wbs_start}, 'yyyy-mm-dd'), wbs_end = to_date(#{item.wbs_end}, 'yyyy-mm-dd')
where wbs_unique = #{item.wbs_unique}
</if>
<if test="#{item.wbs_start} != null and #{item.wbs_end} == null">
update wbs set task_name = #{item.task_name}, wbs_worker = #{item.wbs_worker}, wbs_tester = #{item.wbs_tester}, task_status = #{item.task_status}, wbs_start = to_date(#{item.wbs_start}, 'yyyy-mm-dd')
where wbs_unique = #{item.wbs_unique}
</if>
<if test="#{item.wbs_start} == null and #{item.wbs_end} != null">
update wbs set task_name = #{item.task_name}, wbs_worker = #{item.wbs_worker}, wbs_tester = #{item.wbs_tester}, task_status = #{item.task_status}, wbs_end = to_date(#{item.wbs_end}, 'yyyy-mm-dd')
where wbs_unique = #{item.wbs_unique}
</if>
<if test="#{item.wbs_start} == null and #{item.wbs_end} == null">
update wbs set task_name = #{item.task_name}, wbs_worker = #{item.wbs_worker}, wbs_tester = #{item.wbs_tester}, task_status = #{item.task_status}
where wbs_unique = #{item.wbs_unique}
</if>
</foreach>;
END;
</if>
</update>我真的不知道我错过了什么。我已经尽我所能了..。
请帮我解决这个问题。
发布于 2018-07-29 01:21:26
根据mybatis documentation
使用地图(或Map.Entry对象的集合)时,索引将是关键对象,项目将是值对象
这意味着当您将映射作为参数传递给foreach时,循环将覆盖映射值。在您的示例中,必须具有值为字符串的键proj_no、task_name等:
{
'proj_no' -> 'some-project-number',
'task_name' -> 'some-task-name',
// and so on
}如果您将Map传递给foreach,那么item将在第一次迭代中使用some-project-number,在第二次迭代中使用some-task-name,依此类推。#{item.proj_no}尝试从该对象获取一个名为proj_no的属性。因为它是一个字符串,所以它找不到,并在错误消息中显示。
如果您传递的List包含一个带有键proj_no、task_name等的Map,那么您的代码就可以正常工作,因为在这种情况下,foreach循环中的item将是整个映射。如果您只需要传递单个映射,则不需要foreach,请执行以下操作:
<insert id="insertWbs" parameterType="HashMap">
insert into wbs(proj_no, task_name, wbs_worker, wbs_tester, wbs_ex_start, wbs_ex_end, wbs_unique, task_status, wbs_parent)
select #{paramList.proj_no}, #{paramList.task_name}, #{paramList.wbs_worker}, #{paramList.wbs_tester}, to_date(#{paramList.wbs_ex_start}, 'yyyy-mm-dd'), to_date(#{paramList.wbs_ex_end}, 'yyyy-mm-dd'), #{wbs_unique}, #{paramList.task_status}, #{paramList.wbs_parent} from dual where not exists
(select wbs_unique from wbs where wbs_unique = #{wbs_unique})
</insert>https://stackoverflow.com/questions/51493983
复制相似问题