通过mybatis-plus-join 实现mybatis plus联表查询

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值