bobo667 / mybatis-plus-join

mybatis-plus-join 多表插件 完全按照mp的用法来做,你只要会mp,那你就会用mpj,无感引入,不会对之前的业务产生一点点影响,采用扩展方式来集成mp,更容易适配版本,后期mp版本升级,您不用更改东西,只需要更新版本就行,自定义返回Vo,自定义表别名,自定义查询字段,骚操作爽到爆,让你在喝有时间喝下午茶的时间,还能再吃点心(目前支持大部分主流版本 )
Apache License 2.0
135 stars 14 forks source link

关联主表条件错误:多张表自连接无法正确拼出sql #8

Open changyuansky opened 2 months ago

changyuansky commented 2 months ago

在commitid为bc76f750的提交中,已经存在过对此问题的修复,但是存在局限性。 企业微信截图_e82f1d07-ac90-42af-9de3-d8174f142e7c

局限性为:仅解决了整个查询只有自连接的情况。 举例:(这个sql是可以被正确拼接的)

SELECT
    c.* 
FROM
    cl_category c
    LEFT JOIN cl_category c2 ON c2.pid = c.id

以下这种情况的sql无法被正确拼接: 期望:

SELECT
    goods.*,c.*
FROM
    goods goods
    LEFT JOIN category c ON c.id = goods.cid
    LEFT JOIN category c2 ON c2.id = c.pid
    LEFT JOIN category c3 ON c3.id = c2.cid
    LEFT JOIN category c4 ON c4.id = c3.pid

代码:

    JoinLambdaWrapper<Goods> queryWrapper = new JoinLambdaWrapper<>(ClGoods.class, "goods").selectAll()
                .leftJoin(Category.class, Category::getId, Goods::getCid, "c").selectAll().end()
                .leftJoin(Category.class, Category::getId, Category::getPid, "c2").end()
                .leftJoin(Category.class, Category::getId, Category::getPid, "c3").end()
                .leftJoin(Category.class, Category::getId, Category::getPid, "c4").end();

拼出来的sql为:

SELECT
    goods.*,c.*
FROM
    goods goods
    LEFT JOIN category c ON c.id = goods.cid
    LEFT JOIN category c2 ON c2.id = c2.pid
    LEFT JOIN category c3 ON c3.id = c3.pid
    LEFT JOIN category c4 ON c4.id = c4.pid
changyuansky commented 2 months ago

PR: https://github.com/bobo667/mybatis-plus-join/pull/9 @bobo667

changyuansky commented 2 months ago

解决方案:增加joinMasterAlias参数,让调用方可以指定要关联的主表是哪一个。 image image image image

changyuansky commented 2 months ago

使用方法:

        JoinLambdaWrapper<Goods> queryWrapper = new JoinLambdaWrapper<>(Goods.class, "goods").selectAll()
                .leftJoin(Category.class, Category::getId, Goods::getCid, "c").selectAll().end()
                .leftJoin(Category.class, Category::getId, Category::getPid, "c2", "c").end()
                .leftJoin(Category.class, Category::getId, Category::getPid, "c3", "c2").end()
                .leftJoin(Category.class, Category::getId, Category::getPid, "c4", "c3").end();