mybatis-plus连接SQL Server2012分页查询异常
环境: windows10 + SQL Server2012 + mybatis-plus(3.4.1) + pagehelper-spring-boot-starter(1.4.4)
当查询的表中有关键字且查询时带有order by时,因为sql语法的问题,需要在关键字两边加[]如“[key]”,这会导致框架自动生成的求记录总条数语句报异常,如下:
1 2 | ### SQL: select count(0) from ( SELECT id,module,[key],value,remark,enable,time FROM LCKJ_Config ORDER BY id DESC ) tmp_count ### Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 除非另外还指定了 TOP、OFFSET 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。] |
没有关键字时则正常。
1. 无order by时,则出现如下错误:
1.1 pagehelper配置如下(连接sqlserver2012):
1 2 3 4 5 6 7 | # PageHelper分页插件 pagehelper: # autoRuntimeDialect: true helper-dialect: sqlserver2012 supportMethodsArguments: true params: count=countSql reasonable: true |
1 2 | ### SQL: SELECT id,module,[key],value,remark,enable,time FROM LCKJ_Config OFFSET ? ROWS FETCH NEXT ? ROWS ONLY ### Cause: com.microsoft.sqlserver.jdbc.SQLServerException: “@P0”附近有语法错误。] |
1.2. 将pagehelper配置修改如下(连接sqlserver2005):
1 2 3 4 5 6 7 | # PageHelper分页插件 pagehelper: # autoRuntimeDialect: true helper-dialect: sqlserver supportMethodsArguments: true params: count=countSql reasonable: true |
错误如下:
1 2 3 4 5 6 7 | 2024-05-07 07:08:21,728|WARN|org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver|207|Resolved [org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: com.github.pagehelper.PageException: 不支持该SQL转换为分页查询! ### The error may exist in com/soft/common/mapper/LckjConfigMapper.java (best guess) ### The error may involve com.soft.common.mapper.LckjConfigMapper.selectList_COUNT ### The error occurred while handling results ### SQL: select count(0) from ( SELECT id,module,[key],value,remark,enable,time FROM LCKJ_Config ) tmp_count ### Cause: com.github.pagehelper.PageException: 不支持该SQL转换为分页查询!] |
其实这个语句本身查询是正常的!!
将上述pagehelper的参数配置注释掉,也是同样的错误,可见,默认是认为连接sqlserver2005的!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)
2015-05-07 Sublime Text 3技巧:支持GB2312和GBK编码