[JAVA] ResultType behaves strangely in MyBatis Mapper XML

MyBatis did it again

Continued from last time, I heard that I was addicted to the strange movement of MyBatis again. That said, basically, I'm a little sloppy, and the one who made a mistake is a bad deal. I don't think MyBatis is bad (no, I think it's bad) Surprisingly, I can't find any mistakes myself and spend wasted time. I think it's rare, so I'll leave it so that if someone has a similar problem someday, I can solve it immediately.

The version of MyBatis is 3.2.

What happened

For some reason, the data held by Java's ʻIntegerdid not enter the DB'sNUMERIC` column. (I didn't get the error that was actually spit out, so I'll paste it later!) Certainly

** If you want to put String data in a NUMERIC column, explicitly CAST! ** **

I think it was an error content like that.

e? Why String? I have ʻInteger` ...? It is the content.

ItemService.java


//Various omissions
...
...
...
List<Integer> itemMasterIds = itemMasterMapper.getIdsByCategoryName("tapioca");
//↓ ↓ An error occurs here ↓ ↓
userItemsMapper.add(user.getId(), itemMasterIds);

ItemMasterMapper.java


List<Integer> getIdsByCategoryName(@Param("categoryName") String categoryName);

ItemMasterMapper.xml


<select id="getIdsByCategoryName" resultType="string">
    SELECT item_master_id FROM item_master WHERE category = #{categoryName}
</select>

UserItemsMapper.java


void add(@Param("userId") long userId, @Param("itemMasterIds") List<Integer> itemMasterIds); 

UserItemsMapper.xml


<insert id="add">
    INSERT INTO user_items (user_id, item_master_id)
    VALUES
    <foreach collection="itemMasterIds" item="item" separator=",">
    (#{userId}, #{item})
    </foreach>
</insert>

Cause

Perhaps most people will find out instantly just by looking at the above code. ʻIn ItemMasterMapper.xml, set resultTypeofgetIdsByItemCategory, You wrote string`.

<select id="getIdsByItemCategory" resultType="string">

Normally, the return type defined in the Mapper interface and If the resultType defined in Mapper XML does not match, I think I'll get an error at that point, but for some reason ʻitem_master.item_master_id You can get it withList of String`, isn't it?

I can't find an error like this right away, or it's already the limit.

Recommended Posts

ResultType behaves strangely in MyBatis Mapper XML
How to use Java enums (Enum) in MyBatis Mapper XML
How to store an object in PostgreSQL as JSON with MyBatis (Mapper XML)
Format XML in Java
How to pass an object to Mapper in MyBatis without arguments