springboot整合mybatis-plus实现多表分页查询的示例代码

 更新时间:2021年3月9日 00:00  点击:1515

1.新建一个springboot工程

2.需要导入mybatis和mybatis-plus的依赖文件

<dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>3.1.1</version>
    </dependency>
     <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.0.1</version>
    </dependency>

3.application.yml配置文件

server:
 port: 8080
spring:
 datasource:
  url: jdbc:mysql://localhost:3306/mybatisplus?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=UTC
  username: root
  password: 数据库密码
mybatis:
 mapper-locations: classpath*:mapper/*.xml

mybatis-plus:
 mapper-locations: classpath:/mapper/*Mapper.xml
logging:
 level:
  com.tuanzi.*: debug

4.首先我们需要写一个类来配置分页插件

省略import
@EnableTransactionManagement
@Configuration
@MapperScan("com.tuanzi.*.mapper*")
public class MybatisPlusConfig {

  /**
   * 分页插件
   */
  @Bean
  public PaginationInterceptor paginationInterceptor(){
    return new PaginationInterceptor();
  }
}

5.controller类

@RestController
@RequestMapping("/user")
public class UserController {

  @Autowired
  UserService userService;

  /**
   * 多表关联,分页查询(1对1)
   * @param page
   * @return
   */
  @RequestMapping("/findAll")
  public Result<IPage<User>> findAll(@RequestBody Page<User> page){

     return userService.pages(page);

  }

  /**
   * 多表关联,分页查询(1对多)
   * @param page
   * @return
   */
  @RequestMapping("/selectAll")
  public Result<IPage<User>> selectAll(@RequestBody Page<User> page){

    return userService.pageList(page);

  }
}

6.service类

public interface UserService extends IService<User> {

  Result<IPage<User>> pages(Page<User> page);

  Result<IPage<User>> pageList(Page<User> page);
}

7.service实现类

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

  @Autowired
  UserMapper userMapper;
  @Override
  public Result<IPage<User>> pages(Page<User> page) {
    IPage<User> userIPage = userMapper.Pages(page);
    return Result.getSuccess("分页查询成功",userIPage);
  }

  @Override
  public Result<IPage<User>> pageList(Page<User> page) {
    IPage<User> userIPage = userMapper.pageList(page);
    return Result.getSuccess("分页查询成功",userIPage);
  }
}

8.mapper接口

注意!!: 如果入参是有多个,需要加注解指定参数名才能在xml中取值

@Mapper
@Repository
public interface UserMapper extends BaseMapper<User> {

  IPage<User> Pages(@Param("page") Page<User> page);

  IPage<User> pageList(@Param("page") Page<User> page);
}

9.xml文件

一对一关联

 <!-- 一对一 通用查询映射结果 -->
  <resultMap id="BaseResultMap1" type="com.tuanzi.user.entity.User">
    <result column="id" property="id" />
    <result column="name" property="name" />
    <result column="age" property="age" />
    <result column="email" property="email" />
    <!--assocication  一对一关联查询
        可以指定联合的JavaBean对象
        property="work"指定哪个属性是联合的对象
        javaType:指定这个属性对象的类型
      -->
    <association property="work" javaType="com.tuanzi.user.entity.Work">
      <result column="id" property="id" />
      <result column="position" property="position" />
      <result column="user_id" property="userId" />
    </association>
  </resultMap>

一对多关联

<!-- 一对多 通用查询映射结果 -->
  <resultMap id="BaseResultMap2" type="com.tuanzi.user.entity.User">
    <result column="id" property="id" />
    <result column="name" property="name" />
    <result column="age" property="age" />
    <result column="email" property="email" />
    <!--
		collection定义关联结合类型的属性的封装规则
		property="workList"指定哪个属性是联合的对象
		ofType:指定集合里面元素的类型
		-->
    <collection property="workList" ofType="com.tuanzi.user.entity.Work">
      <result column="id" property="id" />
      <result column="position" property="position" />
      <result column="user_id" property="userId" />
    </collection>
  </resultMap>

SQL语句:

