1.mybatis pageHelper分頁實現
(1)引入依賴
(2)配置pageHelper插件
<configuration>
<!-- 引入 pageHelper插件 -->
<!--注意這里要寫成PageInterceptor, 5.0之前的版本都是寫PageHelper, 5.0之后要換成PageInterceptor-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!--reasonable:分頁合理化參數,默認值為false,直接根據參數進行查詢。
當該參數設置為 true 時,pageNum<=0 時會查詢第一頁, pageNum>pages(超過總數時),會查詢最后一頁。-->
<!--<property name="reasonable" value="true"/>-->
</plugin>
</plugins>
</configuration>
(3)實現分頁
public void queryByPage(User userParam,Integer pageNum,Integer pageSize) {
//利用PageHelper分頁查詢 注意:這個一定要放查詢語句的前一行,否則無法進行分頁,因為它對緊隨其后第一個sql語句有效
PageHelper.startPage(pageNum, pageSize);
List<User> userList = userMapper.queryByPage(userParam);
PageInfo<User> pageInfo = new PageInfo<User>(userList);
}
2.mp簡單查詢分頁實現
如果查詢結果為單表查詢,例如查詢用戶列表,則可以調用mybatis plus的自動生成的mapper中的selectPage()或者selectMapsPage()方法,
Page類的構造函數中第一個參數為當前查詢第幾頁,第二個參數為每頁的記錄數
(1)添加攔截器配置
(2)實現分頁
3.mp復雜(關聯)查詢分頁實現
這種查詢一般需要寫xml。若查詢結果是關聯多個表的操作,則需要用到自定義的mapper,此時的分頁操作也很簡單,只需要給mapper的第一個參數設置為Page對象即可,mybatis接口的第一個參數中,傳遞了Page對象,當mybatis執行此方法的時候,會被mybatis-plus的分頁插件自動攔截到,並且把分頁查詢的結果返回到這個Page對象中
/**
*
* @param page 翻頁對象,可以作為 xml 參數直接使用,傳遞參數 Page 即自動分頁
* @return
*/
@Select("SELECT t_question.*,t_student.`name` FROM t_question,t_student WHERE t_question.student_id=t_student.id")
List<QuestionStudentVO> getQuestionStudent(Pagination page);