【java踩坑搞起】MybatisPlus封装的mapper不支持 join,那咋办

文章介绍了Mybatis-Plus-Join工具,它扩展了MybatisPlus,支持了join查询,使得在Java项目中进行多表查询更加方便。通过MPJLambdaWrapper,开发者可以以类似Mybatis-Plus的QueryWrapper方式编写联表查询,包括选择字段、设置别名、条件过滤等,还支持分页查询。文章提供了示例代码展示如何使用该工具进行三表查询和分页操作。

众所周知,Mybatis Plus 封装的 mapper 不支持 join,如果需要支持就必须自己去实现。但是对于大部分的业务场景来说,都需要多表 join,要不然就没必要采用关系型数据库了。

直到前几天,偶然碰到了这么一款叫做mybatis-plus-join的工具(后面就简称mpj了),使用了一下,不得不说真香!彻底将我从xml地狱中解放了出来,终于可以以类似mybatis-plusQueryWrapper的方式来进行联表查询了,话不多说,我们下面开始体验。

引入依赖

首先在项目中引入引入依赖坐标,因为mpj中依赖较高版本mybatis-plus中的一些api,所以项目建议直接使用高版本。

  • maven
<dependency>
    <groupId>com.github.yulichang</groupId>
    <artifactId>mybatis-plus-join</artifactId>
    <version>1.2.4</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.1</version>
</dependency>

引入相关依赖后,在springboot项目中,像往常一样正常配置数据源连接信息就可以了。

  • Gradle
implementation 'com.github.yulichang:mybatis-plus-join-boot-starter:1.4.4'

使用

  • mapper继承MPJBaseMapper (必选)
  • service继承MPJBaseService (可选)
  • serviceImpl继承MPJBaseServiceImpl (可选)

核心类MPJLambdaWrapper和MPJQueryWrapper

MPJLambdaWrapper用法

简单的三表查询
class test {
    @Resource
    private UserMapper userMapper;

    void testJoin() {
        //和Mybatis plus一致,MPJLambdaWrapper的泛型必须是主表的泛型,并且要用主表的Mapper来调用
        MPJLambdaWrapper<UserDO> wrapper = new MPJLambdaWrapper<UserDO>()
                .selectAll(UserDO.class)//查询user表全部字段
                .select(UserAddressDO::getTel)//查询user_address tel 字段
                .selectAs(UserAddressDO::getAddress, UserDTO::getUserAddress)//别名 t.address AS userAddress
                .select(AreaDO::getProvince, AreaDO::getCity)
                .leftJoin(UserAddressDO.class, UserAddressDO::getUserId, UserDO::getId)
                .leftJoin(AreaDO.class, AreaDO::getId, UserAddressDO::getAreaId)
                .eq(UserDO::getId, 1)
                .like(UserAddressDO::getTel, "1")
                .gt(UserDO::getId, 5);

        //连表查询 返回自定义ResultType
        List<UserDTO> list = userMapper.selectJoinList(UserDTO.class, wrapper);

        //分页查询 (需要启用 mybatis plus 分页插件)
        Page<UserDTO> listPage = userMapper.selectJoinPage(new Page<>(2, 10), UserDTO.class, wrapper);
    }
}

对应sql

SELECT  
    t.id, t.name, t.sex, t.head_img, 
    t1.tel, t1.address AS userAddress,
    t2.province, t2.city 
FROM 
    user t 
    LEFT JOIN user_address t1 ON t1.user_id = t.id 
    LEFT JOIN area t2 ON t2.id = t1.area_id 
WHERE (
    t.id = ? 
    AND t1.tel LIKE ? 
    AND t.id > ?)

说明:

  • UserDTO.class 查询结果返回类(resultType)
  • selectAll() 查询指定实体类的全部字段
  • select() 查询指定的字段,支持可变参数,同一个select只能查询相同表的字段
  • selectAs() 字段别名查询,用于数据库字段与业务实体类属性名不一致时使用
  • leftJoin() 参数说明;第一个参数: 参与连表的实体类class 第二个参数: 连表的ON字段,这个属性必须是第一个参数实体类的属性 第三个参数: 参与连表的ON的另一个实体类属性
  • 默认主表别名是t,其他的表别名以先后调用的顺序使用t1,t2,t3…
  • 条件查询,可以查询主表以及参与连接的所有表的字段,全部调用mp原生的方法,正常使用没有sql注入风险

MPJLambdaWrapper 还有很多其他的功能

  • 简单的SQL函数使用:https://gitee.com/best_handsome/mybatis-plus-join/wikis/selectFunc()?sort_id=4082479
  • ON语句多条件支持:https://gitee.com/best_handsome/mybatis-plus-join/wikis/leftJoin?sort_id=3496671

