今天在写一个查询接口的时候,测试中发现的一个问题,异常全文为:
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就好了。
本文讲述了在开发过程中遇到的SQL语法错误,重点在于Mybatis中如何处理非字符串类型参数的空值判断。作者提醒避免在XML文件中使用`!=`,尤其是在查询语句中,针对Integer类型变量应仅做`!=null`检查。
5139

被折叠的 条评论
为什么被折叠?



