MyBatis-Plus多表关联查询

MyBatis-Plus进行多表关联查询:使用MyBatis-Plus的SQL构建器 ( MPJLambdaWrapper )

还可以使用MyBatis-Plus的SQL构建器进行多表关联查询,例如:

下面详细举一个,联表查询产品和厂商的例子:

(1)引入相关依赖项

<!-- 引入mybatis-plus联表查询相关依赖项 -->
<!-- MVNW pom格式 -->
<dependency>
    <groupId>com.github.yulichang</groupId>
    <artifactId>mybatis-plus-join</artifactId>
    <version>1.2.4</version>
</dependency>
<!-- Gradle 格式 -->
implementation 'com.github.yulichang:mybatis-plus-join:1.2.4'

(2)相关实体类

/**
 * 产品实体
 */
@Data
@TableName(value = "t_product")
public class Product implements Serializable {
    private static final long serialVersionUID = 1L;
    /** 本表id */
    @TableId(value = "id",type = IdType.AUTO)
    private Integer id;
    /** 产品编码code */
    private String code;
    /** 产品名称 */
    private String name;
    /** 厂商编码code */
    private String factoryCode;
}

/**
 * 厂商实体
 */
@Data
@TableName(value = "t_factory")
public class Factory implements Serializable{
    private static final long serialVersionUID = 1L;
    /** 本表id */
    @TableId(value = "id",type = IdType.AUTO)
    private Integer id;
    /** 厂商编码code */
    private String code;
    /** 厂商名称 */
    private String name;
}
/**
 * 联表查询结果类
 */
@Data
public class ProductVO implements Serializable {
    /** 产品编码code */
    private String code;
    /** 产品名称 */
    private String name;
    /** 厂商编码code */
    private String factoryCode;
    /** 厂商名称 */
    private String factoryName;
}

(3)以产品为为主的Mapper层

特别注意集成 MPJBaseMapper,泛型为 Product 实体

/**
 * @Description: Mapper层
 * @Author: mc 2023/6/18 11:10
 */
@Repository
public interface ProductMapper extends MPJBaseMapper<Product> {

}

(4)业务逻辑实现层进行相关查询及逻辑处理

不做更多查询条件约束,数据库内产品表共10条数据,测试普通查询以及联表查询

注意联表查询时,selectJoinList()方法第一个参数为查询结果集映射的实体类,本处为ProductVO.class

/**
 * @Description: 产品相关业务逻辑实现层
 * @Author: mc 2023/6/18 11:14
 */
@Slf4j
@Service
public class ProductService {

    @Autowired
    private ProductMapper productMapper;

    public List<ProductVO> getProductInfo() {

        // 测试单表查询
        List<Product> one = productMapper.selectList(null);
        log.debug("单表查询,查询返回结果为:{}", one);

        // 测试联表查询
        List<ProductVO> two = productMapper.selectJoinList(ProductVO.class,
                new MPJLambdaWrapper<Product>()
                        .select(Product::getCode, Product::getName)
                        .selectAs(Factory::getCode, ProductVO::getFactoryCode)
                        .selectAs(Factory::getName, ProductVO::getFactoryName)
                        .leftJoin(Factory.class, Factory::getCode, Product::getFactoryCode)
        );
        log.debug("联表查询,查询返回结果为:{}", two);

        return null;
    }
}
// 其他方面
selectAll() // 可以查全部参数;
select()    // 查询字段,一个括号内仅能查一个实体,如需要查询多个表的字段,将有多个select();
selectAs()  // 相当于取别名,为了数据表内字段名称和结果集实体名称一致;
leftJoin()  //联结多个表,将有多个leftJoin(),方法3个参数:联入表实体,联入表关系字段,原表关系字段;

(5)查询结果

单表查询:

