wzs1998
wzuosen
2019-09-16 18:29

mybatis <if>标签,insert语句报语法错误

  • spring
  • java
  • mysql

在插入数据库时判断字段非空(message_article_id)

<if test="messageArticleId != null or messageArticleId != ''">
insert into message(message_id,message_title,message_content,message_date,message_category_id, message_user_id ,message_article_id)
values(#{messageId},#{messageTitle} ,#{messageContent},#{messageDate},#{messageCategoryId},#{messageUserId}  ,#{messageArticleId});
</if>
<if test="messageArticleId == null or messageArticleId == ''">
insert into message(message_id,message_title,message_content,message_date,message_category_id, message_user_id)
values(#{messageId},#{messageTitle} ,#{messageContent},#{messageDate},#{messageCategoryId},#{messageUserId});
</if>

但是这样在**messageArticleId**非空的情况下可以插入,非空时总是会报**语法错误**
但是通过下面这种方式就没有问题

        insert into message(message_id,message_title,message_content,message_date,message_category_id,
        message_user_id
        <if test="messageArticleId != null or messageArticleId != ''">
        ,message_article_id
        </if>
        ) 
        values(#{messageId},#{messageTitle}
        ,#{messageContent},#{messageDate},#{messageCategoryId},#{messageUserId}
         <if test="messageArticleId != null or messageArticleId != ''">
         ,#{messageArticleId}
         </if>
         );

求教

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答