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

 更新时间:2021年03月08日 14:32:47   作者:团子.  
这篇文章主要介绍了springboot整合mybatis-plus实现多表分页查询的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

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多表分页查询内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java中的ConcurrentLinkedQueue使用解析

    Java中的ConcurrentLinkedQueue使用解析

    这篇文章主要介绍了Java中的ConcurrentLinkedQueue使用解析,一个基于链接节点的无界线程安全队列,此队列按照 FIFO(先进先出)原则对元素进行排序,队列的头部是队列中时间最长的元素,需要的朋友可以参考下
    2023-12-12
  • Java简单实现调用命令行并获取执行结果示例

    Java简单实现调用命令行并获取执行结果示例

    这篇文章主要介绍了Java简单实现调用命令行并获取执行结果,结合实例形式分析了Java调用ping命令并获取执行结果相关操作技巧,需要的朋友可以参考下
    2018-08-08
  • Java读取、写入文件如何解决乱码问题

    Java读取、写入文件如何解决乱码问题

    这篇文章主要介绍了Java读取、写入文件如何解决乱码问题,需要的朋友可以参考下
    2015-08-08
  • 出现log.info报红的解决方案

    出现log.info报红的解决方案

    这篇文章主要介绍了出现log.info报红的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • Java正则提取中括号中的内容操作示例

    Java正则提取中括号中的内容操作示例

    这篇文章主要介绍了Java正则提取中括号中的内容操作,涉及Java针对字符串的正则匹配、转换、遍历等相关操作技巧,需要的朋友可以参考下
    2018-06-06
  • 如何使用Spring AOP的通知类型及创建通知

    如何使用Spring AOP的通知类型及创建通知

    这篇文章主要给大家介绍了关于如何使用Spring AOP的通知类型及创建通知的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring AOP具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-12-12
  • Java Map遍历2种实现方法代码实例

    Java Map遍历2种实现方法代码实例

    这篇文章主要介绍了Java Map遍历2种实现方法代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10
  • Maven的聚合(多模块)和Parent继承

    Maven的聚合(多模块)和Parent继承

    今天小编就为大家分享一篇关于Maven的聚合(多模块)和Parent继承,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • spring-cloud入门之eureka-server(服务发现)

    spring-cloud入门之eureka-server(服务发现)

    本篇文章主要介绍了spring-cloud入门之eureka-server(服务发现),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • 详解springboot的三种启动方式

    详解springboot的三种启动方式

    这篇文章主要介绍了详解springboot的三种启动方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06

最新评论