本文主要是介绍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多表连表查询(包含分页)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!