MPJ多表连表查询(包含分页)

2023-11-23 15:50
文章标签 查询 分页 mpj 多表连表

本文主要是介绍MPJ多表连表查询(包含分页),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

MPJ是mybatis plus join 的缩写,以前写多表查询时,一般都是写在配置文件,现在mybatis plus join 完全可以解放写xml文件,功能十分强大。
上代码,本文涉及五张表,连表查询从各个表拿出所需字段输出一条记录。
Device表:
在这里插入图片描述
device_category表
在这里插入图片描述
device_image表:
在这里插入图片描述
device_status表:
在这里插入图片描述
producer表:
在这里插入图片描述
项目工程结构:
在这里插入图片描述
1.首先引入MPJ插件

<dependency><groupId>com.github.yulichang</groupId><artifactId>mybatis-plus-join</artifactId><version>1.3.0</version></dependency>

2.配置config

@Configuration
public class MySqlInjector extends MPJSqlInjector {@Overridepublic List<AbstractMethod> getMethodList(Class<?> mapperClass) {//将原来的保持List<AbstractMethod> methodList = super.getMethodList(mapperClass);//多表查询sql注入 从连表插件里移植过来的methodList.add(new SelectJoinOne());methodList.add(new SelectJoinList());methodList.add(new SelectJoinPage());methodList.add(new SelectJoinMap());methodList.add(new SelectJoinMaps());methodList.add(new SelectJoinMapsPage());return methodList;}
}

mapper包

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
dto:

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@ApiModel(value = "DeviceInfoDto", description = "")
public class DeviceInfoDto implements Serializable {private static final long serialVersionUID = 1L;/** device表* */@ApiModelProperty("设备id")private Long id;/** device表* */@ApiModelProperty("设备名称")private String deviceName;/** device表* */@ApiModelProperty("设备序列号")private String serialNum;/** device表* */@ApiModelProperty("设备型号")private String modelNum;/** device_category表* */@ApiModelProperty("类别名称")private String categoryName;/** producer表* */@ApiModelProperty("设备产商")private String producer;/** device_status表* */@ApiModelProperty("设备状态值")private String statusValue;/** device_image表* */@ApiModelProperty("图片地址")private String imageUrl;
}

主启动:
在这里插入图片描述
测试代码:

