随笔 - 493  文章 - 0  评论 - 97  阅读 - 239万

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的!

 

posted on   清清飞扬  阅读(573)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2015-05-07 Sublime Text 3技巧:支持GB2312和GBK编码
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示