MybatisPlus多表关联查询

在接触MybaitsPlus的时候,之前使用的mybatis,通常项目中需要用到多表关联查询,但是因为MybatisPlus在官网不支持进行多表关联查询,所以在此记录一下,以供大家多多学习。
网上还推荐一种第三方写的一种插件

链接地址: mybatis-plus-join

功能介绍:
支持连表查询的 mybatis-plus
演示工程
一对一,一对多
可以通过selectJoinPage进行像mybaits的xml中写的那样

一、一对一查询

1.1、查询单条记录

maven文件

 <!-- 实体转换	-->
 <dependency>
     <groupId>org.mapstruct</groupId>
     <artifactId>mapstruct</artifactId>
     <version>1.3.1.Final</version>
 </dependency>
 <dependency>
     <groupId>org.mapstruct</groupId>
     <artifactId>mapstruct-jdk8</artifactId>
     <version>1.3.1.Final</version>
 </dependency>
 <dependency>
     <groupId>org.mapstruct</groupId>
     <artifactId>mapstruct-processor</artifactId>
     <version>1.3.1.Final</version>
 </dependency>
 <dependency>
     <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus-core</artifactId>
     <version>3.1.2</version>
 </dependency>

实体转Vo

package com.whkj.project.converter;


import com.whkj.project.entity.Article;
import com.whkj.project.vo.ArticleVo;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring")
public interface ArticleConverter {

    public ArticleVo entityToDto(Article article);

}

持久层方法

public RestResult get(Integer id) {
    Article article = articleMapper.selectById(id);
    ArticleVo articleVo = articleConverter.entityToDto(article);
    Optional.ofNullable(articleVo).ifPresent(this::addArticleTypeNameInfo);
    return RestResult.ok(articleVo);
}

补充信息方法

private void addArticleTypeNameInfo(ArticleVo vo){
    LambdaQueryWrapper<ArticleType> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(ArticleType::getId,vo.getType());
    ArticleType articleType = articleTypeMapper.selectOne(wrapper);
    Optional.ofNullable(articleType).ifPresent(e -> vo.setTypeName(e.getName()));
}

1.2、查询多条记录

持久层方法

public List<ArticleVo> getArticleByList() {
    // 先查询用户信息(表现形式为列表)
    List<Article> user = articleMapper.selectList(Wrappers.emptyWrapper());
    List<ArticleVo> articleVos = articleConverter.entityToDto(user);
    // 此步骤可以有多个
    addArticleTypeNameInfo(articleVos);
    return articleVos;
}

补充信息方法

private void addArticleTypeNameInfo(List<ArticleVo> vo){
    // 提取类型编号,方便后续循环遍历
     Set<Integer> typeIds = vo.stream().map(ArticleVo::getType).collect(toSet());
     // 根据类型编号查询出符合条件的数据结果集
     LambdaQueryWrapper<ArticleType> wrapper = new LambdaQueryWrapper<>();
     wrapper.in(ArticleType::getId,typeIds);
     List<ArticleType> articleTypes = articleTypeMapper.selectList(wrapper);
     // 构造映射条件,方便匹配类型编号、名称
     Map<Integer, String> hashMap = articleTypes.stream().collect(toMap(ArticleType::getId, ArticleType::getName));
     // 封装Vo,并添加到集合中(关键内容)
     vo.forEach(e -> e.setTypeName(hashMap.get(e.getId())));
 }

1.3、分页列表

持久层方法

/**
 * 分页查询文章列表,一个文章对应一个分类
 */
public IPage<ArticleVo> page(Integer num, Integer size, String keyWords) {
    IPage<Article> articleIPage = null;
    try {
         LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper();
         if (null != keyWords && !keyWords.equals("")) {
             queryWrapper.and(wrapper -> wrapper.like(Article::getTitle, keyWords));
         }
         articleIPage = articleMapper.selectPage(new Page<>(num, size), queryWrapper);
     } catch (Exception e) {
         e.printStackTrace();
     }
    // 初始化Vo
    IPage<ArticleVo> voListPage= articleConverter.entityToDto(articleIPage);
    if (voList.getRecords().size() > 0) {
        addArticleTypeNameInfo(voListPage);
    }
    return userVoPage;
}

补充信息方法

private void addArticleTypeNameInfo(IPage<ArticleVo> voListPage) {
    // 提取用户userId,方便批量查询
    Set<Integer> typeIds = userVoPage.getRecords().stream().map(ArticleVo::getType).collect(toSet());
    // 根据deptId查询deptName
    LambdaQueryWrapper<ArticleType> wrapper = new LambdaQueryWrapper<>();
     wrapper.in(ArticleType::getId,typeIds);
     List<ArticleType> articleTypes = articleTypeMapper.selectList(wrapper);
     // 构造映射条件,方便匹配类型编号、名称
     Map<Integer, String> hashMap = articleTypes.stream().collect(toMap(ArticleType::getId, ArticleType::getName));
    // 将查询补充的信息添加到Vo中
    voListPage.convert(e -> e.setTypeName(hashMap.get(e.getId())));
}

二、一对多查询

2.1、查询单条记录

2.2、查询多条数据

2.3、分页列表

三、多对多查询

3.1、查询单条记录

3.2、查询多条数据

3.3、分页列表

通过该种方式想法是:https://blog.51cto.com/u_15495434/4955709该博客的想法

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我明白了。有关于mybatisplus多表关联查询的问题,我可以给你一些简单的解释和示例。 在mybatisplus中,可以通过注解或者XML来进行多表关联查询。其中,注解方式可以使用@JoinQuery注解。示例代码如下: ```java @Data public class User { private Long id; private String name; private Integer age; private String email; } @Data public class Order { private Long id; private Long userId; private String productName; private BigDecimal price; @TableField(exist = false) private User user; } @TableName("user") @Data public class UserVo extends User { @TableField(exist = false) private List<Order> orders; } public interface UserMapper extends BaseMapper<User> { @Select("SELECT * FROM user WHERE id=#{id}") @JoinQuery(value="test_order",entity=Order.class,on="user.id=order.user_id",type=JoinType.LEFT_JOIN,resultMap="userVo") UserVo getByIdWithOrder(@Param("id") Long id); } ``` 在这个例子中,我们定义了两个实体类:User和Order。然后,我们使用@JoinQuery注解对UserVo实体类进行关联查询关联查询的条件是user.id=order.user_id,使用了左连接(JoinType.LEFT_JOIN)。返回的结果使用了UserVo这个实体类封装。 这个例子中,我们查询了UserVo和Order两个表,返回了id为指定id的用户以及其所拥有的所有订单。 希望这个示例能够帮助你理解mybatisplus多表关联查询的实现方式。如果你有更多的问题,可以继续问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

故里明月

感谢大大的打赏,俺会继续努力的

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值