|
|
@@ -17,6 +17,7 @@ import com.baomidou.mybatisplus.annotation.TableField;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
import com.github.yulichang.wrapper.MPJLambdaWrapper;
|
|
|
+
|
|
|
/**
|
|
|
* @Description 拼接查询条件工具类
|
|
|
*/
|
|
|
@@ -66,7 +67,7 @@ public class QueryLambdaUtil {
|
|
|
for (Field field : reflectForField(clazz)) {
|
|
|
TableField tableField = AnnotationUtils.getAnnotation(field, TableField.class);
|
|
|
// 字段没有TableField这个注解和有这个主机上false的
|
|
|
- // 这里有可能没有注解,是使用了骆驼ming'm
|
|
|
+
|
|
|
if (tableField != null && !tableField.exist()) {
|
|
|
continue;
|
|
|
}
|
|
|
@@ -161,31 +162,144 @@ public class QueryLambdaUtil {
|
|
|
queryWrapper.orderByDesc(fieldName);
|
|
|
break;
|
|
|
case AND:
|
|
|
+ // 支持通过attribute指定多字段AND连接(格式:{"field1", "field2"})
|
|
|
+ String[] andFields = queryWapperAnnotation.attribute();
|
|
|
+ if (ArrayUtils.isNotEmpty(andFields)) {
|
|
|
+ for (String andField : andFields) {
|
|
|
+ // 反射获取关联字段的值
|
|
|
+ Method andMethod = getMethod(clazz, andField);
|
|
|
+ if (andMethod != null) {
|
|
|
+ Object andValue = andMethod.invoke(obj);
|
|
|
+ if (!ObjectUtils.isEmpty(andValue)) {
|
|
|
+ // 转换为数据库字段名
|
|
|
+ String dbAndField = getDbFieldName(clazz, andField, tableField);
|
|
|
+ queryWrapper.eq(getSFunction(clazz, dbAndField), andValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
break;
|
|
|
+
|
|
|
case BETWEEN:
|
|
|
+ // 通过attribute指定开始/结束字段(格式:{"startField", "endField"})
|
|
|
+ String[] betweenFields = queryWapperAnnotation.attribute();
|
|
|
+ if (betweenFields.length == 2) {
|
|
|
+ // 获取开始值
|
|
|
+ Method startMethod = getMethod(clazz, betweenFields[0]);
|
|
|
+ Object startVal = startMethod != null ? startMethod.invoke(obj) : null;
|
|
|
+ // 获取结束值
|
|
|
+ Method endMethod = getMethod(clazz, betweenFields[1]);
|
|
|
+ Object endVal = endMethod != null ? endMethod.invoke(obj) : null;
|
|
|
+ // 两个值都存在时才添加BETWEEN条件
|
|
|
+ if (!ObjectUtils.isEmpty(startVal) && !ObjectUtils.isEmpty(endVal)) {
|
|
|
+ queryWrapper.between(getSFunction(clazz, fieldName), startVal, endVal);
|
|
|
+ }
|
|
|
+ }
|
|
|
break;
|
|
|
- case EXISTS:
|
|
|
- break;
|
|
|
+
|
|
|
case GROUP_BY:
|
|
|
+ // 通过attribute指定分组字段(格式:{"field1", "field2"})
|
|
|
+ String[] groupFields = queryWapperAnnotation.attribute();
|
|
|
+ if (ArrayUtils.isNotEmpty(groupFields)) {
|
|
|
+ for (String groupField : groupFields) {
|
|
|
+ String dbGroupField = getDbFieldName(clazz, groupField, tableField);
|
|
|
+ queryWrapper.groupBy(getSFunction(clazz, dbGroupField));
|
|
|
+ }
|
|
|
+ }
|
|
|
break;
|
|
|
+
|
|
|
case HAVING:
|
|
|
+ // 通过attribute指定HAVING条件(格式:{"SUM(score) > 90"})
|
|
|
+ String[] havingConditions = queryWapperAnnotation.attribute();
|
|
|
+ if (ArrayUtils.isNotEmpty(havingConditions)) {
|
|
|
+ queryWrapper.having(havingConditions[0]);
|
|
|
+ }
|
|
|
break;
|
|
|
+
|
|
|
case IS_NOT_NULL:
|
|
|
+ // 字段非空判断(无需值,直接添加条件)
|
|
|
+ queryWrapper.isNotNull(getSFunction(clazz, fieldName));
|
|
|
break;
|
|
|
+
|
|
|
case IS_NULL:
|
|
|
+ // 字段为空判断(无需值,直接添加条件)
|
|
|
+ queryWrapper.isNull(getSFunction(clazz, fieldName));
|
|
|
break;
|
|
|
+
|
|
|
case NOT:
|
|
|
+ // 对后续条件取反(结合attribute指定的字段)
|
|
|
+ String[] notFields = queryWapperAnnotation.attribute();
|
|
|
+ if (ArrayUtils.isNotEmpty(notFields)) {
|
|
|
+ String notField = notFields[0];
|
|
|
+ Method notMethod = getMethod(clazz, notField);
|
|
|
+ if (notMethod != null) {
|
|
|
+ Object notValue = notMethod.invoke(obj);
|
|
|
+ if (!ObjectUtils.isEmpty(notValue)) {
|
|
|
+ String dbNotField = getDbFieldName(clazz, notField, tableField);
|
|
|
+ queryWrapper.not(i -> {
|
|
|
+ try {
|
|
|
+ i.eq(getSFunction(clazz, dbNotField), notValue);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
break;
|
|
|
+
|
|
|
case NOT_BETWEEN:
|
|
|
+ // 同BETWEEN,取反逻辑
|
|
|
+ String[] notBetweenFields = queryWapperAnnotation.attribute();
|
|
|
+ if (notBetweenFields.length == 2) {
|
|
|
+ Method notStartMethod = getMethod(clazz, notBetweenFields[0]);
|
|
|
+ Object notStartVal = notStartMethod != null ? notStartMethod.invoke(obj) : null;
|
|
|
+ Method notEndMethod = getMethod(clazz, notBetweenFields[1]);
|
|
|
+ Object notEndVal = notEndMethod != null ? notEndMethod.invoke(obj) : null;
|
|
|
+ if (!ObjectUtils.isEmpty(notStartVal) && !ObjectUtils.isEmpty(notEndVal)) {
|
|
|
+ queryWrapper.notBetween(getSFunction(clazz, fieldName), notStartVal,
|
|
|
+ notEndVal);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ case EXISTS:
|
|
|
break;
|
|
|
case NOT_EXISTS:
|
|
|
break;
|
|
|
case NOT_IN:
|
|
|
+ // 同IN,取反逻辑
|
|
|
+ if (fieldValue != null && fieldValue.toString().contains(",")) {
|
|
|
+ List<String> notInList = Arrays.asList(fieldValue.toString().split(","));
|
|
|
+ if (!ObjectUtils.isEmpty(notInList)) {
|
|
|
+ queryWrapper.notIn(getSFunction(clazz, fieldName), notInList);
|
|
|
+ }
|
|
|
+ }
|
|
|
break;
|
|
|
- case OR:
|
|
|
- queryWrapper.or();
|
|
|
- break;
|
|
|
+
|
|
|
case ORDER_BY:
|
|
|
+ // 支持多字段排序(格式:{"field1,asc", "field2,desc"})
|
|
|
+ // @QueryWapper(value = QueryKeyword.ORDER_BY, attribute = {"createTime,desc",
|
|
|
+ // "id,asc"})
|
|
|
+ // private String orderBy;
|
|
|
+ String[] orderRules = queryWapperAnnotation.attribute();
|
|
|
+ if (ArrayUtils.isNotEmpty(orderRules)) {
|
|
|
+ for (String rule : orderRules) {
|
|
|
+ if (StringUtils.isBlank(rule))
|
|
|
+ continue;
|
|
|
+ String[] parts = rule.split(",", 2);
|
|
|
+ String orderField = parts[0].trim();
|
|
|
+ if (StringUtils.isBlank(orderField))
|
|
|
+ continue;
|
|
|
+ String direction = parts.length > 1 ? parts[1].trim().toUpperCase() : "ASC";
|
|
|
+ String dbOrderField = getDbFieldName(clazz, orderField, tableField);
|
|
|
+ if ("DESC".equals(direction)) {
|
|
|
+ queryWrapper.orderByDesc(getSFunction(clazz, dbOrderField));
|
|
|
+ } else {
|
|
|
+ queryWrapper.orderByAsc(getSFunction(clazz, dbOrderField));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
break;
|
|
|
case ASC:
|
|
|
queryWrapper.orderByAsc(fieldName);
|
|
|
@@ -238,6 +352,17 @@ public class QueryLambdaUtil {
|
|
|
return queryWrapper;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取数据库字段名(处理TableField注解)
|
|
|
+ */
|
|
|
+ private static <T> String getDbFieldName(Class<T> clazz, String fieldName, TableField tableField) {
|
|
|
+ if (tableField != null) {
|
|
|
+ return tableField.value();
|
|
|
+ }
|
|
|
+ // 无注解时驼峰转下划线
|
|
|
+ return StringUtils.camelToUnderline(fieldName);
|
|
|
+ }
|
|
|
+
|
|
|
// 获取get方法
|
|
|
private static Method getMethod(Class<?> clazz, String filedName) throws Exception {
|
|
|
// String alpha = filedName.substring(0, 1).toUpperCase();
|