<select id="Pages" resultMap="BaseResultMap1">
    select a.id id,a.name name,a.age age,a.email email,b.position position,b.user_id user_id from user a LEFT JOIN work b on a.id=b.user_id
  </select>
  <select id="pageList" resultMap="BaseResultMap2">
    select a.id id,a.name name,a.age age,a.email email,b.position position,b.user_id user_id from user a LEFT JOIN work b on a.id=b.user_id
  </select>

10.这样就基本完成了!我这里省略了实体类

我们运行一下,用postman测试一下结果
这里我们需要传2个参数,当然我们也可以不用传,因为mybatis-plus有默认值
来看下mybatis-plus的page源码

在这里插入图片描述

效果图:

在这里插入图片描述

在这里插入图片描述

最后附赠源码地址:demo

到此这篇关于springboot整合mybatis-plus实现多表分页查询的示例代码的文章就介绍到这了,更多相关springboot整合mybatis-plus多表分页查询内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

[!--infotagslink--]

相关文章

  • 解决springboot使用logback日志出现LOG_PATH_IS_UNDEFINED文件夹的问题

    这篇文章主要介绍了解决springboot使用logback日志出现LOG_PATH_IS_UNDEFINED文件夹的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-04-28
  • mybatis-plus 表名添加前缀的实现方法

    这篇文章主要介绍了mybatis-plus 表名添加前缀的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-08-26
  • mybatis-plus 返回部分字段的解决方式

    这篇文章主要介绍了mybatis-plus 返回部分字段的解决方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-10-02
  • SpringBoot实现excel文件生成和下载

    这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
  • MyBatis-Plus自动填充功能失效导致的原因及解决

    这篇文章主要介绍了MyBatis-Plus自动填充功能失效导致的原因及解决,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-04
  • mybatis-plus 处理大数据插入太慢的解决

    这篇文章主要介绍了mybatis-plus 处理大数据插入太慢的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-18
  • 详解springBoot启动时找不到或无法加载主类解决办法

    这篇文章主要介绍了详解springBoot启动时找不到或无法加载主类解决办法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-16
  • 解决mybatis-plus 查询耗时慢的问题

    这篇文章主要介绍了解决mybatis-plus 查询耗时慢的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-07-04
  • 使用mybatis-plus报错Invalid bound statement (not found)错误

    这篇文章主要介绍了使用mybatis-plus报错Invalid bound statement (not found)错误,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-02
  • SpringBoot集成Redis实现消息队列的方法

    这篇文章主要介绍了SpringBoot集成Redis实现消息队列的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-10
  • 解决Springboot get请求是参数过长的情况

    这篇文章主要介绍了解决Springboot get请求是参数过长的情况,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-17
  • Springboot+TCP监听服务器搭建过程图解

    这篇文章主要介绍了Springboot+TCP监听服务器搭建过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-10-28
  • Spring Boot项目@RestController使用重定向redirect方式

    这篇文章主要介绍了Spring Boot项目@RestController使用重定向redirect方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-02
  • springBoot 项目排除数据库启动方式

    这篇文章主要介绍了springBoot 项目排除数据库启动方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-10
  • mybatis-plus实体类主键策略有3种(小结)

    这篇文章主要介绍了mybatis-plus实体类主键策略有3种(小结),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-08-27
  • springboot中使用@Transactional注解事物不生效的坑

    这篇文章主要介绍了springboot中使用@Transactional注解事物不生效的原因,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-26
  • SpringBoot接口接收json参数解析

    这篇文章主要介绍了SpringBoot接口接收json参数解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-10-19
  • 详解SpringBoot之访问静态资源(webapp...)

    这篇文章主要介绍了详解SpringBoot之访问静态资源(webapp...),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-14
  • mybatis-plus getOne和逻辑删除问题详解

    这篇文章主要介绍了mybatis-plus getOne和逻辑删除,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-08-26
  • mybatis-plus  mapper中foreach循环操作代码详解(新增或修改)

    这篇文章主要介绍了mybatis-plus mapper中foreach循环操作代码详解(新增或修改),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-11-17