阅读 13 SEO

SpringBoot Mybatis-Plus 分页模糊查询 分页参数和响应封装

上一篇 SpringBoot集成Mysql、Mybatis、Mybatis-Plus,实现增删改查

一、前言

Mybatis-Plus封装了分页操作,简单配置一下就可以用。我这里还简单做了分页请求参数的封装和响应的封装。

二、Sql语句

CREATE TABLE `user_info` (
    `id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '用户id',
    `username` VARCHAR(20) NOT NULL DEFAULT '' COMMENT '用户名' COLLATE 'utf8mb4_general_ci',
    `password` VARCHAR(100) NOT NULL DEFAULT '' COMMENT '密码' COLLATE 'utf8mb4_general_ci',
    `is_deleted` INT(2) NOT NULL DEFAULT '0' COMMENT '是否删除 0-未删除 1-已删除',
    `create_time` DATETIME NOT NULL COMMENT '创建时间',
    `update_time` DATETIME NOT NULL COMMENT '更新时间',
    PRIMARY KEY (`id`) USING BTREE
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=3
;

三、配置分页

@Configuration
public class MyConfiguration {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 数据库分页
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}
  • 这里分页类型 DbType.MYSQL

四、分页请求参数和响应的封装

BasePageParam 分页参数类

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 +
                '}';
    }
}

BasePageResult 分页响应类

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 +
                '}';
    }
}

五、分页模糊查询

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));
    }
}
  • 根据keyword 模糊匹配用户名,LambdaQueryWrapperlike
  • UserInfoPageParam继承了BasePageParam

UserInfoPageParam 请求参数类

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();
    }
}

六、测试

QQ截图20210517142502.png

七、结语

作者:小虎哥的技术博客

原文链接:https://www.jianshu.com/p/febd391572f8

文章分类
后端
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 gxwowoo@163.com 举报,一经查实,本站将立刻删除。
相关推荐