對於mybatis中.xml文件的無效判<insert id="insertDate">
</insert>
<if test="xxxEntityList != null and xxxEntityList.size() > 0">
INSERT into `date_insert`
(
`a_column`,
`b_column`,
`c_column`
)
VALUES
<foreach collection="xxxEntityList" item="xxxEntity" separator=",">
(
#{xxxEntity.aColumn},
#{xxxEntity.bColumn},
#{xxxEntity.bColumn})
</foreach>
</if>
</insert>
int insert(@Param("xxxEntityList") List<xxxEntity> xxxEntityList);
如果我們這里插入的xxxEntityList為null,那么就會報如下圖的這個錯誤;
原因分析:
不是我們這里的 <if test="xxxEntityList != null and xxxEntityList.size() > 0"> 沒有生效;
個人理解,mybatis中的sql在運行的時候的sql是作為字符串去輸出運行的,而我們的判空運行之后,我們的<insert></insert>
中就什么都沒有了,由此,報了SQL String cannot be empty這么個錯誤。
解決方案:
if( !CollectionUtils.isEmpty(xxxEntityList) )
xxxMapper.insertDate(xxxEntityList);
這個邏輯添加在mapper層就可以解決了。