mybatis insert foreach

本文探讨了在使用MyBatis进行批量插入时遇到的'Parameter 'statusInfoId' not found'错误,通过实例展示了如何正确设置参数类型和foreach标签,确保mapper接口和SQL语句的匹配,避免此类常见问题。
摘要由CSDN通过智能技术生成

项目场景:

报错 ,找不到参数
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘statusInfoId’ not found. Available parameters are [collection, list]

  @Mapper
public interface PatrolRecordMapper extends BaseMapper<PatrolRecord> {

    int insertList(@Param(value = "list") List<PatrolRecord> list);
}

mapper 换了很多种写法

    <insert id="insertList" parameterType="com.iricto.soft.patrol.entity.PatrolRecord">
        insert into patrol_record(status_info_id,route_id,place_name,patrol_time,
        patrol_user,patrol_class,`status`)
        VALUES

        <foreach collection="list" item="patrolRecord" separator=",">
            (patrolRecord.#{statusInfoId},
            patrolRecord.#{routeId},
            patrolRecord.#{placeName},
            patrolRecord.#{patrolTime},
            patrolRecord.#{patrolUser},
            patrolRecord.#{patrolClass},
            patrolRecord.#{status})

        </foreach>


    </insert>

mapper

    <insert id="insertList" parameterType="com.iricto.soft.patrol.entity.PatrolRecord">
        insert into patrol_record(status_info_id,route_id,place_name,patrol_time,
        patrol_user,patrol_class,`status`)
        VALUES

        <foreach collection="list" item="list"
                 open="(" separator="," close=")">
            list.#{statusInfoId},
            list.#{routeId},
            list.#{placeName},
            list.#{patrolTime},
            list.#{patrolUser},
            list.#{patrolClass},
            list.#{status}

        </foreach>
    </insert>

最后应该这么写才对 : mapper

  
    <insert id="insertList" parameterType="com.iricto.soft.patrol.entity.PatrolRecord">
        insert into patrol_record(status_info_id,route_id,place_name,patrol_time,
        patrol_user,patrol_class,`status`)
        VALUES

        <foreach collection="list" item="patrolRecord" separator=",">
            (
            #{patrolRecord.statusInfoId},
            #{patrolRecord.routeId},
            #{patrolRecord.placeName},
            #{patrolRecord.patrolTime},
            #{patrolRecord.patrolUser},
            #{patrolRecord.patrolClass},
            #{patrolRecord.status})

        </foreach>


    </insert>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值