com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException

MyBatis使用过程中实现的语法异常

分页:limit start,limit

异常内容
---------------------------------------------
### Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 ''0','20'' at line 9
### The error may involve getP2pTdProjectAdvanceRepaymentList-Inline
### The error occurred while setting parameters
### SQL: select      projectId,deadline,deadline_unit     from p2p_td_project_advance_repayment     where 1 = 1            and repayment_date = ?                  limit ?,?
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 ''0','20'' at line 9
; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 ''0','20'' at line 9

原因分析
  • 分页查询接口,mapper.xml 中该接口以 java.util.Map 作为入参;在 Service 层进行接口调用时,limit start,limit 中起始量与偏移量,都按照字符串传值
  • #{} 按照字符串解析,带有引号,导致不符合语法规则
解决方案
  • Service 中调用时将传入的参数的类型改为 Integer
  • Mapper.xml 中 limit 后用 ${} 而不使用 #{}
MyBatis 中 #{}与${} 的区别

in 操作

异常内容
select * from t_table where id in () 
原因分析
  • Mapper.xml 中没有对 foreach 的集合做相应的判空处理,认为此处传入的数据不会为空(本地测试时当然不会为空),生产环境没有数据,导致语法异常
解决方案
  • Mapper.xml 中对应接口加入 <if test="list != null"></if> 的判断;而如果可以确认此集合为空时不查询此接口,则在 Service 层中调用时就添加判断是否调用此 Mapper 接口
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值