问题描述:
想要实现这个接口,同时传入planId和projectPlans,屡次报错。好在一路坎坷,解决了多方bug,最后终于搞定了
void insertList(@Param("planId") Integer planId, @Param("projectPlans") List<ProjectPlan> projectPlans);
1 刚开始爆绑定失败
后来发现原来是我的xml文件没有写mapper后缀
2 多个参数传值用@Param失败
别人说用这个,但是发现根本不行,
void insertList(@Param("planId") Integer planId, @Param("projectPlans") List<ProjectPlan> projectPlans);
<insert id="insertList" parameterType="java.util.Map">
insert into nursing_project_plan (plan_id,project_id,execute_time,execute_cycle,
execute_frequency,create_by, update_by, create_time, update_time,remark) values
<foreach collection="projectPlans" item="project" separator=",">
(#{planId},#{project.projectId},#{project.executeTime},#{project.executeCycle},
#{project.executeFrequency},#{project.createBy},#{project.updateBy},#{project.createTime},#{project.updateTime},#{project.remark}
)
</foreach>
</insert>
解决思路
报错信息
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'customerIdList' not found. Available parameters are [arg0, collection, list]
解释:
解释:
当我们传递一个 List 实例或者数组作为参数对象传给 MyBatis。当你这么做的时候,MyBatis 会自动将它包装在一个 Map 中,用名称在作为键。List 实例将会以list 作为键,而数组实例将会以array作为键。所以,当我们传递的是一个List集合时,mybatis会自动把我们的list集合包装成以list为Key值的map。
错误代码:
mapper层
void insertList(List<ProjectPlan>projectPlans );
//这个方法也是会错 void insertList(List<ProjectPlan>projectPlans ,Integer planId);
xml层
<insert id="insertList" >
insert into nursing_project_plan (plan_id,project_id,execute_time,execute_cycle,
execute_frequency,create_by, update_by, create_time, update_time,remark) values
<foreach collection="projectPlans" item="project" separator=",">
(#{planId},#{project.projectId},#{project.executeTime},#{project.executeCycle},
#{project.executeFrequency},#{project.createBy},#{project.updateBy},#{project.createTime},#{project.updateTime},#{project.remark}
)
</foreach>
</insert>
解决方法
- service层
(封装成map)
Map maps = new HashMap();
maps.put("projectPlans", projectPlans);
maps.put("planId", 2);
nursingProjectPlanMapper.insertList(maps);
- mapper层
void insertList( Map maps);
- xml层
<insert id="insertList" parameterType="java.util.Map">
insert into nursing_project_plan (plan_id,project_id,execute_time,execute_cycle,
execute_frequency,create_by, update_by, create_time, update_time,remark) values
<foreach collection="projectPlans" item="project" separator=",">
(#{planId},#{project.projectId},#{project.executeTime},#{project.executeCycle},
#{project.executeFrequency},#{project.createBy},#{project.updateBy},#{project.createTime},#{project.updateTime},#{project.remark}
)
</foreach>
</insert>