Mybatis实现Join链接,最简单方式!

使用:

首先引入依赖

Maven

<dependency>
    <groupId>com.github.yulichang</groupId>
    <artifactId>mybatis-plus-join-boot-starter</artifactId>
    <version>1.4.11</version>
</dependency>

mapper继承MPJBaseMapper 

mapper继承MPJBaseMapper使用里面的selectJoinList方法

代码解析:

public List<DishVO> listWithFlavor(Long categoryId) {
    // 创建MPJLambdaWrapper对象,泛型里填的是与mapper同步的类型
    MPJLambdaWrapper<Dish> wrapper = new MPJLambdaWrapper<Dish>()
        // 开始构造参数,这里和sql语句一样,首先查询Dish表的所有字段
        .selectAll(Dish.class)
        // 查询Category的name字段并添加别名为CategoryName
        .selectAs(Category::getName, DishVO::getCategoryName)
        // 左连接查询,链接Category表,后面是绑定条件
        .leftJoin(Category.class, Category::getId, Dish::getCategoryId)
        // 这里就是查询条件
        .eq(Dish::getCategoryId, categoryId)
        .eq(Dish::getStatus, 1);

        调用selectJoinList方法连表查询
        List<DishVO> dishVOList = dishMapper.selectJoinList(DishVO.class, wrapper);

一对多,多对多

MPJLambdaWrapper<Dish> wrapper = new MPJLambdaWrapper<Dish>()
        .selectAll(Dish.class)
        .selectCollection(DishFlavor.class, DishVO::getFlavors)
        .leftJoin(DishFlavor.class, DishFlavor::getDishId, Dish::getId);

        List<DishVO> dtoList = userMapper.selectJoinList(DishVO.class, wrapper);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值