使用mybatis批量注入注意点:foreach的collection命名

问题描述

我在使用mybatis批量注入时,我的对应mapper中的方法如下所示

 Integer batchInsert(List<AnalysisResult> resultList);

其中的参数名为resultList,对应的xml中的collection命名也为:resultList

<insert id="batchInsert" parameterType="java.util.List">
   insert into test(area_name, city_name, city_code) VALUES
       <foreach collection="resultList" item="result" index="index" separator=",">
            (#{result.area_name}, #{result.city_name}, #{result.city_code})
        </foreach>
</insert>

这时执行批量插入时就会出现如下错误

org.apache.ibatis.binding.BindingException: Parameter 'resultList' not found. Available parameters are [collection, list]

 解决办法之一

将mapper中的参数改成list,对应xml中的collection也改成list,即可解决上述问题

其具体原因还没有搞懂,若有人知道,希望留言指导下。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
MyBatis中,可以使用foreach标签来实现批量插入操作。foreach标签可以用于循环遍历集合或数组,并执行相应的SQL语句。以下是几个示例: 1. 使用foreach批量删除数据: ``` delete from xxx_table where id in <foreach collection="list" item="item" index="index" open="(" separator="," close=")"> #{item} </foreach> => (1,2,3,4) ``` 2. 使用foreach批量插入数据: ``` <insert id="batchInsert"> insert into ref_teacher_course (teacher_id,course_id,create_time,update_time) values <foreach collection="teacherCourseList.CourseList" item="item" index="index" separator=","> (#{teacherCourseList.username}, #{item}, #{teacherCourseList.create_time}, #{teacherCourseList.update_time}) </foreach> </insert> ``` 3. 在Mapper接口中定义批量保存方法: ``` @Mapper public interface UserMapper { void batchSave(List<User> userList); } ``` 4. 使用foreach批量保存数据: ``` <insert id="batchSave"> <foreach collection="list" item="user" separator=";"> insert into user(name, password) values (#{user.name}, #{user.password}) </foreach> </insert> ``` 在测试类中调用批量保存方法: ``` @RunWith(SpringRunner.class) @SpringBootTest public class SbMybatis02ApplicationTests { @Test public void testBatchSave(){ User user1 = new User(); user1.setName("关羽"); user1.setPassword("guanyu"); User user2 = new User(); user2.setName("张飞"); user2.setPassword("zhangfei"); List<User> userList = new ArrayList<>(); userList.add(user1); userList.add(user2); userMapper.batchSave(userList); } } ``` 以上就是使用MyBatis中的foreach实现批量插入的示例。使用foreach可以简化批量操作的编写,提高效率。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Mybatis批量添加(foreach标签)](https://blog.csdn.net/m0_51212267/article/details/121641252)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [MyBatis 使用 foreach 批量插入](https://blog.csdn.net/lizhengyu891231/article/details/125546407)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Flying_6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值