Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Mybatis to achieve paging effect

Shulou Source: shulou.com Published: 2022-06-02 07:05:45 09月14日 Update

This article mainly introduces how to use Mybatis to achieve paging effect, the article is very detailed, has a certain reference value, interested friends must read it!

First, create a paging tool class PageUti, which stores five pieces of data needed for paging: pageIndex (current page number), pageSize (number of records per page), count (total number of records), pageNum (total number of pages), list (content of each page).

/ * * @ param generic class * / public class PageUtil {/ / current page number private int pageIndex; / / records per page private int pageSize; / / total records private int count; / / total pages private int pageNum; / / content private List list; per page}

Second, xml files: MySQL supports paging through the limit clause.

Such as:

# 10 records per page. Take the first page and return the first 10 records

Select * from books limit (1-1) * 10pm 10

# 10 records per page, take the second page, return the 11th record, to the 20th record

Select * from books limit (2-1) * 10pm 10

So: the first parameter of limit = (current page number-1) * records per page

Select * from books limit # {pageIndex}, # {pageSize} select count (*) from books

Third, BooksServiceImpl.java: save the two parameters needed by limit into the map collection, and then encapsulate the five data needed for paging.

Public PageUtil getBooksPage (int pageIndex, int pageSize) {PageUtil pageUtil=new PageUtil (); SqlSession sqlSession= MybatisUtil.getSqlSession (); try {/ / booksMapper is a proxy object created by using dynamic proxy mode and running reflection mechanism, BooksMapper booksMapper=sqlSession.getMapper (BooksMapper.class); Map map=new HashMap (); int row=booksMapper.getCount (map) / / calculate the total number of pages int pageCount= (int) Math.ceil ((double) row / pageSize); / / the first parameter of limit = (current page number-1) * records per page map.put ("pageIndex", (pageIndex-1) * pageSize); map.put ("pageSize", pageSize); List list=booksMapper.getBooksPage (map) / / encapsulate data into pageUtil.setPageIndex (pageIndex); pageUtil.setPageSize (pageSize); pageUtil.setCount (row); pageUtil.setPageNum (pageCount); pageUtil.setList (list);} catch (Exception e) {e.printStackTrace ();} finally {sqlSession.close ();} return pageUtil }

4. Servlet:

Protected void getBooks (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {if (request.getParameter ("pageIndex")! = null) {int pageIndex=Integer.parseInt (request.getParameter ("pageIndex")); show (response,pageIndex,search);} else {show (response);}} private void show (HttpServletResponse response,Object... ages) throws IOException {int pageIndex= 1 / / default first page if (ages.length > 0) {pageIndex= (int) ages [0];} PageUtil pageUtil=new PageUtil (); / / 10 pieces of data per page pageUtil=new BooksServiceImpl () .getBooksPage (pageIndex,10); String js= JSON.toJSONString (pageUtil); PrintWriter pw=response.getWriter (); pw.write (js);}

Fifth, the front page

Show pagination:

Var page = "total" + data.count + ", total" + data.pageNum + "page, current" + data.pageIndex + "page"; page + = "home page"; if (data.pageIndex = = 1) {page + = "previous page";} else {page + = "previous page";} / / for (var I = 1; I)

Tags: Page number content data parameter object page number effect tool file method article previous agent encapsulation two value interest dynamic clause buddy Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS NVidia Xiaomi Docker Microsoft