联表查询 :

  • 6
    点赞
  • 71
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
### 回答1: Mybatis-plus多表关联查询是指在使用Mybatis-plus框架进行数据库操作时,通过多个表之间的关联关系,实现一次性查询多个表中的数据。这种查询方式可以大大提高查询效率,减少数据库访问次数,提高系统性能。在Mybatis-plus中,可以使用注解或XML配置的方式实现多表关联查询,具体实现方式可以参考Mybatis-plus官方文档。 ### 回答2: MyBatis-Plus是基于MyBatis的增强工具,提供了很多实用的功能,其中包括了多表关联查询。在实际开发中,我们经常需要查询多个表的数据,此时就需要用到多表关联查询。下面介绍一下MyBatis-Plus多表关联查询的实现方法。 1.通过@TableName注解指定表名 在实体类上使用@TableName注解,可以指定当前实体类对应的表名,这样在进行多表关联查询时,就可以直接使用表名进行操作。示例代码如下: ``` @Data @TableName("t_user") public class User { @TableId(type = IdType.AUTO) private Long id; private String username; private String password; } ``` 2.使用Mapper中的方法进行多表关联查询 MyBatis-Plus提供了一些方法,可以进行多表关联查询。其中比较常用的方法是selectList()和selectPage()。这些方法可以通过Wrapper条件构造器指定查询条件,并且支持多表关联查询。示例代码如下: ``` public interface UserMapper extends BaseMapper<User> { @Select("select * from t_user u " + "left join t_role r on r.id = u.role_id " + "where u.id = #{id}") UserVO selectUserWithRole(Long id); @Select("select * from t_user u " + "left join t_role r on r.id = u.role_id " + "${ew.customSqlSegment}") List<UserVO> selectUserListWithRole(@Param(Constants.WRAPPER) Wrapper wrapper); } ``` 3.使用@Result注解映射查询结果 在使用Mapper中的方法进行多表关联查询时,我们可以使用@Result注解对查询结果进行映射,这样可以更方便地获取查询结果。示例代码如下: ``` public interface UserMapper extends BaseMapper<User> { @Select("select u.*, r.name as roleName from t_user u " + "left join t_role r on r.id = u.role_id " + "where u.id = #{id}") @Results({ @Result(column = "roleName", property = "roleName") }) UserVO selectUserWithRole(Long id); } ``` 总之,MyBatis-Plus多表关联查询使用起来非常方便,只需要在实体类中指定表名,使用Mapper中的方法进行查询,并使用@Result注解对查询结果进行映射即可。需要注意的是,在进行多表关联查询时,要确保关联的字段在两个表中是唯一的,并且要注意查询效率和查询结果的正确性。 ### 回答3: Mybatis-Plus是一个优秀的基于Mybatis的ORM框架,它极大地简化了开发人员的开发工作。在实际开发中,多表关联查询是常见的需求,Mybatis-Plus提供了多种方式支持多表关联查询。以下是几种常见的多表关联查询方式: 1.通过Join方式查询 使用Join语句可以在一个查询中查询多张表,从而实现多表关联查询,如下: ``` SELECT t1.*, t2.* FROM t1 INNER JOIN t2 ON t1.id = t2.t1_id ``` 如果需要使用Join方式查询,需要使用Mybatis-Plus的Wrapper类实现多表关联查询。 2.通过关联查询实现多表查询 如果需要实现包含关联实体的复杂查询,可以使用Mybatis-Plus提供的lambdaQuery方法。例如: ``` List<User> userList = userMapper.selectList(Wrappers .lambdaQuery(User.class) .eq(User::getStatus, 1) .nested(i -> i.eq(User::getName, "Tom") .or() .eq(User::getName, "Jerry")) .orderByAsc(User::getAge) .select(User::getId, User::getName, User::getAge, User::getEmail) .leftJoin(User.class, User::getId, UserRole::getUserId) .leftJoin(UserRole.class, UserRole::getRoleId, Role::getId) .select(UserRole::getRoleId, Role::getName) ) ``` 3.通过XML mapper方式查询 使用XML mapper文件可以实现多表关联查询。通过XML可以方便地实现复杂的多表查询。如下: ``` <select id="getUserRoleList" resultMap="BaseResultMap"> select ur.*, r.name as roleName from user_role ur left join role r on ur.role_id = r.id where ur.user_id = #{userId} </select> ``` 更多关于Mybatis-Plus多表关联查询,可以参考Mybatis-Plus官方文档或者相关博客文章。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值