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>

 

当我们在使用MyBatis进行开发时,有时候会遇到"java mybatis-3-mapper.dtd找不到文件"的报错。这个错误的原因是MyBatis在解析mapper.xml文件时,会通过DTD(Document Type Definition)来验证XML的结构和语法是否正确。而这个DTD文件通常会从MyBatis的jar包中加载。 出现找不到文件的情况可能是由以下几个原因导致的: 1. 项目配置问题:检查项目的配置文件,例如mybatis-config.xml是否正确配置了DTD路径。通常在mybatis-config.xml文件中会有类似以下的配置: <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 确保该配置正确并指向了DTD文件的正确位置。 2. Maven依赖问题:如果你是通过Maven来管理依赖的话,检查是否正确引入了MyBatis的相关依赖。通常需要引入以下两个依赖: <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.x.x</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.x.x</version> </dependency> 请确保版本号正确,并且Maven能够正确下载这些依赖。 3. 服务端网络问题:如果你的项目是部署在远程服务器上的,有可能是服务器无法访问MyBatis的DTD文件所在的URL导致的。可以尝试在本地手动访问DTD文件的URL,看是否能够正常下载。 以上是解决"java mybatis-3-mapper.dtd找不到文件"报错的一些常见方法,希望能够帮助到你。如果以上方法不能解决问题,可能需要进一步检查你的项目配置和环境设置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值