mabatis Mapper.xml報錯

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 (A.developer_id="李白" or T.contact_name like "%李白%")' at line 17
### The error may exist in class path resource [mybatis/AppInfoMapper.xml]
### The error may involve com.xiaomi.mina.center.mapper.AppInfoMapper.listFirstOnlineAppInfo-Inline
### The error occurred while setting parameters
### SQL: select A.*,if(A.app_version_code=B.min_app_version_code,0,2) as version_state, T.contact_name from app_info A         join (select min(app_version_code) as min_app_version_code,app_id from app_info         where status=4 group by app_id) B on A.app_id=B.app_id         inner join developer_info T on T.developer_id = A.developer_id         and A.app_version_code=B.min_app_version_code and A.status=4         where         date_format(from_unixtime(A.update_time/1000),'%Y%m%d') >= ?         and date_format(from_unixtime(A.update_time/1000),'%Y%m%d') <= ?                                                               limit 0,20                                 and (A.developer_id="李白" or T.contact_name like "%李白%")
### 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 (A.developer_id="李白" or T.contact_name like "%李白%")' at line 17
 

感覺應該是sql語句的問題,但是一直無法定位到問題,mapper.xml中對應內容爲:

<select id="listFirstOnlineAppInfo" parameterType="com.xiaomi.mina.center.base.AppInfoQUERY"
            resultMap="BaseResultMap">
        select A.*,if(A.app_version_code=B.min_app_version_code,0,2) as version_state from app_info A
        join (select min(app_version_code) as min_app_version_code,app_id from app_info
        where status=4 group by app_id) B on A.app_id=B.app_id
        inner join developer_info T on T.developer_id = A.developer_id
        and A.app_version_code=B.min_app_version_code and A.status=4
        where
        date_format(from_unixtime(A.update_time/1000),'%Y%m%d') &gt;= #{updateBeginDate}
        and date_format(from_unixtime(A.update_time/1000),'%Y%m%d') &lt;= #{updateEndDate}
        <if test="appKeywords != null">
            and (A.app_id="${appKeywords}" or A.package_name like "%${appKeywords}%" or A.app_name like
            "%${appKeywords}%")
        </if>
        <if test="type != null">
            and A.type=#{type}
        </if>
        <if test="cardType != null">
            and A.app_type=#{cardType}
        </if>
        <if test="order != null and prop != null">
            order by A.${prop} ${order}
        </if>
        <if test="startNum != null">
            limit ${startNum},${pageSize}
        </if>
        <if test="developerKeywords != null">
            and (A.developer_id="${developerKeywords}" or T.contact_name like "%${developerKeywords}%")
        </if>
    </select>

生成的sql語句有問題,

select A.*,if(A.app_version_code=B.min_app_version_code,0,2) as version_state, T.contact_name from app_info A         join (select min(app_version_code) as min_app_version_code,app_id from app_info         where status=4 group by app_id) B on A.app_id=B.app_id         inner join developer_info T on T.developer_id = A.developer_id         and A.app_version_code=B.min_app_version_code and A.status=4         where         date_format(from_unixtime(A.update_time/1000),'%Y%m%d') >= ?         and date_format(from_unixtime(A.update_time/1000),'%Y%m%d') <= ?                                                               limit 0,20                                 and (A.developer_id="李白" or T.contact_name like "%李白%")

加的那句起碼應該在limit後面啊,我也太馬虎了。。。遇到這種問題首先檢查sql語句是否有問題!!

修改後:

<select id="listFirstOnlineAppInfo" parameterType="com.xiaomi.mina.center.base.AppInfoQUERY"
            resultMap="BaseResultMap">
        select A.*,if(A.app_version_code=B.min_app_version_code,0,2) as version_state from app_info A
        join (select min(app_version_code) as min_app_version_code,app_id from app_info
        where status=4 group by app_id) B on A.app_id=B.app_id
        inner join developer_info T on T.developer_id = A.developer_id
        and A.app_version_code=B.min_app_version_code and A.status=4
        where
        date_format(from_unixtime(A.update_time/1000),'%Y%m%d') &gt;= #{updateBeginDate}
        and date_format(from_unixtime(A.update_time/1000),'%Y%m%d') &lt;= #{updateEndDate}
        <if test="appKeywords != null">
            and (A.app_id="${appKeywords}" or A.package_name like "%${appKeywords}%" or A.app_name like
            "%${appKeywords}%")
        </if>
        <if test="type != null">
            and A.type=#{type}
        </if>
        <if test="cardType != null">
            and A.app_type=#{cardType}
        </if>
        <if test="developerKeywords != null">
            and (T.developer_id="${developerKeywords}" or T.contact_name like "%${developerKeywords}%")
        </if>
        <if test="order != null and prop != null">
            order by A.${prop} ${order}
        </if>
        <if test="startNum != null">
            limit ${startNum},${pageSize}
        </if>

    </select>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章