Paging query--Java project actual combat

Series Article Directory

Global Exception Handling – Java Practical Projects
Perfect Login Function – Use of Filters in
Java Backend Development Functional Module Ideas
Spring Boot Automatic Configuration – How to Switch the Built-in Web Server
SpringBoot Project Deployment
This series of articles is continuously updated, for more articles, please click my Check out the homepage!



foreword

Paging query, everyone must have understood what is paging query when reading this article. This function is also a very common functional module knowledge point in the development process. Most of what the user sees on the displayed page is the data displayed by the paging query. When there is a lot of data, there will be a search box. After the user enters the information in the search box, the background will use fuzzy query to retrieve the information the user searched for. Relevant content is displayed on the page to the user. This process is inseparable from paging queries.

However, it is a very complicated process to write the source code for pagination query. Here Mybatis-Plus provides us with the function of paging query. We only need to write the business logic code based on the packaging in Mybatis-Plus (you can use it), without worrying about how the paging query operation is implemented. .

Next, we will implement a paging query through a case to display employee information on the page.


1. Import dependencies

Just now we talked about paging query, using the plug-in in MyBatis-Plus to realize this function. To use MyBatis-Plus, you need to import the required dependencies first.
Add dependencies in the pom.xml file, the code is as follows:

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>

2. Configure the pagination plug-in

Add the configuration class MybatisPlusConfig under the config package. Under this class, add the PaginationInnerInterceptor() paging query plug-in, which will be loaded when the project starts, and we can also implement the paging plug-in method in it.

/**
 * 配置MP的分页插件
 * */
@Configuration
public class MybatisPlusConfig {
    
    
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
    
    
        MybatisPlusInterceptor mybatisPlusInterceptor=new MybatisPlusInterceptor();

        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return mybatisPlusInterceptor;
    }
}

3. Write code

3.1 View interface information

From the figure below, we can see that the incoming parameter is
page: the data on which page
pageSize: how many pieces of data are displayed on one page
name: the parameter when entering the information for filtering and querying. The
requested path is employee/page. A request sent in GET mode.
As shown below:
insert image description here

3.2 Implement the method according to the interface

We need to know that when returning parameters, the return value is the Page class, which is the class encapsulated by Mybatis-Plus. We can encapsulate the query data and return the Page information.
The code logic is as follows:
1. First, create a paging constructor, and pass in the number of pages to be queried and the pageSize of data to be queried on each page. Like this: Page pageInfo=new Page(page,pageSize);

2. Then use the LambdaQueryWrapper of Mybatis-Plus to perform conditional query. Note: we need to use fuzzy query for the name parameter entered by the user here.

3. Finally, call the page method of Mybatis-Plus. The first parameter is the pageInfo information to be created, and the second is the query condition queryWrapper.

code show as below:

/**
     * 员工信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
    
    
        //创建分页构造器
        Page pageInfo=new Page(page,pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        //查询条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //对查询结果排序
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询
        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

3.3 Page display effect

From the figure below, you can see that the first page of data in the default query is 5, and my database has 8 data, divided into 2 pages.

If you click on the second page, the parameter information is page=2&pageSize=5, but the data on the second page is not enough for 5 items, and the remaining 3 items in the data table will be displayed. Adding the name parameter is to query the corresponding information according to the employee name.
insert image description here


Summarize

The paging query has been implemented here. Here we mainly explain the usage of the paging query function and how to use the paging function. But just like the data echo related to how the front-end page is realized, the front-end knowledge points designed in this case, such as axios asynchronous requests and styles, are not displayed. Here we still focus on the development and implementation of back-end java.

From the article, you can also see that MyBatis-Plus provides us with many powerful functions for us to simplify development. It has greatly improved our development efficiency, and we can focus more on the processing of business logic. Friends who have not studied MyBatis-Plus suggest learning to learn (on the basis of learning MyBatis). Because if you directly learn MP (MyBatis-Plus for short) without learning MyBatis, then there is still a big lack of underlying foundation.

I still need to work hard. Friends who are interested in back-end java development can communicate in private messages~

Guess you like

Origin blog.csdn.net/weixin_52258054/article/details/129048666