First, write a page class:
Package Penalty for com.dk.util; Import java.util.List; Import java.util.Map; public class PageBean { private List List; // list of records to return a page of private int allRow; // total number of records private int TotalPage; // pages Private int currentPage; // current page Private int pageSize; // page number of records Private boolean isFirstPage; // whether the first page of the Private booleanisLastPage; // is the last one Private Boolean hasPreviousPage; // if there is a front Private Boolean hasNextPage; // if a next public List the getList () { return List; } public void setList (List List) { the this = .list List; } public int getAllRow () { return allRow; } public void setAllRow ( int allRow) { the this .allRow = allRow; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getPageSize() { return pageSize; } public void setPageSize ( int the pageSize) { the this .pageSize = the pageSize; } / ** * ** // * initialize the paging information * / public void the init () { the this .isFirstPage = isFirstPage (); the this .isLastPage = isLastPage (); the this .hasPreviousPage = isHasPreviousPage (); the this .hasNextPage = isHasNextPage (); } / ** * ** // * the following determination information page, just getter method (is method) to * @return * / public BooleanisFirstPage () { return The currentPage ==. 1; // case of this page is the first page. 1 } public Boolean isLastPage () { return The currentPage == TotalPage; // if this page is the last page } public Boolean isHasPreviousPage () { return The currentPage ! = 1; // long as this page is not a page 1 } public Boolean isHasNextPage () { return The currentPage = TotalPage;! // long as this page is not the last one } / ** * ** @ * to calculate the total number of pages, static methods for external calls directly through the class name *@param the pageSize per page recording * @param total number of records allRow * @return Pages * / public static int countTotalPage ( Final int the pageSize, Final int allRow) { int TotalPage = (% allRow the pageSize == 0 && allRow! = ? 0) allRow / pageSize: allRow / pageSize + 1 ; return TotalPage; } / ** * // ** * calculate the current page to start recording * @param pageSize number of records per page * @param currentPage current first few pages * @returnThis page start record number * / public static int countOffset ( Final int the pageSize, Final int The currentPage) { Final int offset; IF (The currentPage == 0 ) { offset = 0 ; } the else { offset = the pageSize * (The currentPage-. 1 ); } return offset; } / ** * ** // * calculate this page, URL or 0 if it is not requested, then replaced with a "page =?" * @param page parameters passed (possibly empty, i.e. 0, 1 is returned) *@return 当前页 */ public static int countCurrentPage(int page){ final int curPage = (page==0?1:page); return curPage; } public static String queryStr(Map<String, String> queryMap) { if(null!=queryMap){ String queryUrl=""; for(Map.Entry<String, String> qm : queryMap.entrySet()){ if(qm.getValue()!=null && !qm.getValue().equals("") && qm.getValue().length()>0){ queryUrl += "&query." + qm.getKey()+"=" + qm.getValue(); } } return queryUrl; } return ""; } }
package com.dk.core.dao; import com.dk.core.vo.User; import org.apache.ibatis.annotations.*; import org.apache.ibatis.type.JdbcType; import java.util.List; @Mapper public interface UserMapper { @Select("${sql}") List<com.dk.core.vo.User> findUser(@Param("sql")String sql); @Select("${sqlCount}") int findUserCount(@Param("sqlCount")String sqlCount); }
service
package com.dk.core.service; import com.dk.core.dao.UserMapper; import com.dk.core.vo.User; import com.dk.util.PageBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Map; @Transactional @Service public class UserService implements IUserService{ @Autowired private UserMapper userMapper; @Override public PageBean getList(int pageSize, int page, Map<String, String> query) { String sql = "from User obj "; if(query != null && query.size() >0 ){ sql += " where obj.deleteStatus = false " ; } //System.out.println("hql = "+hql); int allRow = this.getAllRowCount(sql); int totalPage = PageBean.countTotalPage(pageSize, allRow); final int offset = PageBean.countOffset(pageSize, page); final int length = pageSize; final int currentPage = PageBean.countCurrentPage(page); List list = this.query(sql, offset, length); PageBean pageBean = new PageBean(); pageBean.setPageSize(pageSize); pageBean.setCurrentPage(currentPage); pageBean.setAllRow(allRow); pageBean.setTotalPage(totalPage); pageBean.setList(list); pageBean.init(); return pageBean; } @Override public int getAllRowCount(String sql) { return userMapper.findUserCount(sql); } @Override public List<User> query(String sql, int offset, int length) { if (offset!=-1 || length!= -1){ sql +=" limit "+offset+","+length; } return userMapper.findUser(sql); } }
package com.dk.core.service; import com.dk.core.vo.User; import com.dk.util.PageBean; import java.util.List; import java.util.Map; public interface IUserService { PageBean getList(int pageSize, int page, Map<String, String> query); int getAllRowCount(String sql); List<User> query(String sql, int offset,int length); }
package com.dk.api.control; import com.dk.core.service.IUserService; import com.dk.core.vo.User; import com.dk.util.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @RestController @RequestMapping("/json") public class UserJsonControllor { @Autowired IUserService userService; @RequestMapping(value = "/userList") public Result userList(int page){ int pageSize = 10; String sql = "select count(obj.id) from shopping_user obj"; int allRow = userService.getAllRowCount(sql); //总共多少页 int totalPage = PageBean.countTotalPage(pageSize, allRow); if (page>totalPage){ page=totalPage; } int offset = PageBean.countOffset(pageSize, page); String userSql = "select * from shopping_user obj"; List<User> userList = userService.query(userSql,offset,pageSize); Map map =new HashMap(); map.put("list",userList); return Result.success(map); } }