pageHelper is a free plug-in paging, it can be applied to a variety of databases.
Use tab plug can greatly reduce the amount of code, the method used here will be described the plug tab.
1. Download
https://github.com/pagehelper/Mybatis-PageHelper
I'm using here is jsqlparser-2.0.jar + pagehelper-5.1.10.jar
2. How to use:
2.1 Configuring Paging Assistant:
Conf.xml add the following code, note added <properties resource = "db.properties" /> later
<! - Configuration tab widget -> < plugins > <! - com.github.pagehelper package name for the class is PageHelper -> < plugin Interceptor = "com.github.pagehelper.PageInterceptor" > <! - Set database type Oracle, Mysql, MariaDB, SQLite, Hsqldb, PostgreSQL database six kinds -> < Property name = "helperDialect" value = "MySQL" /> </ plugin > </ plugins >
2.2 is defined UsersMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- com.zhiyou.zyl.UsersMapper.getUser --> <mapper namespace="com.zhiyou.zyl.dao.UsersDao"> <select id="selectAll" resultType="com.zhiyou.zyl.bean.Users"> select * from users </select> </mapper>
2.3 Test Page
class UsersTest { static the SqlSession the session = null ; static UsersDao UD; @BeforeAll static void setUpBeforeClass () throws Exception { String Resource = "conf.xml" ; // load mybatis profile (which also load the associated map file) Reader Reader = Resources.getResourceAsReader (Resource); // build sqlSession factory SqlSessionFactory sessionFactory = new new the SqlSessionFactoryBuilder () build (Reader);. // create a map file can be executed in the sql sqlSession the session = sessionFactory.openSession(); ud=session.getMapper(UsersDao.class); } @AfterAll static void tearDownAfterClass() throws Exception { //提交 session.commit(); } @Test void testSelectAll() { int pageNum=1; int pageSize=2; PageHelper.startPage(pageNum, pageSize); List<Users> users=ud.selectAll(); PageInfo<Users> list = new PageInfo<Users>(users); System.out.println(list); }
2.4 operating results
PageInfo{pageNum=1, pageSize=2, size=2, startRow=1, endRow=2, total=4, pages=2, list=Page{count=true, pageNum=1, pageSize=2, startRow=0, endRow=2, total=4, pages=2, reasonable=false, pageSizeZero=false}[Users [id=1, name=张三, age=18], Users [id=2, name=李四, age=19]], prePage=0, nextPage=2, isFirstPage=true, isLastPage=false, hasPreviousPage=false, hasNextPage=true, navigatePages=8, navigateFirstPage=1, navigateLastPage=2, navigatepageNums=[1, 2]}
2.5pageInfo property description:
pageNum: current page number
pageSize: pieces of data per page
size: The current number of page data
startRow / endRow: start / end number of
total: total number of
pages: Pages
list: the result set
prePage / nextPage: Prev / Next
isFirstPage / isLastPage: whether the first / last page
hasPreviousPage / hasNextPage: Is there Previous / Next
navigatePages: navigation Page Number
navigateFirstPage: the first page navigation
navigateLastPage: navigation Next
navigatePageNums: all navigation pages