MyBatis‑Plus 神兵利器:CommonServiceUtils 深度揭秘

在使用 MyBatis‑Plus 的过程中,我们常被各种 Wrapper 构造、批量 IN 查询、动态条件拼装,以及实体↔DTO 属性复制等“样板”操作所困扰。CommonServiceUtils 应运而生——它集成了通用查询、动态条件拼装、列表与对象转换、Bean 属性复制等功能,让你只需一行静态方法调用,就能轻松搞定各种数据访问场景。

 

方法介绍

  1. findByFieldInTargetField

从源集合中提取字段值并做批量 IN 查询。

常规写法:

// 先提取所有 userId
List<Long> userIds = orders.stream()
    .map(Order::getUserId)
    .collect(Collectors.toList());
// 再构建 LambdaQueryWrapper
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
wrapper.in(User::getId, userIds);
// 执行查询
List<User> users = userService.list(wrapper);

使用 CommonServiceUtils:

List<User> users = CommonServiceUtils.findByFieldInTargetField(
    orderList, userService,
    Order::getUserId, User::getId
);

  2. buildQueryWrapperByField

根据单个字段和值快速构建 LambdaQueryWrapper。

常规写法:

// 构建查询条件
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(User::getStatus, "ACTIVE");
// 执行查询
List<User> activeUsers = userService.list(wrapper);

 使用 CommonServiceUtils:

LambdaQueryWrapper<User> wrapper =
  CommonServiceUtils.buildQueryWrapperByField(User::getStatus, "ACTIVE");
List<User> activeUsers = userService.list(wrapper);

  3. findByFieldEqTargetField

根据单字段和值直接查询实体列表。

常规写法:

LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Product::getCategory, "BOOK");
List<Product> books = productService.list(wrapper);

使用 CommonServiceUtils:

List<Product> books = CommonServiceUtils.findByFieldEqTargetField(
    Product::getCategory, "BOOK", productService
);

  4. findByFieldEqTargetFields

根据多个字段和值动态构建查询并执行。

常规写法:

LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(User::getGender, "M")
       .eq(User::getAge, 30);
List<User> list = userService.list(wrapper);

使用 CommonServiceUtils:

Map<SFunction<User,?>, Object> cond = Map.of(
  User::getGender, "M",
  User::getAge,    30
);
List<User> list = CommonServiceUtils.findByFieldEqTargetFields(cond, userService);

  5. convertList

将一个类型的列表转换为另一个类型(实体 ↔ DTO)。

常规写法:

List<UserDto> dtos = userList.stream()
    .map(user -> {
       UserDto dto = new UserDto();
       BeanUtils.copyProperties(user, dto);
       return dto;
    })
    .collect(Collectors.toList());

使用 CommonServiceUtils:

List<UserDto> dtos = CommonServiceUtils.convertList(userList, UserDto.class);

  6. copyProperties

复制单个对象属性并返回新实例。

常规写法:

UserDto dto = new UserDto();
BeanUtils.copyProperties(userEntity, dto);

使用 CommonServiceUtils:

UserDto dto = CommonServiceUtils.copyProperties(userEntity, UserDto.class);

 

使用说明

1. 修改maven的配置文件

因为我把 custom-toolkit 发布到了 JitPack(而非中央仓库),所以在使用阿里云公共镜像时必须排除 JitPack,否则 Maven 会被镜像拦截、无法拉取自定义依赖。因此在你的 conf/settings.xml 镜像配置中将<mirrorOf>*</mirrorOf>修改为<mirrorOf>*,!jitpack.io</mirrorOf>

<mirrors>
  <!-- 阿里云公共仓库(排除 jitpack.io,否则 custom-toolkit 无法获取) -->
  <mirror>
    <id>aliyunmaven</id>
    <mirrorOf>*,!jitpack.io</mirrorOf>
    <name>阿里云公共仓库</name>
    <url>https://maven.aliyun.com/repository/public</url>
  </mirror>
</mirrors>

2. 在你的 pom.xml 中添加 JitPack 仓库,与dependencies同级

<repositories>
  <repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
  </repository>
</repositories>

 3. 引入 custom-toolkit 依赖

<dependency>
    <groupId>com.github.code-cxz</groupId>
    <artifactId>custom-toolkit</artifactId>
    <version>1.0.2</version>
</dependency>

 引入上述依赖之后,即可在项目中正常使用 CommonServiceUtils 的所有静态方法

 

分割线


2025.07.24 回来更新了,今天成功将Jar包发布到 Maven 中央仓库,不需要再使用JitPack仓库以及修改Maven的配置文件

直接引入这个依赖就可以正常使用了

<dependency>
    <groupId>io.github.code-cxz</groupId>
    <artifactId>user-defined</artifactId>
    <version>1.0.0</version>
</dependency>

 

GitHub 源码地址:https://github.com/code-cxz/custom-toolkit.git

posted @ 2025-07-23 18:07  chenxinzhi  阅读(174)  评论(0)    收藏  举报