To open project Management, you need to see a list showing the project data, such as this (screenshot is a demo of the front-end framework, just for illustration) :

Then the project management functions corresponding to our platform need to include:

  • List shows the added project data
  • The specified project can be queried by project name
  • The new project
  • Edit item
  • Other features…

The interface of the new item has been implemented. Next, implement the interface of the item list.

The list interface, which I need to be able to handle both paging and conditional queries, is somewhere around /list/{currentPage}/{pageSize}.

CurrentPage indicates the currentPage number, and pageSize indicates the pageSize. For example, /list/1/5} would indicate that I want to look up page 1 and show 5 data items per page.

First, the paging plug-in

Mybatis – Plus provides some useful plugins, including the paging plugin.

Create a new MybatisPlusConfig class to store the configuration of mybatis-plus plugin, and register the paging plugin:

package com.pingguo.bloomtest.config; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @configuration Public class MybatisPlusConfig {@bean public PaginationInterceptor PaginationInterceptor () { return new PaginationInterceptor(); }}Copy the code

Two, implementation interface

Mybatis – Plus also provides the AutoGenerator.

It can quickly generate the code of Entity, Mapper, Mapper XML, Service, Controller and other modules to improve the development efficiency.

But I don’t want to use it here, because it is a learning process, write more to deepen the impression. In addition, the amount of this project is really not big, there is no need to use.

1. Write the Service layer

Under the previous ProjectService, the query method getProjectList was added.

import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; Public IPage<Project> getProjectList(int currentPage, int pageSize, Project Project) { Page<Project> pageProject = new Page<>(currentPage, pageSize); QueryWrapper<Project> wrapperProject = new QueryWrapper<>(); // The first parameter is whether to execute the condition, Wrapperproject.like (stringutils.isnoneblank (project.getprojectName ())), "projectName", project.getProjectName()); Return projectDao.selectPage (pageProject, wrapperProject); return projectDao.selectPage (pageProject, wrapperProject); }Copy the code
  • Create a Page object Page with two parameters, where current is the current Page and size is the maximum number of records per Page.
  • Because the getProjectList method also implements conditional queries, create conditional query objects.
  • Wrapperproject. like uses fuzzy queries. The like method can take three arguments. The first argument is whether to execute the condition or not.
  • Finally, the selectPage method is called, passing in the paging object -PageProject and constructing the condition object wrapperProject.

The object type returned is IPage and the package name is posted.

2. Write the Controller layer

Add the control method getProjectList under the previous ProjectController class to handle requests:

    @PostMapping("/list/{currentPage}/{pageSize}")
    public Result getProjectList(@PathVariable int currentPage,
                                 @PathVariable int pageSize,
                                 @RequestBody Project project) {
        IPage<Project> IPageProject = projectService.getProjectList(currentPage, pageSize, project);
        return Result.success(IPageProject);

    }
Copy the code

The @pathvariable annotation is used to retrieve the parameters on the path, and the @requestbody annotation is used to retrieve the RequestBody.

Return result. success(IPageProject).

Three, test interface

Test your query interface. There are currently 14 entries in the Project table.

1. Test paging

Localhost: 8080 / bloomtest article/project/list / 2/5, 14 data normally will be divided into three pages, the second page starting from the data of id = 6, each page shows article 5.

I keep going until I get to id=10, and I get 5, and I get it right.

In addition, you can also see several fields in the returned content, which are paginated:

  • "total": 14Indicates a total of 14 records.
  • "size": 5Indicates a maximum of five records per page.
  • "current": 2Indicates the current page is page 2.
  • "pages": 3Indicates 3 pages.

These fields are needed when the front end uses paging controls.

2. Query test conditions

Pass in query parameters:

{"projectName": "Test project 5"}Copy the code

Test fuzzy query:

{
    "projectName": "6"
}
Copy the code

The result is correct.