Spring Boot integrated pagehelper paging plugin


In our projects, we often use query paging, which is also a very practical function. Here I will record the springboot integrated pagehelper, the paging query plug-in

1. Add pagehelper dependency in pom.xml

Add the following code in pom.xml:

        <!-- pagehelper 插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>

2. Modify the configuration file

Add the following code in application.properties:

#pagehelper分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql

3. Modify the query method in the Controller

Add the parameters @RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize), the two parameters indicate the page number and the size of each page; then add to PageHelper.startPage(pageNo,pageSize);indicate the use of this plug-in.
The other codes are the same as before.
The specific code is as follows:

    //查找所有用户
    @ResponseBody
    @RequestMapping("/user/findAllUsers")
    public ResultInfo findAllUsers(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize){
    
    
        try{
    
    
            PageHelper.startPage(pageNo,pageSize);
            List<User> users = userService.findAllUsers();
            if (users.size()<=0){
    
    
                info.setCode(Code.NODATA);
                return info;
            }
            HashMap<String,Object> userHashMap = new HashMap<>();
            userHashMap.put("users",users);
            info.setData(userHashMap);
            return info;
        }catch (Exception e){
    
    
            e.printStackTrace();
        }
        info.setCode(Code.NODATA);
        return info;
    }

Postman test results: It
Insert picture description here
should be noted that the paging code PageHelper.startPage(pageNo,pageSize); is only valid for the first query after it. If you change the code to the following and add a query, the second query does not have pagination

            PageHelper.startPage(pageNo,pageSize);
            List<User> users = userService.findAllUsers(); //这个查询会分页
            List<User> users1 = userService.findAllUsers(); //这个查询不会分页

4. Return page information

Sometimes, our business needs us to return paging information. At this time, we need to modify the return value of the methods in the controller, service, and mapper. Change List to Page. Page is a class in the com.github.pagehelper package. , It is a subclass of java.util.ArrayList.

1. Modify UserMapper

// List<User> selectAll();
Page<User> selectAll();

2. Modify UserService

//List<User> findAllUsers();
 Page<User> findAllUsers();

3. Modify UserServiceImpl


//    @Override
//    public List<User> findAllUsers() {
    
    
//        return userMapper.selectAll();
//    }
    @Override
    public Page<User> findAllUsers() {
    
    
        return userMapper.selectAll();
    }

4. Use com.github.pagehelper.PageInfo class to encapsulate Page data

Key code:

PageInfo<User> pageInfo = new PageInfo<>(userService.findAllUsers());

The complete code is as follows:

    //查找所有用户
    @ResponseBody
    @RequestMapping("/user/findAllUsers")
    public ResultInfo findAllUsers(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize){
    
    
        try{
    
    
            PageHelper.startPage(pageNo,pageSize);
            PageInfo<User> pageInfo = new PageInfo<>(userService.findAllUsers());
            if (pageInfo.getSize()<=0){
    
    
                info.setCode(Code.NODATA);
                return info;
            }
            HashMap<String,Object> userHashMap = new HashMap<>();
            userHashMap.put("users",pageInfo);
            info.setData(userHashMap);
            return info;
        }catch (Exception e){
    
    
            e.printStackTrace();
        }
        info.setCode(Code.NODATA);
        return info;
    }

5. Testing

The result is as follows:
It returned a lot of information on the page.
Insert picture description here
Since then, the simple Spring Boot integrated pagehelper paging plugin is complete!

Guess you like

Origin blog.csdn.net/weixin_43520670/article/details/112587588