Mbatis批量插入

Mbatis批量插入

@Date 2016.11.13

使用Mbatis批量插入功能代碼示例

  • 在做批量插入時要注意細節、如有寫錯會有奇怪的異常拋出
  • 有可能會出現異常 : Parameter ‘__frch_callRecord_0’ not found
@Insert('''<script>
        INSERT INTO xxx (
            prefix_number,
            serial_number,
            is_register,
            insert_date,
            update_date,
            all_number,
            merchant_id
        ) VALUES
        <foreach collection="list" item="item" index="index" separator="," >
            (
                #{item.prefixNumber},
                #{item.serialNumber},
                #{item.isRegister},
                now(),
                now(),
                #{item.allNumber},
                #{item.merchantId}
            )
        </foreach>
    </script>''')

批量插入時遇到如下異常(Caused by: org.apache.ibatis.binding.BindingException: Parameter ‘__frch_callRecord_0’ not found.)

異常現象

  • 使用mbatis批量插入時、出現如下異常:
Caused by: org.apache.ibatis.binding.BindingException: Parameter '__frch_callRecord_0' not found.

解決思路

  • Mbatis對此類問題的異常描述不是很清晰,如出現上訴異常,主要檢查以下幾個方面
    • 批量插入的List對象中字段是否和數據庫表中字段名一致並且都存在
    • 批量插入的List對象中是否有NULL的對象(此原因很重要)
    • 在Mbatis的XML或者註解的Sql語句中,是否傳入的表字段和list字段一致
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章