使用:
首先引入依赖
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);