等效于ResultMap

<resultMap id="xxxxxxxx" type="com.github.yulichang.join.dto.UserDTO">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <!--其他属性省略-->
    <collection property="addressList" javaType="java.util.List"
                ofType="com.github.yulichang.join.entity.UserAddressDO">
        <id property="id" column="mpj_id"/>
        <result property="address" column="address"/>
        <result property="userId" column="user_id"/>
        <!--其他属性省略-->
    </collection>
</resultMap>

MPJLambdaWrapper其他功能

  • 一对一,一对多使用:https://ylctmh.com/pages/core/lambda/select/selectCollection.html
  • 简单的SQL函数使用:https://ylctmh.com/pages/core/lambda/select/selectFunc.html
  • ON语句多条件支持:https://ylctmh.com/pages/core/lambda/join/leftJoin.html

分页查询

mpj中也能很好的支持列表查询中的分页功能,首先我们要在项目中加入分页拦截器:

@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
    return interceptor;
}

接下来改造上面的代码,调用selectJoinPage()方法:

public void page() {
    IPage<OrderDto> orderPage = orderMapper.selectJoinPage(
      new Page<OrderDto>(2,10),
      OrderDto.class,
      new MPJLambdaWrapper<Order>()
        .selectAll(Order.class)
        .select(Product::getUnitPrice)
        .selectAs(User::getName, OrderDto::getUserName)
        .selectAs(Product::getName, OrderDto::getProductName)
        .leftJoin(User.class, User::getId, Order::getUserId)
        .leftJoin(Product.class, Product::getId, Order::getProductId)
        .orderByAsc(Order::getId));

    orderPage.getRecords().forEach(System.out::println);
}

注意在这里需要添加一个分页参数的Page对象,我们再执行上面的代码,并对日志进行解析,查看sql语句:

图片

可以看到底层通过添加limit进行了分页,同理,MPJQueryWrapper也可以这样进行分页。

最后

经过简单的测试,个人感觉mpj这款工具在联表查询方面还是比较实用的,能更应对项目中不是非常复杂的场景下的sql查询,大大提高我们的生产效率。当然,在项目的issues中也能看到当前版本中也仍然存在一些问题,希望在后续版本迭代中能继续完善。

关注我,后续更新更多好用的工具,效率天花板

### MyBatis PlusMapper 接口的配置 在 MyBatis Plus 框架中,`Mapper` 接口通过继承 `BaseMapper<T>` 来获得基本 CRUD 功能。这使得开发者无需手动编写 SQL 语句即可完成常见的数据库操作[^1]。 #### 配置 Mapper 接口 为了使自定义的 `Mapper` 接口能够正常工作并利用 MyBatis Plus 提供的功能,通常需要遵循以下几点: - **引入依赖** 确保项目的构建工具(如 Maven 或 Gradle)已经包含了 MyBatis Plus 的相关依赖项。对于 Maven 用户来说,在 `pom.xml` 文件内添加如下片段可以满足需求: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis.plus.version}</version> </dependency> ``` - **创建实体类** 按照 Java 类与 MySQL 数据库表之间的映射关系来设计实体类。例如,如果有一个名为 `student` 的数据表,则应存在相应的 `Student` 实体类用于表示该表格结构及其字段: ```java package com.example.entity; import lombok.Data; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableName; @Data @TableName("student") public class Student { @TableField(value = "id", exist = true) private Long id; @TableField(value = "name", exist = true) private String name; } ``` - **定义 Mapper 接口** 让特定业务逻辑对应的 DAO 层接口继承通用的基础接口 `BaseMapper<T>` 即可自动获取一系列预设的操作方法。此外还可以在此基础上进一步声明额外的方法以适应更复杂的应用场景[^2]: ```java package com.szh.mybatisplus.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.szh.mybatisplus.entity.Student; import org.apache.ibatis.annotations.Param; import java.util.List; public interface StudentMapper extends BaseMapper<Student> { // 自定义插入学生记录的方法 int insertStudent(Student student); // 根据ID查询单个学生的详情 Student selectStudentById(@Param("id") Integer id); // 查询所有学生列表 List<Student> selectAll(); } ``` - **注册扫描路径** 为了让 Spring Boot 能够识别到这些新创建的数据访问层组件,可以在启动类上标注 `@MapperScan` 注解指定包名以便于自动装配所有的 `Mapper` 接口实例: ```java @SpringBootApplication @MapperScan(basePackages = {"com.szh.mybatisplus.mapper"}) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 以上就是关于如何在 MyBatis Plus 中正确配置 `Mapper` 接口及其实现的主要内容介绍。通过这种方式仅可以简化开发流程而且提高了代码复用性和维护效率。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

毕创工社

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值