Maybe I am missing something obvious, but I cannot get a simple mapped insert statement executing successfully.
Using the following interface
public interface CustomItemMapper
{
Integer insert(CustomItem item, @Param("extra") String someparam);
}
and the following XML mapping
<insert id="insert" useGeneratedKeys="false" parameterType="map" keyProperty="id">
insert into CustomItem (id, column2, column3, column4, column5, column6)
values (#{id}, #{field2}, #{field3}, #{field4}, #{field5}, #{extra})
</insert>
and this code
SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) servletContext.getAttribute("SqlSessionFactory");
SqlSession session = sqlSessionFactory.openSession();
try
{
CustomItemMapper mapper = session.getMapper(CustomItemMapper.class);
mapper.update(item);
session.commit();
}
finally
{
session.close();
}
I get the following debug output:
Checked out connection 368716606 from pool.
ooo Connection Opened
==> Executing: insert into CustomItem (id, column2, column3, column4, column5, column6) values (?, ?, ?, ?, ?, ?)
==> Parameters: null, null, null, null, null, actual_value_of_extra(String)
xxx Connection Closed
Returned connection 368716606 to pool.
followed by the SQL exception (cannot enter null into the id column).
So the only value that was passed in properly is the extra supplied string. I have verified that the field names are correct and object is not null at this point and the fields are populated properly.
I also tried using parameterType="CustomItem" instead of parameterType="map" but the result did not change.
I would appreciate if someone could let me know what I am missing here.
Thanks in advance.
PS: I get into the same problem with insert statements, as well.
You need to specify the name of the other parameter in your mapper method. I’m assuming CustomItem is a Java bean, with get methods for field1 etc. As already mentioned, change your parameter type, so that it is not Map.
From mybatis documentation,
So, change your Mapper,
and change your xml to,