Steven

You're still young --that's your fault

 

【解决】【 MyBatis 】使用MyBatis批量增加,出现 Parameter '__frch_item_0' not found. Available parameters are [list]

异常信息:

复制代码
1 java.lang.RuntimeException: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter '__frch_item_0' not found. Available parameters are [list]
2 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter '__frch_item_0' not found. Available parameters are [list]
3     at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
4     at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:365)
复制代码

 

最终查到导致异常的原因是 foreach中的属性字段名字写错了!表字段太多,一个个找的还是很费劲的!建议这种做好单元测试!

另外,写xml的时差错,还可以从下面几点检查。

1、parameterType="Java.util.List",这个parameterType有没有写错;

2、<foreach collection="list" item="item" index="index" open="" close="" separator=";">这行中,collection是不是List

关于批量更新做一个总结:

批量更新有两种,一种是通过id更新很多字段,第二种是更改一列,值是固定的这种。下面分别贴出列子

例子:

更新一列:

复制代码
1 <update id="batchUpdateResult" parameterType="java.util.List">    
2          update
3          <include refid="input_invoice_original_record" />
4        set isGet = 0 where invoiceNum in
5        <foreach collection="list" item="item" open="(" separator="," close=")">
6        #{item}
7        </foreach>
8 </update>
复制代码

这种直接是item就行,只是一个查询的条件范围。

更新很多字段:

复制代码
 1 <update id="batchUpdate" parameterType="list">
 2             <foreach collection="list" item="item" index="index" open="" close="" separator=";">
 3                   update
 4                   <include refid="input_invoice_original_record" />
 5                   <trim prefix="SET" suffixOverrides=",">
 6                         <if test="item.title != null">
 7                               title = #{item.title},
 8                         </if>
 9                         <if test="item.invoiceNum != null">
10                               invoiceNum = #{item.invoiceNum},
11                         </if>
12                         <if test="item.invoiceCode != null">
13                               invoiceCode = #{item.invoiceCode},
14                         </if>
15                         <if test="item.invoiceDate != null">
16                               invoiceDate = #{item.invoiceDate},
17                         </if>
18     
19                         </trim>
20                   WHERE id = #{item.id}
21             </foreach>
22       </update>
23     
复制代码

注意,这种传的是一个个的对象在list集合中,要加item.属性名称,空判断的时候别忘记这种格式,这里我犯过错,没少费时间。

贴出来当时忘记加item的异常 信息:

复制代码
1 java.lang.RuntimeException: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'title' not found. Available parameters are [list]
2 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'title' not found. Available parameters are [list]
3     at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
4     at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:365)
复制代码

找这个问题用了不少时间!

 

下面说一下批量插入操作。

复制代码
 1       <insert id="batchInsert" parameterType="java.util.List"
 2             useGeneratedKeys="true" keyProperty="id">
 3             <selectKey resultType="long" keyProperty="id" order="AFTER">
 4                   SELECT
 5                   LAST_INSERT_ID()
 6             </selectKey>
 7             INSERT INTO
 8             <include refid="input_invoice_original_record" />
 9             (
10             title ,
11             invoiceNum,
12             invoiceCode,
13             invoiceDate,
14             sellDate ,
15             invoiceCategory
16             )
17             VALUES
18             <foreach collection="list" item="item" index="index"
19                   separator=",">
20                   (
21                   #{item.title} ,
22                   #{item.invoiceNum},
23                   #{item.invoiceCode},
24                   #{item.invoiceDate},
25                   #{item.sellDate} ,
26                   #{item.invoiceCategory}
27                   )
28             </foreach>
29       </insert>
复制代码

易错点和批量更新一样,写的时候要仔细。

 

这里做一个总结,如果不正确之处,欢迎指正!

 

1.查看parameterType的类型是不是Java.util.List类型,如果是的话,看foreach 的collection属性是不是list,

    因为 传递一个 List 实例或者数组作为参数对象传给 MyBatis,MyBatis 会自动将它包装在一个 Map 中,用名称在作为键。List 实例将会以“list” 作为键,而数组实例将会以“array”作为键 

2.看一下foreach里面的值有没有传递进来 

3.看foreach里面的名称字段是否写错 

4.还有就是我用Mybatis的时候,用MySQL的值插入自动增长值,里面的key我在数据库中没有设置自动增长,然后我又用了selectkey,所以也会出现这种情况


应该还有别的错误能够导致这个错误。但是我就只遇到这几种。所以做个总结



【可用例子】

    <!-- createEmergencyBatch:批量插入交接班 - 突发事件 关联对象列表 -->
    <insert id="createEmergencyBatch" parameterType="java.util.List">
        INSERT INTO DUTY_CHANGE_DUTY_EMERGENCY
        (ID,EMERGENCY_ID,CHANGE_WORK_DUTY_ID,STATUS,CREATE_TIME)
        (
        <foreach collection="list" item="item" index="index" separator="union all">
            SELECT
            #{item.id,jdbcType=VARCHAR},
            #{item.emergencyId,jdbcType=VARCHAR},
            #{item.changeWorkDutyId,jdbcType=VARCHAR},
            #{item.status,jdbcType=VARCHAR},
            #{item.createTime,jdbcType=TIMESTAMP}
            FROM dual
        </foreach>
        )
    </insert>


梅花香自苦寒来

posted on 2017-05-09 08:09 wen.ding 阅读(3831) 评论(0)  编辑  收藏 所属分类: Exception


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿

随笔分类

随笔档案

文章分类

文章档案

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