|
|
@@ -1,9 +1,6 @@
|
|
|
package me.zhengjie.base.plus;
|
|
|
|
|
|
-import java.beans.PropertyDescriptor;
|
|
|
import java.lang.reflect.Field;
|
|
|
-import java.lang.reflect.Method;
|
|
|
-import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
|
|
|
@@ -19,22 +16,6 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
* @Description 拼接查询条件工具类
|
|
|
*/
|
|
|
public class QueryWrapperUtil {
|
|
|
- /**
|
|
|
- * 得到父类的对象
|
|
|
- *
|
|
|
- * @param <T> 查询的泛型
|
|
|
- * @param clazz
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static <T> List<Field> reflectForField(Class<T> clazz) {
|
|
|
- Class<?> tmpClazz = clazz;
|
|
|
- List<Field> fieldList = new ArrayList<>();
|
|
|
- while (tmpClazz != null) {
|
|
|
- fieldList.addAll(Arrays.asList(tmpClazz.getDeclaredFields()));
|
|
|
- tmpClazz = tmpClazz.getSuperclass();
|
|
|
- }
|
|
|
- return fieldList;
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* 拼接查询条件
|
|
|
@@ -48,29 +29,24 @@ public class QueryWrapperUtil {
|
|
|
if (ArrayUtils.isNotEmpty(jsonObj)) {
|
|
|
param = jsonObj[0];
|
|
|
}
|
|
|
+
|
|
|
QueryWrapper<T> queryWrapper = new QueryWrapper<>();
|
|
|
Class<?> clazz = obj.getClass();
|
|
|
try {
|
|
|
- // TODO 这里应该通过共有方法来进行反射
|
|
|
- for (Field field : reflectForField(clazz)) {
|
|
|
-
|
|
|
+ // 反射遍历属性
|
|
|
+ for (Field field : clazz.getDeclaredFields()) {
|
|
|
+ // 获取属性名
|
|
|
+ String fieldname = field.getName();
|
|
|
// 抑制Java对修饰符的检查
|
|
|
-// field.setAccessible(true);
|
|
|
+ field.setAccessible(true);
|
|
|
// 获取属性值
|
|
|
-// Object fieldValue = field.get(obj);
|
|
|
+ Object fieldValue = field.get(obj);
|
|
|
// String fieldValue = getFieldValue(obj ,field.getName()).toString();
|
|
|
TableField tableField = AnnotationUtils.getAnnotation(field, TableField.class);
|
|
|
// 字段没有TableField这个注解和有这个主机上false的
|
|
|
if (ObjectUtils.isEmpty(tableField) || !tableField.exist()) {
|
|
|
continue;
|
|
|
}
|
|
|
- // 获取属性名
|
|
|
- String name = field.getName();
|
|
|
- // 声明属性描述对象
|
|
|
- PropertyDescriptor propertyDescriptor = new PropertyDescriptor(name, clazz);
|
|
|
- // 获取getter方法
|
|
|
- Method method = propertyDescriptor.getReadMethod();
|
|
|
- Object fieldValue = method.invoke(obj);
|
|
|
String fieldName = tableField.value();
|
|
|
// 默认是相等
|
|
|
QueryWapper queryWapperAnnotation = AnnotationUtils.getAnnotation(field, QueryWapper.class);
|
|
|
@@ -128,35 +104,24 @@ public class QueryWrapperUtil {
|
|
|
// 设置开始的值和结束的值
|
|
|
String[] attribute = queryWapperAnnotation.attribute();
|
|
|
// 得到开始的属性值,通过反射设置属性值
|
|
|
-// Field startRange = clazz.getDeclaredField(attribute[0]);
|
|
|
-// startRange.setAccessible(true);
|
|
|
-// Object val = startRange.get(obj);
|
|
|
- String startRange = attribute[0];
|
|
|
- propertyDescriptor = new PropertyDescriptor(startRange, clazz);
|
|
|
- // 获取getter方法
|
|
|
- method = propertyDescriptor.getReadMethod();
|
|
|
- Object val = method.invoke(obj);
|
|
|
+ Field startRange = clazz.getDeclaredField(attribute[0]);
|
|
|
+ startRange.setAccessible(true);
|
|
|
+ Object val = startRange.get(obj);
|
|
|
if (!ObjectUtils.isEmpty(val)) {
|
|
|
queryWrapper.ge(fieldName, val);
|
|
|
}
|
|
|
- // 结束的属性值,通过反射设置属性值
|
|
|
- String endRange = attribute[1];
|
|
|
- propertyDescriptor = new PropertyDescriptor(endRange, clazz);
|
|
|
- // 获取getter方法
|
|
|
- method = propertyDescriptor.getReadMethod();
|
|
|
- val = method.invoke(obj);
|
|
|
- if (!ObjectUtils.isEmpty(val)) {
|
|
|
- queryWrapper.le(fieldName, val);
|
|
|
- }
|
|
|
- // ---属性值查询结束的值----
|
|
|
-
|
|
|
- // 通过传入的参数查询值
|
|
|
// 然后通过参数查询
|
|
|
val = param.get(attribute[0]);
|
|
|
if (!ObjectUtils.isEmpty(val)) {
|
|
|
queryWrapper.ge(fieldName, val);
|
|
|
}
|
|
|
-
|
|
|
+ // 结束的属性值,通过反射设置属性值
|
|
|
+ Field endRange = clazz.getDeclaredField(attribute[1]);
|
|
|
+ endRange.setAccessible(true);
|
|
|
+ val = endRange.get(obj);
|
|
|
+ if (!ObjectUtils.isEmpty(val)) {
|
|
|
+ queryWrapper.le(fieldName, val);
|
|
|
+ }
|
|
|
// 然后通过参数查询
|
|
|
val = param.get(attribute[1]);
|
|
|
if (!ObjectUtils.isEmpty(val)) {
|