1. 程式人生 > >分頁外掛不起效果java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.github.pagehelper.Pa

分頁外掛不起效果java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.github.pagehelper.Pa

錯誤程式碼

 public List findUserPageFromMybatis(HttpServletRequest request, Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = this.userService.selectAll();
        PageInfo pageInfo = new PageInfo(list);
        Page page = (Page) list;
        return "PageInfo: " + JSON.toJSONString(pageInfo) + ", Page: " + JSON.toJSONString(page);
    }

list強制轉換成page錯,網上有人說不搭邊的型別轉換當然錯,其實不然。

參考的文章沒錯,為什麼我有錯?

即maven引入出錯,應該引入如下:

 <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>

而不是

  <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>3.4.2</version>
        </dependency>