diff --git a/common/src/main/java/com/hzya/frame/beanutil/BeanUtil.java b/common/src/main/java/com/hzya/frame/beanutil/BeanUtil.java index 12d83d25..4f3e25fd 100644 --- a/common/src/main/java/com/hzya/frame/beanutil/BeanUtil.java +++ b/common/src/main/java/com/hzya/frame/beanutil/BeanUtil.java @@ -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 copyProperties(Object source, Object target) { + Map 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 copyPropertiesV2(Object source, Object target) { + // 获取 target 及其父类的所有字段映射 + Map targetFieldMap = new HashMap<>(); + Class currentClass = target.getClass(); + while (currentClass != null) { + Map currentClassMap = CacheFieldMap.getFieldMap(currentClass); + currentClassMap.forEach((key, value) -> targetFieldMap.put(key, value)); + currentClass = currentClass.getSuperclass(); + } + + Map 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> cacheMap = new HashMap<>(); + + private static Map getFieldMap(Class clazz) { + Map result = cacheMap.get(clazz.getName()); + if (result == null) { + synchronized (CacheFieldMap.class) { + if (result == null) { + Map 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; + } } } diff --git a/common/src/main/java/com/hzya/frame/split/SplitListByCountUtil.java b/common/src/main/java/com/hzya/frame/split/SplitListByCountUtil.java new file mode 100644 index 00000000..c7f0211a --- /dev/null +++ b/common/src/main/java/com/hzya/frame/split/SplitListByCountUtil.java @@ -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 List> splitListByCount(List list, int count) { + List> 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; + } +}