mybatis-plus連線SQL Server2012分頁查詢異常

清清飞扬發表於2024-05-07

環境: 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的!

相關文章