# 自连接

以父子关系为例, 查询子User的name到父User的createName中

注意

自连接 需要 mybatis-plus-join 版本大于等于 1.4.0

# MPJQueryWrapper

自定义别名即可

List<UserDO> dtos = userMapper.selectJoinList(UserDO.class, new MPJQueryWrapper<UserDO>()
        .selectAll(UserDO.class)
        .select("u.`name` as createName")
        .leftJoin("`user` u on u.pid = t.id")
        .lt("t.id ",5));

# MPJLambdaWrapper

MPJLambdaWrapper同样可以这么写

List<UserDO> dtos = userMapper.selectJoinList(UserDO.class, new MPJLambdaWrapper<UserDO>()
        .selectAll(UserDO.class)
        .select("u.`name` as createName")
        .leftJoin("`user` u on u.pid = t.id"));

也可以lambda + String实现

List<UserDO> dtos1 = userMapper.selectJoinList(UserDO.class, new MPJLambdaWrapper<UserDO>()
        .selectAll(UserDO.class)
        //这个select 和 后面的两个selectAs等效
        .select("u.`name` as createName")
        .selectAs("u.`name`", UserDO::getCreateName)
        .selectAs("u", UserDO::getName, UserDO::getCreateName)
        //这里容易混淆, on语句两个参数都是UserDO, 第一个为副表条件, 第二个为主表条件, 不要弄混了
        .leftJoin(UserDO.class, "u", UserDO::getPid, UserDO::getId));

也可以纯lambda实现

List<UserDO> dtos = userMapper.selectJoinList(UserDO.class, new MPJLambdaWrapper<UserDO>()
        .selectAll(UserDO.class)
        //这里容易混淆, on语句两个参数都是UserDO, 第一个为副表条件, 第二个为主表条件, 不要弄混了
        .leftJoin(UserDO.class, UserDO::getPid, UserDO::getId, ext->ext
                .selectAs(UserDO::getName,UserDO::getCreateName))
        //查询 t1.`name` AS createTime
        .lt(UserDO::getId,5));