Skip to content

Commit

Permalink
refactor: 优化通用查询注解多字段模糊查询
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Sep 12, 2023
1 parent 36fda57 commit 3758107
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
QueryTypeEnum type() default QueryTypeEnum.EQUAL;

/**
* 多属性模糊查询,仅支持 String 类型属性,多个属性之间用逗号分隔
* 多属性模糊查询,仅支持 String 类型属性
* <p>
* 例如:@Query(blurry = "username,email") 表示根据用户名和邮箱模糊查询
* 例如:@Query(blurry = {"username", "email"}) 表示根据用户名和邮箱模糊查询
* </p>
*/
String blurry() default "";
String[] blurry() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

Expand Down Expand Up @@ -131,12 +132,11 @@ private static <R> void parse(Query queryAnnotation, String fieldName, Object fi
QueryWrapper<R> queryWrapper) {
// 解析多属性模糊查询
// 如果设置了多属性模糊查询,分割属性进行条件拼接
String blurry = queryAnnotation.blurry();
if (StrUtil.isNotBlank(blurry)) {
String[] propertyArr = blurry.split(",");
String[] blurryPropertyArr = queryAnnotation.blurry();
if (ArrayUtil.isNotEmpty(blurryPropertyArr)) {
queryWrapper.and(wrapper -> {
for (String property : propertyArr) {
wrapper.or().like(StrUtil.toUnderlineCase(property), fieldValue);
for (String blurryProperty : blurryPropertyArr) {
wrapper.or().like(StrUtil.toUnderlineCase(blurryProperty), fieldValue);
}
});
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class RoleQuery implements Serializable {
* 角色名称
*/
@Schema(description = "角色名称", example = "测试人员")
@Query(blurry = "name,code")
@Query(blurry = {"name", "code"})
private String name;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class UserQuery implements Serializable {
* 用户名
*/
@Schema(description = "用户名", example = "zhangsan")
@Query(blurry = "username,nickname,email,phone")
@Query(blurry = {"username", "nickname", "email", "phone"})
private String username;

/**
Expand Down

0 comments on commit 3758107

Please sign in to comment.