• 利用mybatis-plus的分页插件在xml文件中联表查询实现分页(MySQL数据库)



    简要说明:利用mybatis-plus的分页插件在xml文件中联表查询实现分页(MySQL数据库)主要的代码说明,详情可以往后看。
    假设有三张表(这三张表在: SpringBoot整合mybatis-plus-CSDN博客,有 )的关系如图所示
    在这里插入图片描述
    假设是从数据库中, 如根据用户id查询用户具有的角色列表,查询第一页,每页两条数据。对应的SQL语句为,这里只是说明一下,代码在后面

    # 如根据用户id查询用户具有的角色列表,查询第一页,每页两条数据。
    select sr.role_name
    from sys_user su
             left join sys_user_role sur on su.id = sur.user_id
             left join sys_role sr on sur.role_id = sr.id
    where su.id = 1
    limit 0, 2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    查询得到的结果为:

    在这里插入图片描述
    结合mybatis-plus分页插件(实现联表查询分页)的简要代码大概如下,就是把分页插件的page对象传给一方法就行了,MP会自动帮我们分页的,如果是联表。就把联表得到的数据表看作是一张单表,然后分页方法就当做是单表的分页也行。具体可以看官网:有详细对分页插件的详细说明:分页插件 | MyBatis-Plus (baomidou.com)

    UserMapper.java

    // 下面的currentPage, pageSize,userId, roleName都是Controller层传入,或者自己看着填写,这里只是说明一下UserMapper.java的参数来源
    // IPage page = new Page<>(currentPage, pageSize);
    // IPage roleVoIPage = userService.getUlserListByMulTable(page, userId, roleName);
    // IPage roleVoIPage = userMapper.getUlserListByMulTable(page, userId, roleName);
    /**
         * 联表查询实现分页, 如这里是根据用户ID查询用户具有的角色列表,就把联表查询得到的数据当成一张单表来看就行了
         * 如果是多个条件,将Page对象放在第一个参数传进去,在xml文件的SQL语句中使用分页插件分页
         * @param page
         * @param userId
         * @param roleName
         * @return
         */
        IPage<RoleVo> getUlserListByMulTable(
                @Param("page") IPage<RoleVo> page,
                @Param("userId") Long userId,
                @Param("roleName") String roleName);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    UserMapper.xml

    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.ygy.mapper.UserMapper">
       
        <select id="getUlserListByMulTable" resultType="com.ygy.domain.vo.RoleVo">
            select sr.role_name
            from sys_user su
                     left join sys_user_role sur on su.id = sur.user_id
                     left join sys_role sr on sur.role_id = sr.id
            where su.id = #{userId}
            <if test="roleName != null and roleName != ''">
                AND  sr.role_name like concat('%', #{roleName}, '%')
            if>
        select>
    mapper>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    大概结果如下图所示:对着上面个分页SQL语句的截图看会更好一点

    查询第一页数据,其中截图中的currentPage是当前页码数,pageSize是每页显示条数
    在这里插入图片描述

    IDEA控制台打印的SQL语句如下图所示

    在这里插入图片描述

    多条件查询,其中截图中的currentPage是当前页码数,pageSize是每页显示条数

    在这里插入图片描述

    IDEA控制台打印的SQL语句如下图所示

    在这里插入图片描述

    下面有详细的说明,感兴趣的可以往下看。下面有详细的说明,感兴趣的可以往下看。下面有详细的说明,感兴趣的可以往下看。

    准备工作

    SpringBoot2.7.10 + JDK17 + MySQL8.0.30社区版 + Mybatis-Plus,SpringBoot整合mybatis-plus看这篇文章:SpringBoot整合mybatis-plus-CSDN博客 这里有说明SpringBoot + Mybatis-Plus的整合和下文需要用到的数据库表数据,这里需要用到这篇文章中的三张表,实现联表查询,以及对应的Maven依赖,都有说明。直接从这篇文章中导入即可。

    Mybatis-Plus分页插件配置

    mybatis-plus内置了一个分页插件,并且适配多种数据库,具体看官网就知道了,不过需要配置才能生效,详情看官网。分页插件 | MyBatis-Plus (baomidou.com),在项目中的配置如下所示:

    在这里插入图片描述

    MybatisPlusConfig.java

    package com.ygy.config;
    
    import com.baomidou.mybatisplus.annotation.DbType;
    import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
    import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * MP分页插件配置
     */
    @Configuration
    // 这个包扫描因为在启动类中已经配置了,所以这里注释掉就好了,不然会报警告,并不会出错
    //@MapperScan("com.ygy.mapper") 
    public class MybatisPlusConfig {
    
        /**
         * 添加分页插件
         */
        @Bean
        public MybatisPlusInterceptor mybatisPlusInterceptor() {
            MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
            // 这里可以根据需求配置数据库,我配过PostgreSQL的分页插件,跟MySQL是一样的道理,就在这里改数据源就好了
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//如果配置多个插件,切记分页最后添加
            //interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); 如果有多数据源可以不配具体类型 否则都建议配上具体的DbType
            return interceptor;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    进行分页测试

    配置好分页插件之后。我们就可以通过MP内置的Page对象进行分页查询了,Page对象包括的内容如下所示:图片来源于:分页插件 | MyBatis-Plus (baomidou.com),这个需要大概看一下。
    在这里插入图片描述
    然后写对应的接口,就可以进行分页测试了,目录结构如下所示
    在这里插入图片描述

    domain层

    直接复制即可,如果不想复制的话,使用SpringBoot整合mybatis-plus-CSDN博客 这篇文章的代码生成器生成也行。,User.java

    package com.ygy.domain;
    
    import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.annotation.TableId;
    import com.baomidou.mybatisplus.annotation.TableName;
    import java.io.Serializable;
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    import lombok.Getter;
    import lombok.Setter;
    
    /**
     * 

    * 用户表 *

    * * @author ygy * @since 2023-11-05 */
    @Getter @Setter @TableName("sys_user") @ApiModel(value = "User对象", description = "用户表") public class User implements Serializable { private static final long serialVersionUID = 1L; @ApiModelProperty("用户ID") @TableId(value = "id", type = IdType.AUTO) private Long id; @ApiModelProperty("姓名") private String name; @ApiModelProperty("年龄") private Integer age; @ApiModelProperty("性别(0,女,1,男)") private Integer sex; }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    PageVo.java

    package com.ygy.domain.vo;
    
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    import lombok.AllArgsConstructor;
    import lombok.Builder;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    import java.util.List;
    
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    @ApiModel(value = "PageVo对象", description = "分页返回结果对象")
    public class PageVo {
    
        @ApiModelProperty("当前页码数")
        private Long currentPage;
        @ApiModelProperty("每页显示条数")
        private Long pageSise;
        @ApiModelProperty("查询列表总记录数")
        private Long pageTotal;
        @ApiModelProperty("查询数据列表")
        private List records;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    Controller层

    直接复制即可,如果不想复制的话,使用SpringBoot整合mybatis-plus-CSDN博客 这篇文章的代码生成器生成也行。,UserController .java

    package com.ygy.controller;
    
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.ygy.domain.User;
    import com.ygy.domain.vo.PageVo;
    import com.ygy.service.IUserService;
    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.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    /**
     * 

    * 用户表 前端控制器 *

    * * @author ygy * @since 2023-11-05 */
    @Api(tags = "用户管理模块") // 这个是swagger的注解 @RestController @RequestMapping("/user") public class UserController { @Autowired private IUserService userService; /** * 其中 @ApiParam("当前页码数")、@ApiParam("每页显示条数")、@ApiOperation("分页获取用户列表")是swagger的注解 * @param currentPage 当前页码数 * @param pageSize 每页显示条数 * @return 分页结果封装 */ @GetMapping("/getUlserListByPage") @ApiOperation("分页获取用户列表") public PageVo getUlserListByPage( @ApiParam("当前页码数") Integer currentPage, @ApiParam("每页显示条数") Integer pageSize) { IPage<User> page = new Page<>(currentPage, pageSize); // 这里就是调用MP的分页插件,然后得到一个分页结果 IPage<User> userIPage = userService.page(page); // 封装一下分页结果 PageVo pageVo = new PageVo(); pageVo.setCurrentPage(userIPage.getCurrent()); pageVo.setPageSise(userIPage.getSize()); pageVo.setPageTotal(userIPage.getTotal()); pageVo.setRecords(userIPage.getRecords()); return pageVo; } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    Service层

    直接复制即可,如果不想复制的话,使用SpringBoot整合mybatis-plus-CSDN博客 这篇文章的代码生成器生成也行。,IUserService .java

    package com.ygy.service;
    
    import com.ygy.domain.User;
    import com.baomidou.mybatisplus.extension.service.IService;
    
    import java.util.List;
    
    /**
     * 

    * 用户表 服务类 *

    * * @author ygy * @since 2023-11-05 */
    public interface IUserService extends IService<User> { }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    ServiceImpl

    直接复制即可,如果不想复制的话,使用SpringBoot整合mybatis-plus-CSDN博客 这篇文章的代码生成器生成也行。UserServiceImpl.java

    package com.ygy.service.impl;
    
    import com.ygy.domain.User;
    import com.ygy.mapper.UserMapper;
    import com.ygy.service.IUserService;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    /**
     * 

    * 用户表 服务实现类 *

    * * @author ygy * @since 2023-11-05 */
    @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService { }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    Mapper层

    直接复制即可,如果不想复制的话,使用SpringBoot整合mybatis-plus-CSDN博客 这篇文章的代码生成器生成也行。UserMapper.java

    package com.ygy.mapper;
    
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.ygy.domain.User;
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import org.apache.ibatis.annotations.Param;
    import org.apache.ibatis.annotations.Select;
    
    import java.util.List;
    
    /**
     * 

    * 用户表 Mapper 接口 *

    * * @author ygy * @since 2023-11-05 */
    public interface UserMapper extends BaseMapper<User> { }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    这里不需要UserMapper.xml也可以

    分页接口测试

    如数据表中的数据如下图所示,一共12条数据,这里我使用的是IDEA内置的MySQL图形化操作插件工具,也可以使用Navicat,这个不是重点。重点是下面的数据内容
    在这里插入图片描述
    查询第一页数据,每页两条数据

    在这里插入图片描述

    IDEA控制台打印的SQL语句如下图所示

    在这里插入图片描述
    查询第二页数据,每页两条数据

    在这里插入图片描述

    IDEA控制台打印的SQL语句如下图所示

    在这里插入图片描述
    从控制台可以看出,这个分页插件可以帮我们自动计算,然后利用SQL语句的的limit实现分页。挺好用的。到此,说明分页插件配置成功了。

    带其他条件的分页查询

    domain层、Service层、mapper层、ServiceImpl的代码都跟上面一样,不用变,什么都不用改,这里就是改一下UserController.java的内容

    package com.ygy.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.ygy.domain.User;
    import com.ygy.domain.vo.PageVo;
    import com.ygy.service.IUserService;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import io.swagger.annotations.ApiParam;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    /**
     * 

    * 用户表 前端控制器 *

    * * @author ygy * @since 2023-11-05 */
    @Api(tags = "用户管理模块") // 这个是swagger的注解 @RestController @RequestMapping("/user") public class UserController { @Autowired private IUserService userService; /** * 其中 @ApiParam("当前页码数")、@ApiParam("每页显示条数")、@ApiOperation("分页获取用户列表")是swagger的注解 * @param currentPage 当前页码数 * @param pageSize 每页显示条数 * @return 分页结果封装 */ @GetMapping("/getUlserListByPage") @ApiOperation("分页获取用户列表") public PageVo getUlserListByPage( @ApiParam("当前页码数") Integer currentPage, @ApiParam("每页显示条数") Integer pageSize, @ApiParam("姓名") String name) { IPage<User> page = new Page<>(currentPage, pageSize); // 这里就是调用MP的分页插件,然后得到一个分页结果 LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>(); // 动态SQL lqw.like(StringUtils.isNotEmpty(name), User::getName, name); // 想要在分页的时候同时实现构造where条件查询,直接在内置的page方法中传入条件就行了 IPage<User> userIPage = userService.page(page,lqw); // 封装一下分页结果 PageVo pageVo = new PageVo(); pageVo.setCurrentPage(userIPage.getCurrent()); pageVo.setPageSise(userIPage.getSize()); pageVo.setPageTotal(userIPage.getTotal()); pageVo.setRecords(userIPage.getRecords()); return pageVo; } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    分页带其他条件查询结果如下所示:

    在这里插入图片描述

    IDEA控制台打印的SQL语句如下图所示

    在这里插入图片描述

    自定义的 mapper#method(xml文件中) 使用分页

    目录结构如下所示:xml文件放在resources目录下的mapper中,mybatis-plus默认配置,就是读取这个目录下的xml文件
    在这里插入图片描述

    domain层

    domain层多加了一个RoleVo类,这个随便写的,只有一个字段,我要的是达到目的就行,其他的跟上面的一样,什么都没有变化。UserVo.java。用户接受联表查询得到的数据

    package com.ygy.domain.vo;
    
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    import lombok.Data;
    
    @Data
    @ApiModel(value = "RoleVo对象", description = "角色表")
    public class RoleVo {
    
        @ApiModelProperty("角色名称")
        private String RoleName;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    Controller层

    UserController.java,就是在上面的基础上加了一个接口方法

    package com.ygy.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.ygy.domain.User;
    import com.ygy.domain.vo.PageVo;
    import com.ygy.domain.vo.RoleVo;
    import com.ygy.service.IUserService;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import io.swagger.annotations.ApiParam;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * 

    * 用户表 前端控制器 *

    * * @author ygy * @since 2023-11-05 */
    @Api(tags = "用户管理模块") // 这个是swagger的注解 @RestController @RequestMapping("/user") public class UserController { @Autowired private IUserService userService; /** * 其中 @ApiParam("当前页码数")、@ApiParam("每页显示条数")、@ApiOperation("分页获取用户列表")是swagger的注解 * @param currentPage 当前页码数 * @param pageSize 每页显示条数 * @return 分页结果封装 */ @GetMapping("/getUlserListByPage") @ApiOperation("分页获取用户列表") public PageVo getUlserListByPage( @ApiParam("当前页码数") Integer currentPage, @ApiParam("每页显示条数") Integer pageSize, @ApiParam("姓名") String name) { IPage<User> page = new Page<>(currentPage, pageSize); // 这里就是调用MP的分页插件,然后得到一个分页结果 LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>(); // 动态SQL lqw.like(StringUtils.isNotEmpty(name), User::getName, name); // 想要在分页的时候同时实现构造where条件查询,直接在内置的page方法中传入条件就行了 IPage<User> userIPage = userService.page(page,lqw); // 封装一下分页结果 PageVo pageVo = new PageVo(); pageVo.setCurrentPage(userIPage.getCurrent()); pageVo.setPageSise(userIPage.getSize()); pageVo.setPageTotal(userIPage.getTotal()); pageVo.setRecords(userIPage.getRecords()); return pageVo; } /** * 其中 @ApiParam("当前页码数")、@ApiParam("每页显示条数")、@ApiOperation("分页获取用户列表")是swagger的注解 * @param currentPage 当前页码数 * @param pageSize 每页显示条数 * @return 分页结果封装 */ @GetMapping("/getUlserListByPage2") @ApiOperation("在xml文件的SQL语句中使用分页插件分页") public PageVo getUlserListByPage2( @ApiParam("当前页码数") Integer currentPage, @ApiParam("每页显示条数") Integer pageSize) { IPage<User> page = new Page<>(currentPage, pageSize); // 将Page对象传进去,在xml文件的SQL语句中使用分页插件分页 IPage<User> userIPage = userService.getUserListByPage(page); // 封装一下分页结果 PageVo pageVo = new PageVo(); pageVo.setCurrentPage(userIPage.getCurrent()); pageVo.setPageSise(userIPage.getSize()); pageVo.setPageTotal(userIPage.getTotal()); pageVo.setRecords(userIPage.getRecords()); return pageVo; } /** * 其中 @ApiParam("当前页码数")、@ApiParam("每页显示条数")、@ApiOperation("分页获取用户列表")是swagger的注解 * @param currentPage 当前页码数 * @param pageSize 每页显示条数 * @param name 姓名 * @return 分页结果封装 */ @GetMapping("/getUlserListBynNameAndPage") @ApiOperation("在xml文件的SQL语句中带查询条件和使用分页插件分页") public PageVo getUlserListBynNameAndPage( @ApiParam("当前页码数") Integer currentPage, @ApiParam("每页显示条数") Integer pageSize, @ApiParam("姓名") String name ) { IPage<User> page = new Page<>(currentPage, pageSize); // 如果是多个条件,将Page对象放在第一个参数传进去,在xml文件的SQL语句中使用分页插件分页 IPage<User> userIPage = userService.getUlserListBynNameAndPage(page, name); // 封装一下分页结果 PageVo pageVo = new PageVo(); pageVo.setCurrentPage(userIPage.getCurrent()); pageVo.setPageSise(userIPage.getSize()); pageVo.setPageTotal(userIPage.getTotal()); pageVo.setRecords(userIPage.getRecords()); return pageVo; } /** * 联表查询实现分页,如这里是根据用户ID查询用户具有的角色列表,就把联表查询得到的数据当成一张单表来看就行了 * * 其中 @ApiParam("当前页码数")、@ApiParam("每页显示条数")、@ApiOperation("分页获取用户列表")是swagger的注解 * @param currentPage 当前页码数 * @param pageSize 每页显示条数 * @param userId 用户id * @param roleName 角色名称 * @return 分页结果封装 */ @GetMapping("/getUlserListByMulTable") @ApiOperation("联表查询实现分页") public PageVo getUlserListByMulTable( @ApiParam("当前页码数") Integer currentPage, @ApiParam("每页显示条数") Integer pageSize, @ApiParam("用户ID") Long userId, @ApiParam("角色名称") String roleName) { IPage<RoleVo> page = new Page<>(currentPage, pageSize); // 如果是多个条件,将Page对象放在第一个参数传进去,在xml文件的SQL语句中使用分页插件分页 IPage<RoleVo> roleVoIPage = userService.getUlserListByMulTable(page, userId, roleName); // 封装一下分页结果 PageVo pageVo = new PageVo(); pageVo.setCurrentPage(roleVoIPage.getCurrent()); pageVo.setPageSise(roleVoIPage.getSize()); pageVo.setPageTotal(roleVoIPage.getTotal()); pageVo.setRecords(roleVoIPage.getRecords()); return pageVo; } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157

    Service层

    就是在上面的Service基础上,添加一个方法,IUserService.java

    package com.ygy.service;
    
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.ygy.domain.User;
    import com.baomidou.mybatisplus.extension.service.IService;
    import com.ygy.domain.vo.RoleVo;
    
    /**
     * 

    * 用户表 服务类 *

    * * @author ygy * @since 2023-11-05 */
    public interface IUserService extends IService<User> { /** * 将Page对象传进去,在xml文件的SQL语句中使用分页插件分页 * @param page * @return */ IPage<User> getUserListByPage(IPage<User> page); /** * 如果是多个条件,将Page对象放在第一个参数传进去,在xml文件的SQL语句中使用分页插件分页 * @param page * @param name * @return */ IPage<User> getUlserListBynNameAndPage(IPage<User> page, String name); /** * 联表查询实现分页, 如这里是根据用户ID查询用户具有的角色列表,就把联表查询得到的数据当成一张单表来看就行了 * 如果是多个条件,将Page对象放在第一个参数传进去,在xml文件的SQL语句中使用分页插件分页 * @param page * @param userId * @param roleName * @return */ IPage<RoleVo> getUlserListByMulTable(IPage<RoleVo> page, Long userId, String roleName); }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    ServiceImpl

    UserServiceImpl .java,就是在上面的基础上添加一个方法

    package com.ygy.service.impl;
    
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.ygy.domain.User;
    import com.ygy.domain.vo.RoleVo;
    import com.ygy.mapper.UserMapper;
    import com.ygy.service.IUserService;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    /**
     * 

    * 用户表 服务实现类 *

    * * @author ygy * @since 2023-11-05 */
    @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService { @Autowired private UserMapper userMapper; /** * 将Page对象传进去,在xml文件的SQL语句中使用分页插件分页 * @param page * @return */ @Override public IPage<User> getUserListByPage(IPage<User> page) { IPage<User> userIPage = userMapper.getUserListByPage(page); return userIPage; } /** * 如果是多个条件,将Page对象放在第一个参数传进去,在xml文件的SQL语句中使用分页插件分页 * @param page * @param name * @return */ @Override public IPage<User> getUlserListBynNameAndPage(IPage<User> page, String name) { IPage<User> userIPage = userMapper.getUlserListBynNameAndPage(page, name); return userIPage; } /** * 联表查询实现分页, 如这里是根据用户ID查询用户具有的角色列表,就把联表查询得到的数据当成一张单表来看就行了 * 如果是多个条件,将Page对象放在第一个参数传进去,在xml文件的SQL语句中使用分页插件分页 * @param page * @param userId * @param roleName * @return */ @Override public IPage<RoleVo> getUlserListByMulTable(IPage<RoleVo> page, Long userId, String roleName) { IPage<RoleVo> roleVoIPage = userMapper.getUlserListByMulTable(page, userId, roleName); return roleVoIPage; } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    Mapper层

    UserMapper.java,就是在上面的基础上添加一个方法

    package com.ygy.mapper;
    
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.ygy.domain.User;
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.ygy.domain.vo.RoleVo;
    import org.apache.ibatis.annotations.Param;
    
    /**
     * 

    * 用户表 Mapper 接口 *

    * * @author ygy * @since 2023-11-05 */
    public interface UserMapper extends BaseMapper<User> { /** * 将Page对象传进去,在xml文件的SQL语句中使用分页插件分页 * @param page * @return */ IPage<User> getUserListByPage(@Param("page") IPage<User> page); /** * 如果是多个条件,将Page对象放在第一个参数传进去,在xml文件的SQL语句中使用分页插件分页 * @param page * @param name * @return */ IPage<User> getUlserListBynNameAndPage(IPage<User> page, String name); /** * 联表查询实现分页, 如这里是根据用户ID查询用户具有的角色列表,就把联表查询得到的数据当成一张单表来看就行了 * 如果是多个条件,将Page对象放在第一个参数传进去,在xml文件的SQL语句中使用分页插件分页 * @param page * @param userId * @param roleName * @return */ IPage<RoleVo> getUlserListByMulTable( @Param("page") IPage<RoleVo> page, @Param("userId") Long userId, @Param("roleName") String roleName); }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    UserMapper.xml

    三张表的关系如图所示
    在这里插入图片描述
    假设是从数据库中, 如根据用户id查询用户具有的角色列表,查询第一页,每页两条数据。对应的SQL语句为,这里只是说明一下,代码在后面

    # 如根据用户id查询用户具有的角色列表,查询第一页,每页两条数据。
    select sr.role_name
    from sys_user su
             left join sys_user_role sur on su.id = sur.user_id
             left join sys_role sr on sur.role_id = sr.id
    where su.id = 1
    limit 0, 2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    查询得到的结果为:

    在这里插入图片描述

    UserMapper.xml

    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.ygy.mapper.UserMapper">
    
    
        <select id="getUserListByPage" resultType="com.ygy.domain.User">
            select * from sys_user
        select>
    
    
    
        <select id="getUlserListBynNameAndPage" resultType="com.ygy.domain.User">
            select * from sys_user
            <where>
                <if test="name != null and name != ''">
                    AND  name like concat('%', #{name}, '%')
                if>
            where>
        select>
    
       
        <select id="getUlserListByMulTable" resultType="com.ygy.domain.vo.RoleVo">
            select sr.role_name
            from sys_user su
                     left join sys_user_role sur on su.id = sur.user_id
                     left join sys_role sr on sur.role_id = sr.id
            where su.id = #{userId}
            <if test="roleName != null and roleName != ''">
                AND  sr.role_name like concat('%', #{roleName}, '%')
            if>
        select>
    
    
    mapper>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    单表查询的结果

    不是多个查询条件的分页结果如下所示:
    在对应的xml文件中的方法实现分页,第1页,每页2条

    在这里插入图片描述

    IDEA控制台打印的SQL语句如下图所示

    在这里插入图片描述

    在对应的xml文件中的方法实现分页,第2页,每页2条

    在这里插入图片描述

    IDEA控制台打印的SQL语句如下图所示

    在这里插入图片描述

    如果是多个查询条件的分页结果

    在这里插入图片描述

    IDEA控制台打印的SQL语句如下图所示

    在这里插入图片描述

    联表查询分页的结果

    查询第一页数据
    在这里插入图片描述

    IDEA控制台打印的SQL语句如下图所示

    在这里插入图片描述

    多条件查询

    在这里插入图片描述

    IDEA控制台打印的SQL语句如下图所示

    在这里插入图片描述

  • 相关阅读:
    数字孪生技术:智慧运维的未来之路
    第6章 Linux的软件包管理 (二)
    C语言之链表
    【自动驾驶系列丛书学习】2.《自动驾驶汽车环境感知》学习笔记
    centos7安装mysql5.7
    Qml使用cpp文件的信号槽
    Adobe推出AI视频超分辨率工具VideoGigaGAN
    用excel计算一个矩阵的转置矩阵
    2022年9月5号 每周任务
    Spring【注解实现IOC(@Component、@Repository、@Service、@Controller)】(三)-全面详解(学习总结---从入门到深化)
  • 原文地址:https://blog.csdn.net/m0_62317155/article/details/134256473