MyBatis-Plus-Join关联查询

  1. 引入依赖

    <dependency>
          <groupId>com.github.yulichang</groupId>
          <artifactId>mybatis-plus-join-boot-starter</artifactId>
          <version>1.4.6</version>
    </dependency>
    
  2. 关联查询

    baseMapper.selectJoinPage(page, StoreEntity.class, new MPJLambdaWrapper<StoreEntity>()
                    .selectAll(StoreEntity.class)
                    .select(DealerEntity::getDealerName)
                    .leftJoin(DealerEntity.class, DealerEntity::getId, StoreEntity::getDistributorId)
                    .eq(store.getStatus() != null, StoreEntity::getStatus, store.getStatus()));
    

    说明:

    • StoreEntity.class是最终返回的DTO
    • new MPJLambdaWrapper()中的StoreEntity是主表查询的实体类
    • .selectAll(StoreEntity.class)是查询主表的全部字段
    • .select(DealerEntity::getDealerName)是查询字表的dealerName字段
    • .leftJoin(DealerEntity.class, DealerEntity::getId, StoreEntity::getDistributorId)是表关联,DealerEntity.class是字表实体类,DealerEntity::getId关联字表的字段,StoreEntity::getDistributorId关联主表的字段
    • .eq(store.getStatus() != null, StoreEntity::getStatus, store.getStatus())这个就是其他的条件查询啦,跟MP是一样的

    以上执行SQL打印

    select t.id,
           t.store_code,
           t.store_name,
           t.distributor_id,
           t.status,
           t.contact_person,
           t.contact_phone,
           t.store_location,
           t.position_lon,
           t.position_lat,
           t.create_by,
           t.create_time,
           t.update_by,
           t.update_time,
           t.dept_id,
           t.tenant_id,
           t.del_flag,
           # 这个是字表的字段
           t1.dealer_name
    from tb_store t
             left join tb_dealer t1 on t1.id = t.distributor_id and t1.tenant_id = 1
    where t.del_flag = '0'
      and t1.del_flag = '0'
      and t.tenant_id = 1
    limit 10
    

    跟写关联查询的sql是一样的

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

讓丄帝愛伱

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值