我用MyBatis编写了以下SQL代码:
<select id="getItems" resultMap="someMap">
SELECT *
FROM my_table
WHERE item_id = #{itemID}
AND source_id IN
<foreach item="sourceID" index="index" collection="sourceIDList"
open="(" separator="," close=")">
#{sourceID}
</foreach>
</select>
Java中对应的mapper类是:
public interface ItemMapper
{
List<ItemDO> getItems(
@Param("itemID") String itemID,
@Param("sourceIDList") List<String> sourceIDList);
}
现在,在调用程序类中,如果我通过Arrays.asList()创建列表,则MyBatis无法正确地映射参数(尽管不会引发异常):>
List<String> sourceIDList = Arrays.asList("US", "UK");
但是,如果我通过构造函数创建列表,则一切正常:
List<String> sourceIDList = new ArrayList<>();
sourceIDList.add("US");
sourceIDList.add("UK");
在日志中,我可以看到Array.asList()的情况:
Parameters: Item1(String), US, UK(String)
第二种情况是这样的:
Parameters: Item1(String), US(String), UK(String)
我知道Arrays.asList()
创建的相应对象不是java.util.ArrayList
类的对象。但是,我很好奇MyBatis为什么要使用这种特定格式?