环境: windows10 + SQL Server2012 + mybatis-plus(3.4.1) + pagehelper-spring-boot-starter(1.4.4)
当查询的表中有关键字且查询时带有order by时,因为sql语法的问题,需要在关键字两边加[]如“[key]”,这会导致框架自动生成的求记录总条数语句报异常,如下:
### 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):
# PageHelper分页插件
pagehelper:
# autoRuntimeDialect: true
helper-dialect: sqlserver2012
supportMethodsArguments: true
params: count=countSql
reasonable: true
### 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):
# PageHelper分页插件
pagehelper:
# autoRuntimeDialect: true
helper-dialect: sqlserver
supportMethodsArguments: true
params: count=countSql
reasonable: true
错误如下:
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的!