简体   繁体   English

无法使用MyBatis批量插入Oracle DB

[英]Unable to make batch insert into Oracle DB using MyBatis

I try to make batch insert into table at Oracle database using MyBatis (integrated with Spring) . 我尝试使用MyBatis(与Spring集成)在Oracle数据库的表中进行批量插入。

It is a definition of method in mapper interface: 它是mapper界面中方法的定义:

public void savePayments(@Param("payments") List<MassPaymentItem> payments);

It is a MyBatis XML mapper code: 这是MyBatis XML映射器代码:

<insert id="savePayments" parameterType="MassPaymentFileItem" useGeneratedKeys="false">
    INSERT INTO mass_payments
        (payment_id, file_id, msisdn, amount, status)
    <foreach collection="payments" item="payment" index="index" separator=" UNION ALL ">
        SELECT SEQ_MASS_PAYMENT.nextval, #{payment.fileId}, #{payment.msisdn}, #{payment.amount}, 0 FROM DUAL
    </foreach>
</insert>

When I execute this I receive MyBatisSystemException with message "nested exception is org.apache.ibatis.builder.BuilderException: Improper inline parameter map format. Should be: #{propName,attr1=val1,attr2=val2}" 执行此操作时,我收到MyBatisSystemException,消息为“嵌套异常为org.apache.ibatis.builder.BuilderException:内联参数映射格式不正确。应为:#{propName,attr1 = val1,attr2 = val2}”

What is wrong? 怎么了? How to fix it? 如何解决?

I found solution here 我在这里找到解决方案

<insert id="savePayments">
   INSERT ALL
   <foreach collection="payments" item="payment">
   INTO
      mass_payments_t (payment_id, file_id, msisdn, amount)
   VALUES
      (seq_mass_payment.nextval, #{payment.fileId, javaType=Integer, jdbcType=NUMERIC}, #{payment.msisdn, javaType=String, jdbcType=VARCHAR}, #{payment.amount, javaType=BigDecimal, jdbcType=NUMERIC})
   </foreach>
   SELECT * FROM dual
</insert>

I found that the first answer doesn't work for me, then i find another solution as below: 我发现第一个答案对我不起作用,然后我找到了另一个解决方案,如下所示:

 <insert id="savePayments" parameterType="java.util.List" useGeneratedKeys="true">
       <selectKey resultType="java.lang.Integer" keyProperty="payment_id"
            order="BEFORE">
            SELECT SEQ_MASS_PAYMENT.nextval as payment_id FROM DUAL
        </selectKey>
        INSERT INTO mass_payments
            (payment_id, file_id, msisdn, amount, status)
            select SEQ_MASS_PAYMENT.nextval, A.* from (
        <foreach collection="payments" item="payment" index="index" separator="UNION ALL">
            SELECT 
            #{payment.fileId} as file_id,
            #{payment.msisdn} as msisdn,
            #{payment.amount} as amount,
            0 as status
            FROM DUAL
        </foreach> ) A
    </insert>

Take a close look at your select statement. 请仔细查看您的select语句。 Where one would expect column name or function, you pass parameter. 如果需要列名或函数,则可以传递参数。 You should change it to something like (notice the location of from : 您应该将其更改为类似(注意from的位置:

<foreach collection="payments" item="payment" index="index" separator=" UNION ALL ">
        (SELECT SEQ_MASS_PAYMENT.nextval from DUAL), #{payment.fileId}, #{payment.msisdn}, #{payment.amount}, 0 
    </foreach>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM