爱老鼠的喵

导航

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层就可以解决了。

posted on 2020-09-09 18:22  爱老鼠的喵  阅读(5604)  评论(0)    收藏  举报