update:查询条件按类型转换支持pg
This commit is contained in:
@@ -10,6 +10,7 @@ import cn.hutool.extra.spring.SpringUtil;
|
|||||||
import cn.hutool.json.JSONArray;
|
import cn.hutool.json.JSONArray;
|
||||||
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONObject;
|
||||||
import com.cool.core.enums.QueryModeEnum;
|
import com.cool.core.enums.QueryModeEnum;
|
||||||
|
import com.cool.core.util.ConvertUtil;
|
||||||
import com.mybatisflex.annotation.Table;
|
import com.mybatisflex.annotation.Table;
|
||||||
import com.mybatisflex.core.query.QueryColumn;
|
import com.mybatisflex.core.query.QueryColumn;
|
||||||
import com.mybatisflex.core.query.QueryCondition;
|
import com.mybatisflex.core.query.QueryCondition;
|
||||||
@@ -127,16 +128,17 @@ public class CrudOption<T> {
|
|||||||
private QueryWrapper build(QueryWrapper queryWrapper, Class<T> entityClass) {
|
private QueryWrapper build(QueryWrapper queryWrapper, Class<T> entityClass) {
|
||||||
if (ObjectUtil.isNotEmpty(fieldEq)) {
|
if (ObjectUtil.isNotEmpty(fieldEq)) {
|
||||||
Arrays.stream(fieldEq).toList().forEach(filed -> {
|
Arrays.stream(fieldEq).toList().forEach(filed -> {
|
||||||
Object obj = requestParams.get(StrUtil.toCamelCase(filed.getName()));
|
String filedName = StrUtil.toCamelCase(filed.getName());
|
||||||
|
Object obj = requestParams.get(filedName);
|
||||||
if (ObjUtil.isEmpty(obj)) {
|
if (ObjUtil.isEmpty(obj)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (obj instanceof JSONArray) {
|
if (obj instanceof JSONArray) {
|
||||||
// 集合
|
// 集合
|
||||||
queryWrapper.and(filed.in(((JSONArray)obj).toArray()));
|
queryWrapper.and(filed.in(ConvertUtil.covertListByClass(filedName, (JSONArray)obj, entityClass).toArray()));
|
||||||
} else {
|
} else {
|
||||||
// 对象
|
// 对象
|
||||||
queryWrapper.and(filed.eq(obj));
|
queryWrapper.and(filed.eq(ConvertUtil.convertByClass(filedName, obj, entityClass)));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,13 @@
|
|||||||
package com.cool.core.util;
|
package com.cool.core.util;
|
||||||
|
|
||||||
|
import cn.hutool.core.convert.Convert;
|
||||||
|
import cn.hutool.core.util.ReflectUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.*;
|
||||||
import java.io.File;
|
import java.lang.reflect.Field;
|
||||||
import java.io.FileInputStream;
|
import java.util.Collections;
|
||||||
import java.io.FileNotFoundException;
|
import java.util.List;
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.ObjectInputStream;
|
|
||||||
import java.io.ObjectOutputStream;
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
@@ -203,4 +200,58 @@ public class ConvertUtil {
|
|||||||
private static String[] splitCamelCase(String input) {
|
private static String[] splitCamelCase(String input) {
|
||||||
return input.split("(?<=.)(?=[A-Z])");
|
return input.split("(?<=.)(?=[A-Z])");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将给定的字段值转换为可序列化的形式
|
||||||
|
* 此方法旨在将一个对象的特定字段值转换为其相应的可序列化类型
|
||||||
|
* 它在序列化和反序列化过程中特别有用,确保字段值可以被正确处理
|
||||||
|
*
|
||||||
|
* @param fieldName 字段名称,用于查找字段类型
|
||||||
|
* @param fieldValue 待转换的字段值
|
||||||
|
* @param clazz 包含该字段的类
|
||||||
|
* @return 转换后的可序列化字段值,如果无法确定字段类型,则返回原始值
|
||||||
|
*/
|
||||||
|
public static Object convertByClass(String fieldName, Object fieldValue, Class<?> clazz) {
|
||||||
|
// 检查输入参数是否为空,如果字段名或字段值为空,则直接返回字段值
|
||||||
|
if (fieldName == null || fieldValue == null) {
|
||||||
|
return fieldValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取字段类型
|
||||||
|
Class<?> fieldType = getFieldType(clazz, fieldName);
|
||||||
|
// 如果字段类型为空,则直接返回字段值
|
||||||
|
if (fieldType == null) {
|
||||||
|
return fieldValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用Convert类的convert方法将字段值转换为字段类型
|
||||||
|
return Convert.convert(fieldType, fieldValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Object> covertListByClass(String fieldName, List<Object> fieldValue, Class<?> clazz) {
|
||||||
|
// 检查输入参数是否为空,如果字段名或字段值为空,则直接返回字段值
|
||||||
|
if (fieldName == null || fieldValue == null) {
|
||||||
|
return fieldValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取字段类型
|
||||||
|
Class<?> fieldType = getFieldType(clazz, fieldName);
|
||||||
|
// 如果字段类型为空,则直接返回字段值
|
||||||
|
if (fieldType == null) {
|
||||||
|
return fieldValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Collections.singletonList(Convert.toList(fieldType, fieldValue));
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 获取指定类中指定字段的类型
|
||||||
|
*
|
||||||
|
* @param clazz 目标类
|
||||||
|
* @param fieldName 字段名称
|
||||||
|
* @return 字段的类型 Class,如果字段不存在则返回 null
|
||||||
|
*/
|
||||||
|
public static Class<?> getFieldType(Class<?> clazz, String fieldName) {
|
||||||
|
Field field = ReflectUtil.getField(clazz, fieldName);
|
||||||
|
return field != null ? field.getType() : null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user