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

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