[JAVA] Suffering from MyBatis foreach error

I was addicted to MyBatis foreach

I tried to bulk INSERT multiple data using MyBatis <foreach>. For some reason, such an error occurs. The version of MyBatis is 3.2.

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter '__frch_item_0' not found. Available parameters are [addItems, param1]

Implementation

ItemMasterMapper.java


void addItems(@Param("addItems") List<Item> addItems);

ItemMasterMapper.xml


<insert id="addItems">
INSERT INTO item_master(name, price, option) VALUES
<foreach item="item" collection="addItems" separator=",">
    (#{item.name}, #{item.price}, #{item.option})
</foreach>
</insert>

Item.java


public class Item {
    private String name;
    private long price;

//~~~ The setter and getter are omitted below ~~~
}

Why is this kind of thing? ?? Is it a typo with the parameter name specified by @ Param in the content of the error? Or is MyBatis you're using too old to support bulk inserts? I just googled for the error message.

Cause

If you take a closer look, ʻoption is not defined in the ʻItem class.

Item.java


public class Item {
    private String name;
    private long price;
    //↓ This was missing
    private String option;

//~~~ The setter and getter are omitted below ~~~
}

There is a discrepancy between the variable name described in Mapper XML and the member variable of the actual model class. What happened inside the <foreach> tag It seems that an error such as '__ frch_item_0' not found has occurred. Why did ʻitem` suddenly not exist here ... I wish I could tell you that there is usually no ʻoption ... Return my one and a half hours ...

Recommended Posts

Suffering from MyBatis foreach error
Moved from iBATIS to MyBatis3
JSP error display from Servlet
SpringBoot + Mybatis error when booting