在使用MyBatis的时候,可能会看起来没有问题,但是代码运行的时候出现意想不到的错误。

看如下代码:

<update id="updateByPrimaryKeySelective" parameterType="com.weimob.mengdian.promotion.dao.model.PromotionInfo">
update promotion_info set
<if test="title != null and title!=''">
title = #{title},
</if>
<if test="startTime != null and startTime!=''">
start_time = #{startTime},
</if>
<if test="endTime != null and endTime!=''">
end_time = #{endTime},
</if>
<if test="promotionType != null and promotionType!=''">
promotion_type = #{promotionType}
</if>
</update>

如上代码看起来没有什么问题,但是大家想想,如果当endTime不为空,而promotionType为空时,会有什么结果?

就会多出来一个逗号,所以代码运行的时候会出现语法错。

解决办法:

最后一个更新的字段一定要保证不是空。

Mybatis避免出现语法错的更多相关文章

  1. spring 整合Mybatis 《报错集合,总结更新》

    错误:java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldExcepti ...

  2. mybatis foreach报错It was either not specified and/or could not be found for the javaType Type handler

    或许是惯性思维,在mybatis使用foreach循环调用的时候,很多时候都是传一个对象,传一个List的情况很少,所以写代码有时候会不注意就用惯性思维方法做了. 今天向sql传参,传了一个List作 ...

  3. Mybatis数据库连接报错:对实体 "characterEncoding" 的引用必须以 ';' 分隔符结尾

    Mybatis数据库连接报错:对实体 "characterEncoding" 的引用必须以 ';' 分隔符结尾 ============================== 蕃薯耀 ...

  4. 【mybatis】mybatis访问报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 或者 feign被调用方使用的mybatis总报空指针异常java.lang.NullPointerException,而变量都没有问题的情况

    mybatis访问报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 需要检查的步骤: ...

  5. 【Mybatis】mybatis查询报错org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'areaName' in 'class java.lang.String'

    mybatis查询报错: Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for pro ...

  6. myBatis查询报错 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near

    myBatis查询报错 You have an error in your SQL syntax; check the manual that corresponds to your MySQL se ...

  7. 尝试EJB整合Mybatis部署时报错:获得带有类加载器MybatisUtil的ModuleClassLoader的反射信息出错,请问大神如何解决

    mybatis的配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configurati ...

  8. Spring+Mybatis整合报错Mapped Statements collection does not contain value原因之一

    报错如下: ### Error updating database. Cause: java.lang.IllegalArgumentException: Mapped Statements coll ...

  9. mybatis配置文件配错

    UG] 2017-10-04 20:04:30,582(137226) --> [http-bio-8082-exec-9] org.springframework.web.servlet.ha ...

随机推荐

  1. PageRank算法原理及实现

    PageRank算法原理介绍 PageRank算法是google的网页排序算法,在<The Top Ten Algorithms in Data Mining>一书中第6章有介绍.大致原理 ...

  2. UVA 11291 Smeech

    [来源]https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. Win 7升级记

    微软要抛弃它的XP了,我也应该提前把家里的PC升级成Win7,省得将来麻烦事多. 其实升级它也很简单,这全要归功于网络上的能人.我首先在网络上下载好一个操作系统DEEP_Ghost_Win7_Sp1_ ...

  4. 2017.10.7北京清北综合强化班DAY7

    1.计数 (count.cpp/c/pas) 时间限制:1s 内存限制:256MB [问题描述] 给出m个数a[1],a[2],…,a[m] 求1~n中有多少数不是a[1],a[2],…,a[m]的倍 ...

  5. bzoj 4300 绝世好题——DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4300 考虑 dp[ i ] 能从哪些 j 转移过来,就是那些 a[ j ] & a[ ...

  6. 得到properties配置文件内容

    代码: 1.配置文件内容 2.文件所在项目中位置: 3.java代码: 01.得到键值对: @Test public void getProp() { Properties prop = new Pr ...

  7. Django基础(二)—— models

    六:Models示例 Django本身提供了非常强大易使用的ORM组件,并且支持多种数据库. 配置连接数据文件 在自己创建的project 目录下编辑settings.py DATABASES = { ...

  8. java事件练习!!

    总结:不晓得怎么跟书上的运行结果显示的...希望标签竖直排列 package com.bc; import java.awt.Color; import java.awt.FlowLayout; im ...

  9. ES之二:Elasticsearch原理

    Elasticsearch是最近两年异军突起的一个兼有搜索引擎和NoSQL数据库功能的开源系统,基于Java/Lucene构建.最近研究了一下,感觉 Elasticsearch 的架构以及其开源的生态 ...

  10. 用nfs挂载内核时出错 ERROR: Cannot umount的解决办法

    SMDK2440 # nfs 30000000 192.168.1.106:/work/nfs_root/uImage                         ERROR: resetting ...