Error updating database. Cause: java.sql.SQLException: SQL String cannot be empty


對於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層就可以解決了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM