SpringBoot实现分页查询

SpringBoot实现分页查询

SpringBoot整合MyBatis-plus实现分页查询

1.创建一个配置类MyBatisPlusConfig

配置类的作用是创建Mybatis-plus的拦截器

package com.yurui.manage_system_springboot.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author YurUi
 * @Version 1.0
 */
@Configuration
public class MyBatisPlusConfig {

    // 配置mybatis-plus拦截器
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 创建分页拦截器,并指定数据库类型,如使用是mysql
        PaginationInnerInterceptor innerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
        // 添加分页拦截器
        interceptor.addInnerInterceptor(innerInterceptor);

        return interceptor;
    }
}

2.Controller控制器方法

package com.yurui.manage_system_springboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yurui.manage_system_springboot.pojo.User;
import com.yurui.manage_system_springboot.pojo.vo.UserVo;
import com.yurui.manage_system_springboot.service.UserService;
import com.yurui.manage_system_springboot.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author YurUi
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin/system/user")
@Api(tags = "系统接口")
public class SystemController {

    @Autowired
    private UserService userService;

    // 分页查询
    @GetMapping("/pageFindUser/{pageNum}/{pageSize}")
    @ApiOperation("分页查询")
    public JsonResult pageFindUser(
            @ApiParam(value = "页码数",name = "pageNum",required = true)
            @PathVariable("pageNum") Long pageNum,
            @ApiParam(value = "每页大小",name = "pageSize",required = true)
            @PathVariable("pageSize")Long pageSize,
            @ApiParam(value = "查询对象",name = "UserVo",required = false)
            UserVo vo
            ){
        // 设置分页参数
        Page<User> page = new Page<>(pageNum,pageSize);

        // 设置service层
        IPage<User> userIPage = userService.selectPageVo(page,vo);

        // 响应数据
        return JsonResult.ok(userIPage);
    }
}    

UserVo类用于模糊查询的条件

package com.yurui.manage_system_springboot.pojo.vo;

import lombok.Data;
import lombok.ToString;

/**
 * @Author YurUi
 * @Version 1.0
 */
// 用户模糊查询条件
@Data
@ToString
public class UserVo {
    private String username;
    private String email;
    private String address;
}

3.Service接口

package com.yurui.manage_system_springboot.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.yurui.manage_system_springboot.pojo.User;
import com.yurui.manage_system_springboot.pojo.vo.UserVo;

import java.util.List;

/**
 * @Author YurUi
 * @Version 1.0
 */
public interface UserService extends IService<User> {
    // 分页查询
    IPage<User> selectPageVo(Page<User> page, UserVo vo);
}

接口实现类:

package com.yurui.manage_system_springboot.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yurui.manage_system_springboot.mapper.UserMapper;
import com.yurui.manage_system_springboot.pojo.User;
import com.yurui.manage_system_springboot.pojo.vo.UserVo;
import com.yurui.manage_system_springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author YurUi
 * @Version 1.0
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    // 分页查询
    @Override
    public IPage<User> selectPageVo(Page<User> page, UserVo vo) {
        IPage<User> p = userMapper.selectPaveVo(page,vo);
        return p;
    }
}

4.Mapper接口

package com.yurui.manage_system_springboot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yurui.manage_system_springboot.pojo.User;
import com.yurui.manage_system_springboot.pojo.vo.UserVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @Author YurUi
 * @Version 1.0
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    // 分页查询
    IPage<User> selectPaveVo(Page<User> page, UserVo vo);
}

xml文件


DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yurui.manage_system_springboot.mapper.UserMapper">

    <resultMap id="UserEntityMap" type="user">
        <id column="id" property="id"/>
        <result column="avatar_url" property="avatarUrl"/>
        <result column="created_time" property="createdTime"/>
        <result column="modified_time" property="modifiedTime"/>
        <result column="created_user" property="createdUser"/>
        <result column="modified_user" property="modifiedUser"/>
        <result column="is_delete" property="isDelete"/>
    resultMap>

    <select id="getAllUser" resultMap="UserEntityMap">
        select * from sys_user where is_delete = 0
    select>

    
    <select id="selectPaveVo" resultMap="UserEntityMap">
        select * from sys_user
        <where>
            <if test="vo.username!=null and vo.username!=''">
                username like "%"#{vo.username}"%"
            if>
            <if test="vo.email!=null and vo.email!=''">
                and email like "%"#{vo.email}"%"
            if>
            <if test="vo.address!=null and vo.address!=''">
                and address like "%"#{vo.address}"%"
            if>
            and is_delete = 0
        where>
        ORDER BY id DESC
    select>

mapper>

5.启动项目测试

测试是整合了Knife4j工具, (244条消息) SpringBoot整合Swagger2_昱晟168的博客-CSDN博客

http://localhost:8888/doc.html

注意是:启动测试的端口必须与项目部署一致,否则Knifed4j页面打不开

SpringBoot实现分页查询_第1张图片
**

你可能感兴趣的:(Spring家庭,spring,boot,mybatis,java)