|
|
@@ -1,4 +1,5 @@
|
|
|
package mybatisex.core.mapper;
|
|
|
+
|
|
|
import java.lang.reflect.Field;
|
|
|
import java.lang.reflect.Method;
|
|
|
import java.lang.reflect.Modifier;
|
|
|
@@ -106,10 +107,13 @@ public class QueryWrapperUtil {
|
|
|
break;
|
|
|
// 这里需要,修改为集合
|
|
|
case IN:
|
|
|
- if (fieldValue != null && fieldValue.toString().contains(",")) {
|
|
|
- List<String> result = Arrays.asList(fieldValue.toString().split(","));
|
|
|
- queryWrapper.in(!ObjectUtils.isEmpty(fieldValue), fieldName, result);
|
|
|
- }
|
|
|
+ if (fieldValue != null)
|
|
|
+ if (fieldValue.toString().contains(",")) {
|
|
|
+ List<String> result = Arrays.asList(fieldValue.toString().split(","));
|
|
|
+ queryWrapper.in(!ObjectUtils.isEmpty(fieldValue), fieldName, result);
|
|
|
+ } else {
|
|
|
+ queryWrapper.in(!ObjectUtils.isEmpty(fieldValue), fieldName, fieldValue);
|
|
|
+ }
|
|
|
break;
|
|
|
case GT:
|
|
|
queryWrapper.gt(!ObjectUtils.isEmpty(fieldValue), fieldName, fieldValue);
|
|
|
@@ -142,35 +146,134 @@ public class QueryWrapperUtil {
|
|
|
queryWrapper.orderByDesc(fieldName);
|
|
|
break;
|
|
|
case AND:
|
|
|
- //queryWrapper.and(!ObjectUtils.isEmpty(fieldValue), fieldName, fieldValue);
|
|
|
- //queryWrapper.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) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Object andValue = andMethod.invoke(obj);
|
|
|
+ if (!ObjectUtils.isEmpty(andValue)) {
|
|
|
+ String dbAndField = getDbFieldName(clazz, andField);
|
|
|
+ queryWrapper.eq(true, dbAndField, andValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
break;
|
|
|
case BETWEEN:
|
|
|
-// queryWrapper.b
|
|
|
-// queryWrapper.and
|
|
|
+ // 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;
|
|
|
+ boolean betweenCondition = !ObjectUtils.isEmpty(startVal)
|
|
|
+ && !ObjectUtils.isEmpty(endVal);
|
|
|
+ queryWrapper.between(betweenCondition, fieldName, startVal, endVal);
|
|
|
+ }
|
|
|
break;
|
|
|
case EXISTS:
|
|
|
+ // attribute[0]传入EXISTS子查询SQL
|
|
|
+ String[] existsSql = queryWapperAnnotation.attribute();
|
|
|
+ if (ArrayUtils.isNotEmpty(existsSql) && !StringUtils.isBlank(existsSql[0])) {
|
|
|
+ queryWrapper.exists(existsSql[0]);
|
|
|
+ }
|
|
|
break;
|
|
|
case GROUP_BY:
|
|
|
+ // attribute指定分组字段(格式:{"field1", "field2"})
|
|
|
+ String[] groupFields = queryWapperAnnotation.attribute();
|
|
|
+ if (ArrayUtils.isNotEmpty(groupFields)) {
|
|
|
+ for (String groupField : groupFields) {
|
|
|
+ if (StringUtils.isBlank(groupField)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String dbGroupField = getDbFieldName(clazz, groupField);
|
|
|
+ queryWrapper.groupBy(dbGroupField);
|
|
|
+ }
|
|
|
+ }
|
|
|
break;
|
|
|
case HAVING:
|
|
|
+ // attribute[0]直接写having条件SQL
|
|
|
+ String[] havingConditions = queryWapperAnnotation.attribute();
|
|
|
+ if (ArrayUtils.isNotEmpty(havingConditions) && !StringUtils.isBlank(havingConditions[0])) {
|
|
|
+ queryWrapper.having(havingConditions[0]);
|
|
|
+ }
|
|
|
break;
|
|
|
case IS_NOT_NULL:
|
|
|
+ queryWrapper.isNotNull(fieldName);
|
|
|
break;
|
|
|
case IS_NULL:
|
|
|
+ queryWrapper.isNull(fieldName);
|
|
|
break;
|
|
|
case NOT:
|
|
|
+ // attribute[0]指定要取反的字段
|
|
|
+ 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);
|
|
|
+ queryWrapper.not(true, i -> i.eq(dbNotField, notValue));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
break;
|
|
|
case NOT_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;
|
|
|
+ boolean notBetweenCondition = !ObjectUtils.isEmpty(notStartVal)
|
|
|
+ && !ObjectUtils.isEmpty(notEndVal);
|
|
|
+ queryWrapper.notBetween(notBetweenCondition, fieldName, notStartVal, notEndVal);
|
|
|
+ }
|
|
|
break;
|
|
|
case NOT_EXISTS:
|
|
|
+ String[] notExistsSql = queryWapperAnnotation.attribute();
|
|
|
+ if (ArrayUtils.isNotEmpty(notExistsSql) && !StringUtils.isBlank(notExistsSql[0])) {
|
|
|
+ queryWrapper.notExists(notExistsSql[0]);
|
|
|
+ }
|
|
|
break;
|
|
|
case NOT_IN:
|
|
|
+ if (fieldValue != null && fieldValue.toString().contains(",")) {
|
|
|
+ List<String> notInList = Arrays.asList(fieldValue.toString().split(","));
|
|
|
+ if (!ObjectUtils.isEmpty(notInList)) {
|
|
|
+ queryWrapper.notIn(true, fieldName, notInList);
|
|
|
+ }
|
|
|
+ }
|
|
|
break;
|
|
|
case OR:
|
|
|
queryWrapper.or();
|
|
|
break;
|
|
|
case ORDER_BY:
|
|
|
+ // 支持多字段排序(格式:{"field1,asc", "field2,desc"})
|
|
|
+ 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);
|
|
|
+ if ("DESC".equals(direction)) {
|
|
|
+ queryWrapper.orderByDesc(dbOrderField);
|
|
|
+ } else {
|
|
|
+ queryWrapper.orderByAsc(dbOrderField);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
break;
|
|
|
case ASC:
|
|
|
queryWrapper.orderByAsc(fieldName);
|
|
|
@@ -234,6 +337,26 @@ public class QueryWrapperUtil {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取数据库字段名(优先TableField.value,其次驼峰转下划线)
|
|
|
+ */
|
|
|
+ private static <T> String getDbFieldName(Class<T> clazz, String fieldName) {
|
|
|
+ if (StringUtils.isBlank(fieldName)) {
|
|
|
+ return fieldName;
|
|
|
+ }
|
|
|
+ for (Field f : reflectForField(clazz)) {
|
|
|
+ if (!f.getName().equals(fieldName)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ TableField attrTableField = AnnotationUtils.getAnnotation(f, TableField.class);
|
|
|
+ if (attrTableField != null && attrTableField.exist() && !StringUtils.isBlank(attrTableField.value())) {
|
|
|
+ return attrTableField.value();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return StringUtils.camelToUnderline(fieldName);
|
|
|
+ }
|
|
|
+
|
|
|
// 获取get方法
|
|
|
public static Method getMethod(Class<?> clazz, String filedName) throws Exception {
|
|
|
// String alpha = filedName.substring(0, 1).toUpperCase();
|