mybatis 项目错误总结(一)

本文记录了一次解决MyBatis中出现的SQL语法错误的过程,并分享了#与$的区别,提供了错误处理方法及推荐的解决方案。同时介绍了MyBatis参数传递的不同方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

三月 01, 2017 11:14:40 上午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [spring] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: 
### 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 '' and 1=1 '  limit 0,5' at line 3
### The error may exist in com/lsj/mapper/FinanceProducts.xml
### The error may involve 
### The error occurred while setting parameters
### SQL: select * from financeProducts f,jiekuanType j,huankuanfangshi h,user u where        f.jiekuanTypeId=j.jiekuanTypeId and f.huankuanfangshiId=h.huankuanfangshiId        and f.productReleaserId=u.uid  ?  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 '' and 1=1 '  limit 0,5' at line 3
; 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 '' and 1=1 '  limit 0,5' at line 3] with root 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 '' and 1=1 '  limit 0,5' at line 3
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

上面的错误解决需要熟悉 #与$的差别(参下)
MyBatis/Ibatis中#和$的区别

http://blog.youkuaiyun.com/downkang/article/details/12499197/

结合以上(关键错误点 near ” and 1=1 ’ limit 0,5’ at line 3)
我的解决办法将#替为$(不推荐)
推荐写法(在Controller层中 将要搜索的条件put进Map 集合 然后传参 在mapper.xml 中遍历 这样更安全 )
顺便熟悉一下mybatis 传参

一、单个参数:

public List getXXBeanList(String xxCode);

  select t.* from tableName t where t.id= #{id}

其中方法名和ID一致,#{}中的参数名与方法中的参数名一直

select 后的字段列表要和bean中的属性名一致, 如果不一致的可以用 as 来补充。

二、多参数:

public List getXXXBeanList(String xxId, String xxCode);

  select t.* from tableName where id = #{0} and name = #{1}

由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始

三、Map封装多参数:

public List getXXXBeanList(HashMap map);

  select 字段… from XXX where id=#{xxId} code = #{xxCode}

其中hashmap是mybatis自己配置好的直接使用就行。map中key的名字是那个就在#{}使用那个

四、List封装in:

public List getXXXBeanList(List list);


  select 字段… from XXX where id in
  
    #{item}
  

foreach 最后的效果是select 字段… from XXX where id in (‘1’,’2’,’3’,’4’)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值