Spring boot mybatis plus paging fuzzy query paging parameters and response encapsulation

Time:2022-1-5

LastSpringboot integrates mysql, mybatis and mybatis plus to add, delete, modify and query

1、 Foreword

Mybatis plus encapsulates the paging operation, which can be used after simple configuration. I have also simply encapsulated the paging request parameters and the response.

2、 SQL statement

CREATE TABLE `user_info` (
    `id` BIGINT(20) NOT NULL AUTO_ Increment comment 'user ID',
    `Username ` varchar (20) not null default '' comment 'username' collate 'utf8mb4_ general_ ci',
    `Password ` varchar (100) not null default '' comment 'password' collate 'utf8mb4_ general_ ci',
    `is_ Deleted ` int (2) not null default '0' comment 'delete 0 - not deleted 1 - deleted',
    `create_ Time ` datetime not null comment 'creation time',
    `update_ Time ` datetime not null comment 'update time',
    PRIMARY KEY (`id`) USING BTREE
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=3
;

3、 Configure paging

@Configuration
public class MyConfiguration {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //Database Paging
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}
  • Paging type hereDbType.MYSQL

4、 Encapsulation of paging request parameters and responses

BasePageParamPaging parameter class

package com.llh.springbootpage.param;

/**
 * @author llh
 */
public class BasePageParam {
    private Long pageNum;
    private Long pageSize;

    public Long getPageNum() {
        return pageNum;
    }

    public void setPageNum(Long pageNum) {
        this.pageNum = pageNum;
    }

    public Long getPageSize() {
        return pageSize;
    }

    public void setPageSize(Long pageSize) {
        this.pageSize = pageSize;
    }

    @Override
    public String toString() {
        return "BasePageParam{" +
                "pageNum=" + pageNum +
                ", pageSize=" + pageSize +
                '}';
    }
}

BasePageResultPaging response class

package com.llh.springbootpage.result;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

/**
 * @author llh
 */
public class BasePageResult<T> {
    private List<T> data;
    private Long totalNum;

    public BasePageResult(List<T> data, Long totalNum) {
        this.data = data;
        this.totalNum = totalNum;
    }

    public static <E> BasePageResult<E> newInstance(IPage<E> page) {
        return new BasePageResult<E>(page.getRecords(), page.getTotal());
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public Long getTotalNum() {
        return totalNum;
    }

    public void setTotalNum(Long totalNum) {
        this.totalNum = totalNum;
    }

    @Override
    public String toString() {
        return "BasePageResult{" +
                "data=" + data +
                ", totalNum=" + totalNum +
                '}';
    }
}

5、 Paging fuzzy query

package com.llh.springbootpage.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.llh.springbootpage.config.CommonResult;
import com.llh.springbootpage.entity.UserInfo;
import com.llh.springbootpage.param.UserInfoPageParam;
import com.llh.springbootpage.result.BasePageResult;
import com.llh.springbootpage.service.UserInfoService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author llh
 */
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {
    @Resource
    private UserInfoService userInfoService;

    @PostMapping("/page")
    public CommonResult<BasePageResult<UserInfo>> page(@RequestBody UserInfoPageParam param) {
        IPage<UserInfo> pageParam = new Page<>(param.getPageNum(), param.getPageSize());
        IPage<UserInfo> pageResult = userInfoService.page(pageParam, new LambdaQueryWrapper<UserInfo>()
                .like(UserInfo::getUsername, param.getKeyword()));
        return CommonResult.success(BasePageResult.newInstance(pageResult));
    }
}
  • The user name is fuzzy matched according to the keyword,LambdaQueryWrapperoflike
  • UserInfoPageParamInheritedBasePageParam

UserInfoPageParamRequest parameter class

package com.llh.springbootpage.param;

/**
 * @author admin
 */
public class UserInfoPageParam extends BasePageParam {
    private String keyword;

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    @Override
    public String toString() {
        return "UserInfoPageParam{" +
                "keyword='" + keyword + '\'' +
                "} " + super.toString();
    }
}

6、 Testing

Spring boot mybatis plus paging fuzzy query paging parameters and response encapsulation

QQ screenshot 20210517142502 png

7、 Conclusion