 @Testvoid testJoin(){String status = "启动";/* MPJLambdaWrapper<Device> wrapper = MPJWrappers.<Device>lambdaJoin().select(Device::getId,Device::getDeviceName,Device::getSerialNum,Device::getModelNum).select(DeviceCategory::getCategoryName).select(Producer::getProducer).select(DeviceStatus::getStatusValue).select(DeviceImage::getImageUrl).leftJoin(DeviceCategory.class,on-> on.eq(Device::getCategoryId,DeviceCategory::getId)).leftJoin(Producer.class,on-> on.eq(Device::getProducerId,Producer::getId)).leftJoin(DeviceStatus.class,on-> on.eq(Device::getStatusId,DeviceStatus::getId)).leftJoin(DeviceImage.class,on->on.eq(Device::getId,DeviceImage::getDeviceId)).eq(Device::getCompanyId,1L).eq(!Objects.isNull(status),DeviceStatus::getStatusValue,status);List<Device> devices = deviceMapper.selectList(wrapper);*/IPage<DeviceInfoDto> page = deviceMapper.selectJoinPage(new Page<>(1,3),DeviceInfoDto.class,MPJWrappers.<Device>lambdaJoin().select(Device::getId,Device::getDeviceName,Device::getSerialNum,Device::getModelNum).select(DeviceCategory::getCategoryName).select(Producer::getProducer).select(DeviceStatus::getStatusValue).select(DeviceImage::getImageUrl).leftJoin(DeviceCategory.class,on-> on.eq(Device::getCategoryId,DeviceCategory::getId)).leftJoin(Producer.class,on-> on.eq(Device::getProducerId,Producer::getId)).leftJoin(DeviceStatus.class,on-> on.eq(Device::getStatusId,DeviceStatus::getId)).leftJoin(DeviceImage.class,on->on.eq(Device::getId,DeviceImage::getDeviceId)).eq(Device::getCompanyId,1L).eq(!Objects.isNull(status),DeviceStatus::getStatusValue,status));page.getRecords().forEach(System.out::println);}

运行结果:
在这里插入图片描述

这篇关于MPJ多表连表查询(包含分页)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/419026

相关文章

MySQL中between and的基本用法、范围查询示例详解

《MySQL中betweenand的基本用法、范围查询示例详解》BETWEENAND操作符在MySQL中用于选择在两个值之间的数据,包括边界值,它支持数值和日期类型,示例展示了如何使用BETWEEN... 目录一、between and语法二、使用示例2.1、betwphpeen and数值查询2.2、be

MyBatis-Plus使用动态表名分表查询的实现

《MyBatis-Plus使用动态表名分表查询的实现》本文主要介绍了MyBatis-Plus使用动态表名分表查询,主要是动态修改表名的几种常见场景,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录1. 引入依赖2. myBATis-plus配置3. TenantContext 类:租户上下文

MySQL基本表查询操作汇总之单表查询+多表操作大全

《MySQL基本表查询操作汇总之单表查询+多表操作大全》本文全面介绍了MySQL单表查询与多表操作的关键技术,包括基本语法、高级查询、表别名使用、多表连接及子查询等,并提供了丰富的实例,感兴趣的朋友跟... 目录一、单表查询整合(一)通用模版展示(二)举例说明(三)注意事项(四)Mapper简单举例简单查询

MySQL 数据库进阶之SQL 数据操作与子查询操作大全

《MySQL数据库进阶之SQL数据操作与子查询操作大全》本文详细介绍了SQL中的子查询、数据添加(INSERT)、数据修改(UPDATE)和数据删除(DELETE、TRUNCATE、DROP)操作... 目录一、子查询:嵌套在查询中的查询1.1 子查询的基本语法1.2 子查询的实战示例二、数据添加:INSE

springboot+mybatis一对多查询+懒加载实例

《springboot+mybatis一对多查询+懒加载实例》文章介绍了如何在SpringBoot和MyBatis中实现一对多查询的懒加载,通过配置MyBatis的`fetchType`属性,可以全局... 目录springboot+myBATis一对多查询+懒加载parent相关代码child 相关代码懒

在DataGrip中操作MySQL完整流程步骤(从登录到数据查询)

《在DataGrip中操作MySQL完整流程步骤(从登录到数据查询)》DataGrip是JetBrains公司出品的一款现代化数据库管理工具,支持多种数据库系统,包括MySQL,:本文主要介绍在D... 目录前言一、登录 mysql 服务器1.1 打开 DataGrip 并添加数据源1.2 配置 MySQL

Go语言中如何进行数据库查询操作

《Go语言中如何进行数据库查询操作》在Go语言中,与数据库交互通常通过使用数据库驱动来实现,Go语言支持多种数据库,如MySQL、PostgreSQL、SQLite等,每种数据库都有其对应的官方或第三... 查询函数QueryRow和Query详细对比特性QueryRowQuery返回值数量1个:*sql

MyBatis Plus大数据量查询慢原因分析及解决

《MyBatisPlus大数据量查询慢原因分析及解决》大数据量查询慢常因全表扫描、分页不当、索引缺失、内存占用高及ORM开销,优化措施包括分页查询、流式读取、SQL优化、批处理、多数据源、结果集二次... 目录大数据量查询慢的常见原因优化方案高级方案配置调优监控与诊断总结大数据量查询慢的常见原因MyBAT

基于Go语言开发一个 IP 归属地查询接口工具

《基于Go语言开发一个IP归属地查询接口工具》在日常开发中,IP地址归属地查询是一个常见需求,本文将带大家使用Go语言快速开发一个IP归属地查询接口服务,有需要的小伙伴可以了解下... 目录功能目标技术栈项目结构核心代码(main.go)使用方法扩展功能总结在日常开发中,IP 地址归属地查询是一个常见需求:

MySQL之复合查询使用及说明

《MySQL之复合查询使用及说明》文章讲解了SQL复合查询中emp、dept、salgrade三张表的使用,涵盖多表连接、自连接、子查询(单行/多行/多列)及合并查询(UNION/UNIONALL)等... 目录复合查询基本查询回顾多表查询笛卡尔积自连接子查询单行子查询多行子查询多列子查询在from子句中使