MyBatis-Plus-Join关联查询

简介: 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是一样的

相关文章
|
5月前
|
XML Java 数据库连接
mybatis中在xml文件中通用查询结果列如何使用
mybatis中在xml文件中通用查询结果列如何使用
303 0
|
5月前
|
Java 数据库连接 mybatis
Mybatis 多级分类查询
Mybatis 多级分类查询
54 0
|
5天前
|
Java 数据库连接 数据库
mybatis查询数据,返回的对象少了一个字段
mybatis查询数据,返回的对象少了一个字段
28 8
|
6天前
|
SQL XML Java
mybatis复习04高级查询 一对多,多对一的映射处理,collection和association标签的使用
文章介绍了MyBatis中高级查询的一对多和多对一映射处理,包括创建数据库表、抽象对应的实体类、使用resultMap中的association和collection标签进行映射处理,以及如何实现级联查询和分步查询。此外,还补充了延迟加载的设置和用法。
mybatis复习04高级查询 一对多,多对一的映射处理,collection和association标签的使用
|
5月前
|
SQL Java 关系型数据库
Mybatis多表关联查询与动态SQL(下)
Mybatis多表关联查询与动态SQL
114 0
|
5月前
|
SQL Java 数据库连接
Mybatis多表关联查询与动态SQL(上)
Mybatis多表关联查询与动态SQL
133 0
|
5月前
|
SQL 缓存 Java
mybatis 一对多查询
mybatis 一对多查询
82 0
|
5月前
|
SQL XML Java
MyBatis-Plus多表关联查询
MyBatis-Plus多表关联查询
452 0
|
3月前
|
Java 数据库连接 mybatis
Mybatis查询传递单个参数和传递多个参数用法
Mybatis查询传递单个参数和传递多个参数用法
52 11
MybatisPlus-标准CRUD制作,新增boolean save(T t),删除 ~ delete(int id),修改 ~ update(T t),根据id查询,T getById....
MybatisPlus-标准CRUD制作,新增boolean save(T t),删除 ~ delete(int id),修改 ~ update(T t),根据id查询,T getById....