开发者社区> 问答> 正文

使用mybatis 动态sql执行order by报语法错误

在使用贵团队项目时,遇到语法解析错误, 直接在mybatis xml配置中写静态sql可以解析正确,使用mybatis动态sql 就会在order by 的地方报解析错误 报错信息: com.alibaba.druid.sql.parser.ParserException: syntax error, error in :'RDER BY ', expect QUES, actual QUES pos 363, line 15, column 19, token QUES 缺失了'O'

数据库 : mysql druid版本: 1.1.2和1.1.10两个版本均报错

详细报错报错信息如下:

2018-08-15 19:32:28.396 ERROR 5320 --- [ main] c.alibaba.druid.filter.stat.StatFilter : merge sql error, dbType mysql, druid-1.1.10, sql : select SysNo, ProjectSysNo, AppSysNo, DeviceId, DataId, Title, Content, OperationTime, InDate, WarningDataFieldJson, WaringState from projectintellidevicewarning WHERE ProjectSysNo=? ORDER BY ? ? com.alibaba.druid.sql.parser.ParserException: syntax error, error in :'RDER BY ', expect QUES, actual QUES pos 363, line 15, column 19, token QUES at com.alibaba.druid.sql.parser.SQLParser.printError(SQLParser.java:284) at com.alibaba.druid.sql.parser.SQLStatementParser.parseStatementList(SQLStatementParser.java:426) at com.alibaba.druid.sql.parser.SQLStatementParser.parseStatementList(SQLStatementParser.java:72) at com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils.parameterize(ParameterizedOutputVisitorUtils.java:94) at com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils.parameterize(ParameterizedOutputVisitorUtils.java:77) at com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils.parameterize(ParameterizedOutputVisitorUtils.java:50) at com.alibaba.druid.filter.stat.StatFilter.mergeSql(StatFilter.java:147) at com.alibaba.druid.filter.stat.StatFilter.createSqlStat(StatFilter.java:648) at com.alibaba.druid.filter.stat.StatFilter.statementPrepareAfter(StatFilter.java:311) at com.alibaba.druid.filter.FilterEventAdapter.connection_prepareStatement(FilterEventAdapter.java:124) at com.alibaba.druid.filter.FilterChainImpl.connection_prepareStatement(FilterChainImpl.java:568) at com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl.prepareStatement(ConnectionProxyImpl.java:342) at com.alibaba.druid.pool.DruidPooledConnection.prepareStatement(DruidPooledConnection.java:349) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.ibatis.logging.jdbc.ConnectionLogger.invoke(ConnectionLogger.java:55) at com.sun.proxy.$Proxy116.prepareStatement(Unknown Source) at org.apache.ibatis.executor.statement.PreparedStatementHandler.instantiateStatement(PreparedStatementHandler.java:87) at org.apache.ibatis.executor.statement.BaseStatementHandler.prepare(BaseStatementHandler.java:88) at org.apache.ibatis.executor.statement.RoutingStatementHandler.prepare(RoutingStatementHandler.java:59) at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:85) at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:62) at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324) at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156) at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109) at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433) at com.sun.proxy.$Proxy101.selectList(Unknown Source) at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230) at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137) at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75)

原提问者GitHub用户wklong

展开
收起
山海行 2023-07-05 19:49:09 347 0
2 条回答
写回答
取消 提交回答
  • 北京阿里云ACE会长

    在MyBatis中,动态SQL可以帮助我们根据不同的条件生成不同的SQL语句。但是,在使用动态SQL时,由于SQL语句的不同部分是动态生成的,可能会导致一些语法错误。对于您的情况,如果您在动态SQL中使用了order by语句,但是在某些情况下没有生成order by语句,就会导致语法错误。

    2023-07-30 15:09:24
    赞同 展开评论 打赏
  • 排序不能用可变参数

    所以 #{xx} -> ${xx}

    原回答者GitHub用户wklong

    2023-07-06 11:09:11
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
SQL Server在电子商务中的应用与实践 立即下载
SQL Server 2017 立即下载
Java Spring Boot开发实战系列课程【第6讲】:Spring Boot 2.0实战MyBatis与优化(Java面试题) 立即下载