1、引包
<dependency>
<groupId>com.github.yulichang</groupId>
<artifactId>mybatis-plus-join-boot-starter</artifactId>
<version>1.4.10</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
2、mapper处理:把mybaits plus的BaseMapper 改成MPJBaseMapper
@Mapper
public interface ScaleMapper extends MPJBaseMapper<Scale> {
}
3、样例: 使用 MPJLambdaWrapper
/**
* 查询扩缩容
* @param resourceType
* @param status
* @param pageSize
* @param pageNum
* @return
*/
public IPage<Scale> getScaleResource(String resourceType, Integer status, Date startTime, Date endTime, Long pageSize, Long pageNum){
log.info("查询扩缩容,resourceType:{}, status:{}, startTime:{}, endTime:{}, pageSize:{}, pageNum:{}", resourceType, status, startTime, endTime, pageSize, pageNum);
MPJLambdaWrapper<Scale> queryWrapper = new MPJLambdaWrapper<>();
Page<Scale> page = new Page<>(pageNum, pageSize);
if(Objects.nonNull(resourceType)){
queryWrapper.eq(Scale::getResourceType, resourceType);
}
if(Objects.nonNull(status)){
queryWrapper.eq(Scale::getStatus, status);
}
if(Objects.nonNull(startTime)){
queryWrapper.ge(Scale::getCreatedAt, startTime);
}
if(Objects.nonNull(endTime)){
queryWrapper.le(Scale::getCreatedAt, endTime);
}
queryWrapper.leftJoin(Strategy.class, Strategy::getId, Scale::getStrategyId);
queryWrapper.selectAll(Scale.class);
queryWrapper.selectAs(Strategy::getName, Scale::getStrategyName);
queryWrapper.orderByDesc(Scale::getCreatedAt);
IPage<Scale> result = scaleMapper.selectPage(page, queryWrapper);
log.info("查询扩缩容结果:{}", JSON.toJSON(result));
return result;
}
样例生成的sql:
SELECT
t.id,
t.resource_id,
t.resource_name,
t.resource_type,
t.pod_id,
t.strategy_id,
t.contents,
t.STATUS,
t.created_at,
t.updated_at,
t.business_id,
t.business_name,
t.ip,
t1.NAME AS strategyName
FROM
comp_sta_scale_t t
LEFT JOIN comp_sta_strategy_t t1 ON ( t1.id = t.strategy_id )
ORDER BY
t.created_at DESC
4、github网址及mybatis-plus-join网址
https://github.com/yulichang/mybatis-plus-join
https://mybatisplusjoin.com/pages/quickstart/js.html