解決pageHelper分頁失效以及如何配置問題
pageHelper分頁失效及配置問題
我在使用pageHelper的系統(tǒng)中加入mybatis-plus, 結果所有分頁都失效了
原因
我這邊的失效原因初步定為是因為mybatis-plus的自動配置和pageHelper的自動配置沖突了, 導致pageHelper的自動配置失效(最終是加上個配置類解決的)
解決方案
新建一個配置類
/** ?* @Author: WanG ?* @Date: 2019-05-13 18:42 ?* @version: v1.0 ?* @description: TODO ?*/ @Configuration public class MybatisConfig { ? ?@Bean ? ?public PageHelper pageHelper() { ? ? ? PageHelper pageHelper = new PageHelper(); ? ? ? Properties p = new Properties(); ? ? ? p.setProperty("offsetAsPageNum", "true"); ? ? ? p.setProperty("rowBoundsWithCount", "true"); ? ? ? p.setProperty("reasonable", "true"); ? ? ? pageHelper.setProperties(p); ? ? ? return pageHelper; ? ?} }
PageHelper分頁無效及報錯
第一種情況SQL報錯
> Error querying database. Cause: java.sql.SQLSyntaxErrorException: You
> have an error in your SQL syntax; check the manual that corresponds to
> your MySQL server version for the right syntax to use near 'LIMIT 5'
> at line 3
原因:在xml寫的sql帶了分號,由于PageHelper會在sql尾部追加limit,所以導致生成sql時有誤,導致錯誤。
錯誤寫法:
<select id="selectAll" resultMap="BaseResultMap"> ? ? ? ? SELECT * FROM student; </select>
正確寫法:
?<select id="selectAll" resultMap="BaseResultMap"> ? ? ? ? SELECT * FROM student </select>
第二種情況分頁無效
原因:可能是代碼前后順序有問題,應該先寫分頁,再執(zhí)行sql。
錯誤寫法:
List<Student> students = studentMapper.selectAll(); PageHelper.startPage(1, 5, true);
正確寫法:
PageHelper.startPage(1, 5, true); List<Student> students = studentMapper.selectAll();
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
java后端把數(shù)據(jù)轉換為樹,map遞歸生成json樹,返回給前端(后臺轉換)
這篇文章主要介紹了java后端把數(shù)據(jù)轉換為樹,map遞歸生成json樹,返回給前端實例(后臺轉換),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-01-01windows10 JDK安裝及配置環(huán)境變量與Eclipse安裝教程
這篇文章主要介紹了windows10 JDK安裝及配置環(huán)境變量與Eclipse安裝,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10