今天在写一个查询接口的时候,测试中发现的一个问题,异常全文为:

Cause: java.sql.SQLSyntaxErrorException: 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 '' at line 1
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: 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 '' at line 1

根据异常的报错情况,很容易找出来问题,是因为有个地方的的" "判断出了问题,经查发现我的查询语句中,对入参做了判断:

   <if test="version != null and version != ''">
            and version=#{version}
        </if>

这种判断语法,但是这个version在对象里面书个Integer类型的,也就是不能用version!=" "来判空,只用前面的version!=null就好了。

注意:如果对象object里面的不是字符串类型的,不要用!=""在mybatis的xml文件的sql重做判断,特别是很多查询的时候。