1. 程式人生 > >Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException最全解決

Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException最全解決

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 'LIMIT 0,10' at line 21
### The error may exist in cn/edu/sdju/yyh/dao/LinkmanDao.xml
### The error may involve cn.edu.sdju.yyh.dao.LinkmanDao.selectLinkmanList-Inline
### The error occurred while setting parameters
### SQL: SELECT    lkm_id,             lkm_name,             lkm_cust_id,    lkm_gender,             lkm_phone,             lkm_mobile,             lkm_email,             lkm_qq,             lkm_position,             lkm_memo   FROM    linkman l      /*多條件查詢*/              WHERE /*所屬客戶*/                                        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 'LIMIT 0,10' at line 21
; 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 'LIMIT 0,10' at line 21
	

用ssm框架做畢設——————————————上面是我的報錯

這個報錯困擾了我整整一天半,難受啊馬飛~

下面是我的mapper.xml:

<!--SQL片段  -->
    <sql id="selectLinkmanListWhere">
        <where>
	       <if test="lkm_name != null" >
               lkm_name like "%"#{lkm_name}"%"
	       </if>
	       <if test="lkm_gender != null" >
	        and lkm_gender = #{lkm_gender}
	       </if>
            /*所屬客戶*/
	       <if test="lkm_cust_id != null" >
	        and lkm_cust_id = #{lkm_cust_id}
	       </if>
        </where>
    </sql>
	<!-- 查詢聯絡人列表  -->
	<select id="selectLinkmanList" parameterType="linkman"
                                           resultType="linkman">
		SELECT
			lkm_id,
            lkm_name,
            lkm_cust_id,
			lkm_gender,
            lkm_phone,
            lkm_mobile,
            lkm_email,
            lkm_qq,
            lkm_position,
            lkm_memo
		FROM
			linkman l
	    /*多條件查詢*/
		<include refid="selectLinkmanListWhere"/>
        <!-- 執行分頁查詢 -->
        <if test="start_index !=null and rows != null">

            LIMIT #{start_index},#{rows}
        </if>
	</select>

我知道你肯定不耐煩了,哈哈~~~

這個錯誤,大部分人肯定是欄位用了mysql的關鍵字導致的。

你可以先把sql語句去資料庫執行一下,重點看欄位和表名啊,千萬別用mysql關鍵字。

還有就是limit的引數也別用mysql關鍵字

而我的問題就有丶秀了,因為愛寫註釋的好習慣,但我把註釋寫到了<where></where>標籤裡面去了,還是/*   */這種形式(用的ctrl+shift+/快捷鍵)

這會導致什麼結果呢:無論你<where>中的<if>條件是否成立,mybatis都會給你拼接一個where

只要去掉註釋就OK了,或者改用<!--   -->

註釋。