Oracle中分頁查詢因為存在偽列rownum,sql語句寫起來較為復(fù)雜,現(xiàn)在介紹一種通過使用MyBatis中的RowBounds進行分頁查詢,非常方便。
使用MyBatis中的RowBounds進行分頁查詢時,不需要在 sql 語句中寫 offset,limit,mybatis 會自動拼接 分頁sql ,添加 offset,limit,實現(xiàn)自動分頁。
需要前臺傳遞參數(shù)currentPage和pageSize兩個參數(shù),分別是當(dāng)前頁和每頁數(shù)量,controller層把參數(shù)傳遞給service層即可,下面是service實現(xiàn)的代碼:
package com.xyfer.service.impl; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.session.RowBounds; import com.xyfer.dao.UserDao; import com.xyfer.service.UserService; public class UserServiceImpl implements UserService { private UserDao userDao; @Override public MapqueryUserList(String currentPage, String pageSize) { //查詢數(shù)據(jù)總條數(shù) int total = userDao.queryCountUser(); //返回結(jié)果集 Map resultMap = new HashMap (); resultMap.put("total", total); //總頁數(shù) int totalpage = (total + Integer.parseInt(pageSize) - 1) / Integer.parseInt(pageSize); resultMap.put("totalpage", totalpage); //數(shù)據(jù)的起始行 int offset = (Integer.parseInt(currentPage)-1)*Integer.parseInt(pageSize); RowBounds rowbounds = new RowBounds(offset, Integer.parseInt(pageSize)); //用戶數(shù)據(jù)集合 List