mybatis 專案錯誤總結(一)
三月 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中#和$的區別
結合以上(關鍵錯誤點 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’)