自定义注解
import java.lang.annotation.*;
/**
* @Author:chenyanbin
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MybatisPlusPageAnnotation {
/**
* 对应数据库字段名称
*
* @return
*/
String value() default "";
/**
* 对应数据库字段描述
*
* @return
*/
String description() default "";
enum CompareRule {
eq, //等于 =
like, //LIKE '%值%'
likeLeft, //LIKE '%值'
likeRight, //LIKE '值%'
gt, //大于
lt, //小于
ge, //大于等于
le //小于等于
}
enum IfRule {
isNotBlank,
isBlank,
isEmpty
}
/**
* 比较规则
* <pre>
* LIKE '%值%'
* </pre>
*
* @return
*/
CompareRule compareRule() default CompareRule.eq;
/**
* 判断规则
* <pre>
* if (StringUtils.isNotBlank(policyNo)) {
*
* }
* </pre>
* <pre>
* StringUtils.isNotBlank(null) = false
* StringUtils.isNotBlank("") = false
* StringUtils.isNotBlank(" ") = false
* StringUtils.isNotBlank("bob") = true
* StringUtils.isNotBlank(" bob ") = true
* </pre>
* <pre>
* StringUtils.isBlank(null) = true
* StringUtils.isBlank("") = true
* StringUtils.isBlank(" ") = true
* StringUtils.isBlank("bob") = false
* StringUtils.isBlank(" bob ") = false
* </pre>
* <pre>
* StringUtils.isEmpty(null) = true
* StringUtils.isEmpty("") = true
* StringUtils.isEmpty(" ") = false
* StringUtils.isEmpty("bob") = false
* StringUtils.isEmpty(" bob ") = false
* </pre>
*
* @return
*/
IfRule ifRule() default IfRule.isNotBlank;
}
工具类
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.isoftstone.insurance.lixiang.api.vas.annotation.MybatisPlusPageAnnotation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.Field;
/**
* @Author:chenyanbin
*/
@Slf4j
public class MybatisPlusPageUtils {
public static void buildQueryWrapper(QueryWrapper queryWrapper, Object data) {
try {
Field[] fields = data.getClass().getDeclaredFields();
for (Field f : fields) {
MybatisPlusPageAnnotation annotation = f.getAnnotation(MybatisPlusPageAnnotation.class);
if (annotation == null) {
continue;
}
f.setAccessible(true);
String objValue = String.valueOf(f.get(data));
if ("null".equalsIgnoreCase(objValue)) {
objValue = "";
}
MybatisPlusPageAnnotation.IfRule ifRule = annotation.ifRule();
MybatisPlusPageAnnotation.CompareRule compareRule = annotation.compareRule();
String fieldValue = annotation.value();
switch (ifRule) {
case isNotBlank:
if (StringUtils.isNotBlank(objValue)) {
handlerValues(queryWrapper, annotation, objValue, fieldValue);
}
break;
case isBlank:
if (StringUtils.isBlank(objValue)) {
handlerValues(queryWrapper, annotation, objValue, fieldValue);
}
break;
case isEmpty:
if (StringUtils.isEmpty(objValue)) {
handlerValues(queryWrapper, annotation, objValue, fieldValue);
}
break;
default:
break;
}
f.setAccessible(false);
}
} catch (Exception e) {
log.error("MybatisPlusPageUtils buildqueryWrapper queryWrapper={},error={}", queryWrapper, e.getMessage());
}
}
private static void handlerValues(QueryWrapper queryWrapper, MybatisPlusPageAnnotation annotation, String objValue, String fieldValue) {
switch (annotation.compareRule()) {
case eq:
queryWrapper.eq(fieldValue, objValue);
break;
case like:
queryWrapper.like(fieldValue, objValue);
break;
case likeLeft:
queryWrapper.likeLeft(fieldValue, objValue);
break;
case likeRight:
queryWrapper.likeRight(fieldValue, objValue);
break;
case gt:
queryWrapper.gt(fieldValue, objValue);
break;
case lt:
queryWrapper.lt(fieldValue, objValue);
break;
case ge:
queryWrapper.ge(fieldValue, objValue);
break;
case le:
queryWrapper.le(fieldValue, objValue);
break;
default:
break;
}
}
}
request类
import lombok.Data;
/**
* @Author:chenyanbin
*/
@Data
public class OrderPageRequest {
@MybatisPlusPageAnnotation(value = "order_code", description = "订单号", compareRule = MybatisPlusPageAnnotation.CompareRule.like)
private String orderCode;
@MybatisPlusPageAnnotation(value = "order_source", description = "订单来源", compareRule = MybatisPlusPageAnnotation.CompareRule.like)
private String orderSource;
@MybatisPlusPageAnnotation(value = "order_status", description = "订单状态")
private String orderStatus;
@MybatisPlusPageAnnotation(value = "policy_no", description = "保单号", compareRule = MybatisPlusPageAnnotation.CompareRule.like)
private String policyNo;
@MybatisPlusPageAnnotation(value = "product_name", description = "商品名称", compareRule = MybatisPlusPageAnnotation.CompareRule.like)
private String productName;
@MybatisPlusPageAnnotation(value = "item_name", description = "服务项目名称", compareRule = MybatisPlusPageAnnotation.CompareRule.like)
private String itemName;
@MybatisPlusPageAnnotation(value = "appoint_name", description = "预约人姓名", compareRule = MybatisPlusPageAnnotation.CompareRule.like)
private String appointName;
@MybatisPlusPageAnnotation(value = "appoint_certi_code", description = "预约人证件号码")
private String appointCertiCode;
@MybatisPlusPageAnnotation(value = "use_name", description = "使用人姓名", compareRule = MybatisPlusPageAnnotation.CompareRule.like)
private String useName;
@MybatisPlusPageAnnotation(value = "use_certi_code", description = "使用人证件号码")
private String useCertiCode;
@MybatisPlusPageAnnotation(value = "created_date", description = "创建时间-开始", compareRule = MybatisPlusPageAnnotation.CompareRule.ge)
private String start;
@MybatisPlusPageAnnotation(value = "created_date", description = "创建时间-结束", compareRule = MybatisPlusPageAnnotation.CompareRule.le)
private String end;
private int size;
private int current;
}
使用
QueryWrapper<VasAppointment> wrapper = new QueryWrapper<VasAppointment>();
OrderPageRequest request = new OrderPageRequest();
request.setOrderCode(orderCode);
request.setOrderSource(orderSource);
request.setOrderStatus(orderStatus);
request.setPolicyNo(policyNo);
request.setProductName(productName);
request.setItemName(itemName);
request.setAppointName(appointName);
request.setAppointCertiCode(appointCertiCode);
request.setUseName(useName);
request.setUseCertiCode(useCertiCode);
request.setStart(start);
request.setEnd(end);
request.setSize(pageReq.getSize());
request.setCurrent(pageReq.getCurrent());
MybatisPlusPageUtils.buildQueryWrapper(wrapper, request);
Page<VasAppointment> pageInfo = new Page<>(pageReq.getCurrent(), pageReq.getSize());
Page<VasAppointment> sourcePage = appointmentMapper.selectPage(pageInfo, wrapper);