需求
能够根据接口请求的对象直接构造查询条件
实现
QueryField 注解
import java.lang.annotation.*;
@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface QueryField {
String value() default "";
QueryFieldOpType op() default QueryFieldOpType.EQ;
}
条件枚举
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
public enum QueryFieldOpType {
/**
* 等于
*/
EQ,
/**
* 左模糊
*/
LEFT_LIKE,
/**
* 右模糊
*/
RIGHT_LIKE,
/**
* 模糊匹配
*/
LIKE,
/**
* 大于
*/
GT,
/**
* 大于等于
*/
GE,
/**
* 小于
*/
LT,
/**
* 小于等于
*/
LE,
;
public <T> void wrapperQuery(String fieldName, Object value, QueryWrapper<T> queryWrapper) {
switch (this) {
case LEFT_LIKE:
queryWrapper.likeLeft(fieldName, value);
break;
case RIGHT_LIKE:
queryWrapper.likeRight(fieldName, value);
break;
case LIKE:
queryWrapper.like(fieldName, value);
break;
case GE:
queryWrapper.ge(fieldName, value);
break;
case GT:
queryWrapper.gt(fieldName, value);
break;
case LT:
queryWrapper.lt(fieldName, value);
break;
case LE:
queryWrapper.le(fieldName, value);
break;
case EQ:
default:
queryWrapper.eq(fieldName, value);
}
}
}
查询条件父类
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.time.LocalDateTime;
/**
* 所有条件查询的父类,可以使用toQueryWrapper()方法转成QueryWrapper对象,也可直接作为Mapper方法的参数
*/
public class QueryInfo {
/**
* 开始时间
*/
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime leCreateAt;
/**
* 结束时间
*/
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime geCreateAt;
/**
* 是否快速查询
*/
private boolean quick;
@SuppressWarnings("all")
public <T> QueryWrapper<T> toQueryWrapper() {
QueryWrapper<T> queryWrapper = new QueryWrapper<>();
Field[] fields = this.getClass().getDeclaredFields();
try {
for (Field field : fields) {
if ((!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass()
.getModifiers()) || Modifier.isFinal(field.getModifiers())) && !field.isAccessible()) {
field.setAccessible(true);
}
Object value = field.get(this);
if (value != null) {
QueryField[] annotations = field.getAnnotationsByType(QueryField.class);
QueryFieldOpType opType = QueryFieldOpType.EQ; // 默认按eq处理
String fieldName = field.getName(); // 默认按当前字段名处理
if (annotations.length > 0) {
// 判断是否有注解
opType = annotations[0].op();
if (StringUtils.isNotEmpty(annotations[0].value())) {
fieldName = annotations[0].value();
}
}
// 快速查询,条件查询结果相或
if (getQuick()) {
queryWrapper.or();
}
opType.wrapperQuery(fieldName, value, queryWrapper);
}
}
} catch (IllegalAccessException e) {
System.out.println("抛出自定义异常");
}
return queryWrapper;
}
public LocalDateTime getLeCreateAt() {
return leCreateAt;
}
public void setLeCreateAt(LocalDateTime leCreateAt) {
this.leCreateAt = leCreateAt;
}
public LocalDateTime getGeCreateAt() {
return geCreateAt;
}
public void setGeCreateAt(LocalDateTime geCreateAt) {
this.geCreateAt = geCreateAt;
}
public boolean getQuick() {
return quick;
}
public QueryInfo setQuick(boolean quick) {
this.quick = quick;
return this;
}
}
使用
import java.util.Date;
public class CaCertInfoRequest extends QueryInfo {
/**
* 证书通用名
*/
@QueryField(value = "entity_cn", op = QueryFieldOpType.EQ)
private String cn;
/**
* 证书状态
*/
@QueryField(value = "status", op = QueryFieldOpType.EQ)
private String status;
/**
* 证书生效时间
*/
@QueryField(value = "not_before", op = QueryFieldOpType.GE)
private Date notBefore;
/**
* 证书失效时间
*/
@QueryField(value = "not_after", op = QueryFieldOpType.LE)
private Date notAfter;
}