'多表支持修改,完成1、列表配置,2、查询配置,3、导出配置'
This commit is contained in:
@@ -51,79 +51,110 @@ public class Kv<K,V> extends LinkedHashMap<K,V> {
|
||||
}
|
||||
|
||||
public <T> T toBean(Class<T> type) {
|
||||
return (T) toBean(this, type);
|
||||
return toBean(this, type);
|
||||
}
|
||||
|
||||
// 首字母大写
|
||||
private static Function<String, String> upFirst = (s) -> {
|
||||
return s.substring(0, 1).toUpperCase() + s.substring(1);
|
||||
};
|
||||
private static Map<Class, Class[]> clazzMap = new HashMap<>();
|
||||
/*private static Map<Class, Class[]> clazzMap = new HashMap<>();
|
||||
static {
|
||||
clazzMap.put(ArrayList.class, new Class[]{List.class, ArrayList.class, String.class});
|
||||
clazzMap.put(HashMap.class, new Class[]{Map.class, HashMap.class, String.class});
|
||||
clazzMap.put(Long.class, new Class[]{Long.class, Integer.class, long.class, int.class, short.class, String.class});
|
||||
clazzMap.put(String.class, new Class[]{String.class, Integer.class, Date.class});
|
||||
clazzMap.put(Double.class, new Class[]{Double.class, Long.class, Integer.class, long.class, int.class, short.class, String.class});
|
||||
}
|
||||
}*/
|
||||
private static Predicate<Class> isNumber = (t) -> {
|
||||
return t == Integer.class || t == int.class
|
||||
|| t == Long.class || t == long.class
|
||||
|| t == Double.class || t == double.class
|
||||
;
|
||||
};
|
||||
public static <T> T toBean(Map m, Class<T> type) {
|
||||
/*public static <T> T toBean2(Map m, Class<T> type) {
|
||||
try {
|
||||
Object obj = type.newInstance();
|
||||
m.forEach((k, v) -> {
|
||||
String methodName = "set" + upFirst.apply(k+"");
|
||||
Class[] clazzs = clazzMap.get(v == null ? null : v.getClass());
|
||||
if (clazzs == null) {
|
||||
//doc.set(k, v);
|
||||
} else {
|
||||
for (Class clazz : clazzs) {
|
||||
Method method = null;
|
||||
try {
|
||||
method = type.getDeclaredMethod(methodName, clazz);
|
||||
} catch (NoSuchMethodException e) {
|
||||
}
|
||||
if (method != null) {
|
||||
try {
|
||||
Object _v = toAs(v, clazz);
|
||||
//如发现 映射异常,打开下面的注释查看 进行映射的值,并对上面的值转换过程升级
|
||||
//System.out.printf("%s:%s %s%n", k,v, v.getClass());
|
||||
method.invoke(obj, _v);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (String k : (Set<String>)m.keySet()) {
|
||||
Object v = m.get(k);
|
||||
if (v == null) {
|
||||
continue;
|
||||
}
|
||||
});
|
||||
|
||||
//得到目标方法
|
||||
String methodName = "set" + upFirst.apply(k + "");
|
||||
Method method = null;
|
||||
Class tClazz = null;
|
||||
try {
|
||||
method = type.getDeclaredMethod(methodName, tClazz = v.getClass());
|
||||
if (method == null) {
|
||||
Class[] clazzs = clazzMap.get(v == null ? null : v.getClass());
|
||||
if (clazzs == null) {
|
||||
//doc.set(k, v);
|
||||
} else {
|
||||
|
||||
for (Class clazz : clazzs) {
|
||||
try {
|
||||
|
||||
type.getMethods();
|
||||
|
||||
method = type.getDeclaredMethod(methodName, tClazz = clazz);
|
||||
} catch (NoSuchMethodException e) {
|
||||
}
|
||||
}
|
||||
|
||||
if (method != null) {
|
||||
try {
|
||||
Object _v = toAs(v, tClazz);
|
||||
//如发现 映射异常,打开下面的注释查看 进行映射的值,并对上面的值转换过程升级
|
||||
//System.out.printf("%s:%s %s%n", k,v, v.getClass());
|
||||
method.invoke(obj, _v);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
if (method != null) {
|
||||
|
||||
}
|
||||
//已知:key,value, 一个实例类型对象
|
||||
//找到目标方法
|
||||
//转换数据类型
|
||||
|
||||
}
|
||||
|
||||
return (T) obj;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}*/
|
||||
|
||||
public static <T> T toAs(Object v, Class<T> clazz) {
|
||||
if (v == null) {
|
||||
return null;
|
||||
}
|
||||
if (v.getClass() == clazz) {
|
||||
} else if (v.getClass() == clazz) {
|
||||
return (T) v;
|
||||
} else if (clazz == String.class) {
|
||||
return (T) String.valueOf(v);
|
||||
}
|
||||
|
||||
Object _v = null;
|
||||
try {
|
||||
if (v == null || "".equals(v)) {
|
||||
|
||||
} else if (v.getClass() == Long.class && clazz != Long.class) {//多种数值类型的处理: Long => x
|
||||
} else if (v.getClass() == Long.class) {//多种数值类型的处理: Long => x
|
||||
switch (clazz.getSimpleName()) {
|
||||
case "int":
|
||||
case "Integer": _v = (int)(long) v; break;
|
||||
@@ -176,4 +207,103 @@ public class Kv<K,V> extends LinkedHashMap<K,V> {
|
||||
}
|
||||
return (T) _v;
|
||||
}
|
||||
|
||||
public static <T> T toBean(Map map, Class<T> clazz) {
|
||||
//按照方法名 + 类型寻找,
|
||||
//按照方法名 寻找
|
||||
//+
|
||||
Object obj = null;
|
||||
try {
|
||||
obj = clazz.newInstance();
|
||||
} catch (InstantiationException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
for (String k : (Set<String>)map.keySet()) {
|
||||
Object v = map.get(k);
|
||||
if (v == null) continue;
|
||||
//寻找method
|
||||
try {
|
||||
String methodName = "set" + upFirst.apply(k);
|
||||
Class tClazz = null;
|
||||
Method method = null;
|
||||
try {
|
||||
method = clazz.getMethod(methodName, tClazz = v.getClass());
|
||||
} catch (NoSuchMethodException e) {
|
||||
//e.printStackTrace();
|
||||
}
|
||||
if (method == null) {
|
||||
for (Method _method : clazz.getMethods()) {
|
||||
if (methodName.equals(_method.getName()) && _method.getParameterCount() == 1) {
|
||||
method = _method;
|
||||
tClazz = _method.getParameterTypes()[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (method == null) {
|
||||
for (Method _method : clazz.getMethods()) {
|
||||
if (methodName.equalsIgnoreCase(_method.getName()) && _method.getParameterCount() == 1) {
|
||||
method = _method;
|
||||
tClazz = _method.getParameterTypes()[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (method != null) {
|
||||
method.invoke(obj, toAs(v, tClazz));
|
||||
}
|
||||
|
||||
//没有方法,找属性注解
|
||||
if (method == null) {
|
||||
Field field = null;
|
||||
Field[] fields = clazz.getDeclaredFields();
|
||||
for (Field _field : fields) {
|
||||
To to = _field.getAnnotation(To.class);
|
||||
if (to != null && k.equals(to.value())) {
|
||||
field = _field;
|
||||
tClazz = _field.getType();
|
||||
break;
|
||||
}
|
||||
}
|
||||
/*if (field == null) {
|
||||
fields = clazz.getDeclaredFields();
|
||||
continue;
|
||||
}
|
||||
while (true){
|
||||
|
||||
|
||||
break;
|
||||
}*/
|
||||
|
||||
if (field != null) {
|
||||
field.setAccessible(true);
|
||||
field.set(obj, toAs(v, tClazz));
|
||||
}
|
||||
}
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return (T) obj;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Map map = new HashMap<>();
|
||||
|
||||
map.put("name", "xxxx");
|
||||
map.put("age", 12);
|
||||
map.put("abx", 123);
|
||||
|
||||
User user = toBean(map, User.class);
|
||||
|
||||
System.out.println(user);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
12
src/main/java/net/tccn/base/To.java
Normal file
12
src/main/java/net/tccn/base/To.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package net.tccn.base;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
/**
|
||||
* Created by liangxianyou at 2019/3/26 20:35.
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface To {
|
||||
String value();
|
||||
}
|
||||
29
src/main/java/net/tccn/base/User.java
Normal file
29
src/main/java/net/tccn/base/User.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package net.tccn.base;
|
||||
|
||||
/**
|
||||
* Created by liangxianyou at 2019/3/26 19:36.
|
||||
*/
|
||||
public class User {
|
||||
|
||||
private String name;
|
||||
private int age;
|
||||
@To("")
|
||||
private float attr;
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
"name='" + name + '\'' +
|
||||
", age=" + age +
|
||||
", attr='" + attr + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
23
src/main/java/net/tccn/base/UtilityExt.java
Normal file
23
src/main/java/net/tccn/base/UtilityExt.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package net.tccn.base;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Created by liangxianyou at 2019/3/19 18:01.
|
||||
*/
|
||||
public class UtilityExt {
|
||||
|
||||
public static <T> Set<T> streamConcat(Stream<T> ... streams) {
|
||||
Stream<T> stream = Stream.empty();
|
||||
for (int i = 0; i < streams.length-1; i++) {
|
||||
stream = Stream.concat(
|
||||
stream, streams[i]
|
||||
);
|
||||
}
|
||||
return stream.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user