The way paging plug-ins are used

  • Modify the POM file to add paging JAR package dependencies
  • Modify the mybatis. XML file
  • UserDAO interface, UserMapper.xml to add corresponding methods and implement SQL
  • Add a paging query method for the UserService interface
  • Test the paging effect

A case in field

1. Modify the POM file to add paging JAR package dependencies

< the dependency > < groupId > com. Making. Pagehelper < / groupId > < artifactId > pagehelper < / artifactId > < version > 4.1.0 < / version >

2. Modify mybatis. XML file

< plugin interceptor = “com. Making. Pagehelper. Pagehelper” > < property name=”dialect” value=”mysql” />

pages queries last page –>

< property name = “params” value = “pageNum = start; pageSize=limit; pageSizeZero=zero; reasonable=heli; count=cou ntsql” />

3. UserDAO interface, UserMapper. XML to add corresponding methods and implement SQL

UserDao interface:

public interface UserDao {



public User queryUserById(int id);



public List<User> queryUsers();



}

UserMapper.xml

< mapper namespace=”com.mage.dao.UserDao”>

4. Add paging query method corresponding to UserService interface

public interface UserService { public User queryUserById(); / * *

  • Paging method definition

  • @Param Pagenum Current page number

  • @Param PageSize sets the number of pages to display per page

  • @return



*/



public PageInfo<User> queryUsers(int pageNum,int pageSize);



}

UserServiceImpl:

@Service



public class UserServiceImpl implements UserService{



@Resource



private UserDao userDao;



public User queryUserById(){



return userDao.queryUserById(7);



}



@Override



public PageInfo<User> queryUsers(int pageNum, int pageSize) {



/**

  • The PageHelper class sets the page number and size per page

*/ PageHelper.startPage(pageNum, pageSize); List

list=userDao.queryUsers(); PageInfo

pageInfo=new PageInfo

(list); return pageInfo; }}

5. Test paging

Database raw record

test

The first time, pageSize equals 1, pageSize equals 1

@Test public void testQueryUsers() { PageInfo

pageInfo= userService.queryUsers(1, 1); for(User user:pageInfo.getList()){ System.out.println(“user:”+user); }}

Results:

The second pageNum is equal to 2, pageSize is equal to 1

@Test public void testQueryUsers() { PageInfo

pageInfo= userService.queryUsers(2, 1); for(User user:pageInfo.getList()){ System.out.println(“user:”+user); }}

The results of

Note: if the paging plugin passes in the number of pages, it will record the total number of pages. We will get the last page of the record

The third test: PageNum=3 pageSize=1

@Test public void testQueryUsers() { PageInfo

pageInfo= userService.queryUsers(3, 1); for(User user:pageInfo.getList()){ System.out.println(“user:”+user); }}

Results:

extension

Paging plug-in compressed version

Unzip it and use it. Configure config.xml as before, and then run run.bat