refactor: 优化通用查询注解多字段模糊查询
This commit is contained in:
parent
36fda57d49
commit
375810772a
@ -43,10 +43,10 @@ public @interface Query {
|
||||
QueryTypeEnum type() default QueryTypeEnum.EQUAL;
|
||||
|
||||
/**
|
||||
* 多属性模糊查询,仅支持 String 类型属性,多个属性之间用逗号分隔
|
||||
* 多属性模糊查询,仅支持 String 类型属性
|
||||
* <p>
|
||||
* 例如:@Query(blurry = "username,email") 表示根据用户名和邮箱模糊查询
|
||||
* 例如:@Query(blurry = {"username", "email"}) 表示根据用户名和邮箱模糊查询
|
||||
* </p>
|
||||
*/
|
||||
String blurry() default "";
|
||||
String[] blurry() default {};
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
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;
|
||||
|
||||
@ -131,12 +132,11 @@ public class QueryHelper {
|
||||
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;
|
||||
|
@ -40,7 +40,7 @@ public class RoleQuery implements Serializable {
|
||||
* 角色名称
|
||||
*/
|
||||
@Schema(description = "角色名称", example = "测试人员")
|
||||
@Query(blurry = "name,code")
|
||||
@Query(blurry = {"name", "code"})
|
||||
private String name;
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user