提交util代码
This commit is contained in:
parent
4464c67e83
commit
2e183532d6
|
@ -1,5 +1,12 @@
|
|||
package com.hzya.frame.beanutil;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Content
|
||||
* @Author 👻👻👻👻👻👻👻👻👻👻 gjh
|
||||
|
@ -8,7 +15,100 @@ package com.hzya.frame.beanutil;
|
|||
*/
|
||||
public class BeanUtil {
|
||||
|
||||
public static String getBean(){
|
||||
return "232";
|
||||
static Logger logger = LoggerFactory.getLogger(BeanUtil.class);
|
||||
|
||||
public static String getBean() {
|
||||
return "232";
|
||||
}
|
||||
|
||||
/**
|
||||
* 忽略大小写的bean copy方法
|
||||
*
|
||||
* @author liuyang
|
||||
*/
|
||||
public static <T> T copyProperties(Object source, Object target) {
|
||||
Map<String, Field> sourceMap = CacheFieldMap.getFieldMap(source.getClass());
|
||||
CacheFieldMap.getFieldMap(target.getClass()).values().forEach((it) -> {
|
||||
logger.info(it.getName());
|
||||
Field field = sourceMap.get(it.getName().toLowerCase().replace("_", ""));
|
||||
if (field != null) {
|
||||
it.setAccessible(true);
|
||||
field.setAccessible(true);
|
||||
try {
|
||||
//忽略null
|
||||
if (field.get(source) != null) {
|
||||
if ("java.lang.Long".equals(it.getType().getName())) {
|
||||
it.set(target, Long.valueOf(field.get(source) + ""));
|
||||
} else {
|
||||
it.set(target, field.get(source));
|
||||
}
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
return (T) target;
|
||||
}
|
||||
|
||||
/**
|
||||
* 忽略大小写的 bean copy 方法,可获取父类字段
|
||||
*
|
||||
* @author liuyang
|
||||
*/
|
||||
public static <T> T copyPropertiesV2(Object source, Object target) {
|
||||
// 获取 target 及其父类的所有字段映射
|
||||
Map<String, Field> targetFieldMap = new HashMap<>();
|
||||
Class<?> currentClass = target.getClass();
|
||||
while (currentClass != null) {
|
||||
Map<String, Field> currentClassMap = CacheFieldMap.getFieldMap(currentClass);
|
||||
currentClassMap.forEach((key, value) -> targetFieldMap.put(key, value));
|
||||
currentClass = currentClass.getSuperclass();
|
||||
}
|
||||
|
||||
Map<String, Field> sourceMap = CacheFieldMap.getFieldMap(source.getClass());
|
||||
|
||||
targetFieldMap.values().forEach((it) -> {
|
||||
logger.info(it.getName());
|
||||
Field field = sourceMap.get(it.getName().toLowerCase().replace("_", ""));
|
||||
if (field != null) {
|
||||
it.setAccessible(true);
|
||||
field.setAccessible(true);
|
||||
try {
|
||||
// 忽略 null
|
||||
if (field.get(source) != null) {
|
||||
if ("java.lang.Long".equals(it.getType().getName())) {
|
||||
it.set(target, Long.valueOf(field.get(source) + ""));
|
||||
} else {
|
||||
it.set(target, field.get(source));
|
||||
}
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
return (T) target;
|
||||
}
|
||||
|
||||
private static class CacheFieldMap {
|
||||
private static Map<String, Map<String, Field>> cacheMap = new HashMap<>();
|
||||
|
||||
private static Map<String, Field> getFieldMap(Class clazz) {
|
||||
Map<String, Field> result = cacheMap.get(clazz.getName());
|
||||
if (result == null) {
|
||||
synchronized (CacheFieldMap.class) {
|
||||
if (result == null) {
|
||||
Map<String, Field> fieldMap = new HashMap<>();
|
||||
for (Field field : clazz.getDeclaredFields()) {
|
||||
fieldMap.put(field.getName().toLowerCase().replace("_", ""), field);
|
||||
}
|
||||
cacheMap.put(clazz.getName(), fieldMap);
|
||||
result = cacheMap.get(clazz.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
package com.hzya.frame.split;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author:liuyang
|
||||
* @Package:com.hzya.frame.split
|
||||
* @Project:kangarooDataCenterV3
|
||||
* @name:SplitListByCountUtil
|
||||
* @Date:2024/8/2 14:20
|
||||
* @Filename:SplitListByCountUtil
|
||||
*/
|
||||
public class SplitListByCountUtil {
|
||||
/**
|
||||
* List集合拆分
|
||||
*
|
||||
* @param list 原始数据 6000
|
||||
* @param count 每个list的元素数量 1000
|
||||
* @return 拆分得到的list集合
|
||||
*/
|
||||
public static <T> List<List<T>> splitListByCount(List<T> list, int count) {
|
||||
List<List<T>> listAll = new ArrayList<>();
|
||||
int size = list.size();
|
||||
if (size > count) { //*size:6000 > count: 1000
|
||||
int absInt = Math.abs(size / count); //6
|
||||
if (size - absInt * count > 0) {
|
||||
listAll.add(list.subList(absInt * count, size));
|
||||
}
|
||||
for (int i = 1; i < absInt + 1; i++) {
|
||||
listAll.add(list.subList((i - 1) * count, i * count));
|
||||
}
|
||||
} else {
|
||||
listAll.add(list);
|
||||
}
|
||||
return listAll;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue