├── .gitignore ├── ReadMe.md ├── pom.xml └── src ├── main └── java │ └── org │ └── map │ ├── common │ └── Assert.java │ ├── core │ └── Mapping.java │ └── utils │ └── BeanMapping.java └── test └── java ├── BeanMappingDemo.java ├── Man.java └── Student.java /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/** 5 | !**/src/test/** 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | *.log 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | 30 | ### VS Code ### 31 | .vscode/ 32 | -------------------------------------------------------------------------------- /ReadMe.md: -------------------------------------------------------------------------------- 1 | # Bean Mapping 2 | 3 | **BeanMapping** 致力于**Bean**之间的映射方案。此项目包含自动映射对象与类之间的属性,手动映射类与对象特殊属性。方便开发者通过**BeanMapping**工具类,轻松使用工具来简化应用开发。 4 | 5 | 依托**BeanMapping**,您需要适配以下要求: 6 | 7 | **JDK**:JDK8+ 8 | 9 | **MAVEN**: apache-maven-3.5.0+ 10 | 11 | 12 | 13 | 为**BeanMapping**贡献代码请参考联系查看开发者信息 14 | 15 | ## 主要功能 16 | 17 | * **语法支持**:默认支持**JDK8**,**lambda**表达式,仅限使用JDK8+版本 18 | * **对象对类自动映射**:默认支持 **source -> class**类的自动映射 19 | * **对象对类手动映射**:适配自动映射,支持不一致字段名称,不一致类型字手动映射。 20 | * **集合对集合的自动映射**:默认支持集合 **source**->**class**类集合的自动映射。 21 | * **集合对集合的手动映射**:适配自动映射,支持不一致字段名称,不一致类型字手动映射。 22 | * **集合对集合的范围自动映射**:默认支持范围集合**source -> class**类集合的自动映射,自动适应范围。 23 | * **集合对集合的范围自动映射**:适配自动范围集合映射,支持不一致字段名称,不一致类型字手动映射。 24 | * **对象对类嵌自动套映射**:默认支持嵌套映射,类与类的继承关系之间自动映射 25 | * **对象对类手动嵌套映射**:适配自动映射,类与类的继承关系不一致字段名称,不一致类型字手动映射。 26 | * **对象对类自动映射JSON字符串**: 默认支持 **source -> class**类的自动映射JSON字符串 27 | * **对象对类手动映射JSON字符串**: 适配自动映射,支持不一致字段名称,不一致类型字手动映射**JSON**字符串 28 | * **集合对集合的自动映射JSON字符串**: 默认支持集合 **source -> class**类集合的自动映射**JSON**字符串 29 | * **集合对集合的手动映射JSON字符串**: 适配自动映射,支持不一致字段名称,不一致类型字手动映射**JSON**字符串 30 | 31 | **** 32 | 33 | 34 | 功能演示请参考源码测试用例。 35 | 36 | ## 组件 37 | 38 | ```java 39 | toBean(T source, Class target) 40 | 41 | toBean(T source, Class target, BiConsumer biConsumer) 42 | 43 | toList(Collection source, Class target) 44 | 45 | toList(Collection source, Class target, BiConsumer biConsumer) 46 | 47 | toListRange(List source, Class target, int skip) 48 | 49 | toListRange(List source, Class target, int skip, BiConsumer biConsumer) 50 | 51 | toListRange(List source, Class target, int skip, int limit) 52 | 53 | toListRange(List source, Class target, int skip, int limit, BiConsumer biConsumer) 54 | 55 | toSet(Collection source, Class target) 56 | 57 | toSet(Collection source, Class target, BiConsumer biConsumer) 58 | 59 | toJsonBean(T source, Class target) 60 | 61 | toJsonBean(T source, Class target, BiConsumer biConsumer) 62 | 63 | toJsonList(Collection source, Class target) 64 | 65 | toJsonList(Collection source, Class target, BiConsumer biConsumer) 66 | 67 | toJsonListRange(List source, Class target, int skip) 68 | 69 | toJsonListRange(List source, Class target, int skip, BiConsumer biConsumer) 70 | 71 | toJsonListRange(List source, Class target, int skip, int limit) 72 | 73 | toJsonListRange(List source, Class target, int skip, int limit, BiConsumer biConsumer) 74 | 75 | toJsonSet(Collection source, Class target) 76 | 77 | toJsonSet(Collection source, Class target, BiConsumer biConsumer) 78 | ``` 79 | 80 | 81 | 82 | ## 如何构建 83 | 84 | * **BeanMapping**:最低支持 JDK 1.8。 85 | * **BeanMapping**:最低支持apache-maven-3.5.0+ 86 | 87 | ## 如何使用 88 | 89 | ### 如何引入依赖 90 | 91 | 如果需要使用已发布的版本,在 `dependencyManagement` 中添加如下配置。 92 | 93 | ```xml 94 | 95 | 96 | com.github.xr2117 97 | bean-mapping 98 | 2.1.1 99 | 100 | 101 | ``` 102 | 103 | 然后在 `dependencies` 中添加自己所需使用的依赖即可使用。 104 | 105 | 106 | ### 邮件列表 107 | 108 | xr2117@126.com,欢迎通过此邮件讨论与 **BeanMapping** 相关的一切。 109 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | com.github.xr2117 7 | bean-mapping 8 | 2.1.1 9 | 10 | BeanMapping 11 | https://github.com/xr2117/bean-mapping 12 | BeanMapping 13 | 14 | 15 | 16 | The Apache Software License, Version 2.0 17 | http://www.apache.org/licenses/LICENSE-2.0.txt 18 | repo 19 | 20 | 21 | 22 | https://github.com/xr2117/bean-mapping 23 | https://github.com/xr2117/bean-mapping.git 24 | https://github.com/xr2117 25 | 26 | 27 | 28 | 29 | 30 | 31 | XueRi 32 | xr2117@126.com 33 | https://github.com/xr2117 34 | +8 35 | 36 | 37 | 38 | 39 | 40 | UTF-8 41 | UTF-8 42 | UTF-8 43 | 44 | 45 | 46 | 47 | 48 | org.projectlombok 49 | lombok 50 | 1.18.12 51 | true 52 | 53 | 54 | com.alibaba 55 | fastjson 56 | 1.2.71 57 | 58 | 59 | junit 60 | junit 61 | 4.12 62 | true 63 | test 64 | 65 | 66 | 67 | 68 | 69 | 70 | org.apache.maven.plugins 71 | maven-compiler-plugin 72 | 3.1 73 | 74 | 8 75 | 8 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | release 84 | 85 | true 86 | 87 | 88 | 89 | 90 | org.apache.maven.plugins 91 | maven-source-plugin 92 | 2.2.1 93 | 94 | 95 | package 96 | 97 | jar-no-fork 98 | 99 | 100 | 101 | 102 | 103 | org.apache.maven.plugins 104 | maven-javadoc-plugin 105 | 2.9.1 106 | 107 | 108 | package 109 | 110 | jar 111 | 112 | 113 | 114 | 115 | 116 | org.apache.maven.plugins 117 | maven-gpg-plugin 118 | 1.5 119 | 120 | 121 | verify 122 | 123 | sign 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | ossrh 133 | https://oss.sonatype.org/content/repositories/snapshots 134 | 135 | 136 | ossrh 137 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 138 | 139 | 140 | 141 | 142 | 143 | 144 | -------------------------------------------------------------------------------- /src/main/java/org/map/common/Assert.java: -------------------------------------------------------------------------------- 1 | package org.map.common; 2 | 3 | 4 | import lombok.NonNull; 5 | 6 | /** 7 | * @author Crazy.X 8 | * @version 2.1.1 9 | */ 10 | public class Assert { 11 | 12 | public static void notNull(@NonNull Object object, String message) { 13 | if (object == null) { 14 | throw new IllegalArgumentException(message); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/org/map/core/Mapping.java: -------------------------------------------------------------------------------- 1 | package org.map.core; 2 | 3 | import java.io.Serializable; 4 | import java.lang.reflect.Field; 5 | import java.lang.reflect.InvocationTargetException; 6 | import java.lang.reflect.Modifier; 7 | import java.util.*; 8 | import java.util.function.BiConsumer; 9 | 10 | /** 11 | * @author Crazy.X 12 | * @version 2.1.1 13 | */ 14 | public class Mapping { 15 | 16 | protected static R getInstanceObject(T source, Class target) { 17 | R instance; 18 | try { 19 | instance = target.getDeclaredConstructor().newInstance(); 20 | } catch (InstantiationException | IllegalAccessException e) { 21 | System.err.println("目标类:" + target.getName() + "不能被实例化"); 22 | e.printStackTrace(); 23 | return null; 24 | } catch (NoSuchMethodException | InvocationTargetException e) { 25 | System.err.println("目标类:" + target.getName() + "缺少默认构造方法"); 26 | e.printStackTrace(); 27 | return null; 28 | } 29 | Set declaredFields = new HashSet<>(); 30 | getSourceFields(source.getClass(), declaredFields); 31 | HashMap fieldMap = new HashMap<>(); 32 | getInstanceFields(target, fieldMap); 33 | 34 | for (Field field : declaredFields) { 35 | if (fieldMap.containsKey(field.getName())) { 36 | Field instanceField = fieldMap.get(field.getName()); 37 | if (field.getType().equals(instanceField.getType())) { 38 | field.setAccessible(true); 39 | instanceField.setAccessible(true); 40 | try { 41 | Object val = field.get(source); 42 | instanceField.set(instance, val); 43 | } catch (IllegalAccessException e) { 44 | e.printStackTrace(); 45 | } 46 | } 47 | } 48 | } 49 | return instance; 50 | } 51 | 52 | private static void getSourceFields(Class sourceCla, Set declaredFields) { 53 | boolean serialize = false; 54 | if (!Object.class.equals(sourceCla.getSuperclass())) { 55 | getSourceFields(sourceCla.getSuperclass(), declaredFields); 56 | } 57 | Class[] interfaces = sourceCla.getInterfaces(); 58 | for (Class inter : interfaces) { 59 | if (Serializable.class.equals(inter)) { 60 | serialize = true; 61 | break; 62 | } 63 | } 64 | Field[] fields = sourceCla.getDeclaredFields(); 65 | // Static property does not map 66 | for (Field field : fields) { 67 | if (serialize) { 68 | if (!Modifier.isStatic(field.getModifiers()) || !field.getName().contains("serial")) { 69 | declaredFields.add(field); 70 | } 71 | } else { 72 | if (!Modifier.isStatic(field.getModifiers())) { 73 | declaredFields.add(field); 74 | } 75 | } 76 | } 77 | } 78 | 79 | private static void getInstanceFields(Class instanceCla, HashMap declaredFields) { 80 | if (!Object.class.equals(instanceCla.getSuperclass())) { 81 | getInstanceFields(instanceCla.getSuperclass(), declaredFields); 82 | } 83 | Field[] fields = instanceCla.getDeclaredFields(); 84 | for (Field field : fields) { 85 | // Static property does not map 86 | if (!Modifier.isStatic(field.getModifiers())) { 87 | declaredFields.put(field.getName(), field); 88 | } 89 | } 90 | } 91 | 92 | protected static void getInstanceCollection(Collection targetCollection, Collection source, Class target, BiConsumer biConsumer) { 93 | for (T t : source) { 94 | R instance = getInstanceObject(t, target); 95 | if (!Objects.isNull(biConsumer)) { 96 | biConsumer.accept(t, instance); 97 | } 98 | targetCollection.add(instance); 99 | } 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/main/java/org/map/utils/BeanMapping.java: -------------------------------------------------------------------------------- 1 | package org.map.utils; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import lombok.NonNull; 5 | import org.map.common.Assert; 6 | import org.map.core.Mapping; 7 | 8 | import java.util.*; 9 | import java.util.function.BiConsumer; 10 | 11 | /** 12 | * @author Crazy.X 13 | * @version 2.1.1 14 | */ 15 | public class BeanMapping extends Mapping { 16 | 17 | /** 18 | * 映射一个新的实例 19 | * @param source 数据源 20 | * @param target 目标 21 | * @param 数据源类型 22 | * @param 目标类型 23 | * @return 返回R类型的实例 24 | */ 25 | public static R toBean(@NonNull T source, @NonNull Class target) { 26 | return toBean(source, target, null); 27 | } 28 | 29 | /** 30 | * 映射一个新的实例 31 | * @param source 数据源 32 | * @param target 目标 33 | * @param biConsumer 数据源字段与目标字段不同,可通过手动映射 34 | * @param 数据源类型 35 | * @param 目标类型 36 | * @return 返回R类型的实例 37 | */ 38 | public static R toBean(@NonNull T source, @NonNull Class target, BiConsumer biConsumer) { 39 | Assert.notNull(source, "Source must not be null"); 40 | Assert.notNull(target, "target must not be null"); 41 | R instance = getInstanceObject(source, target); 42 | if (!Objects.isNull(instance) && !Objects.isNull(biConsumer)) { 43 | biConsumer.accept(source, instance); 44 | } 45 | return instance; 46 | } 47 | 48 | /** 49 | * 映射一个新的List 50 | * @param source 数据源 51 | * @param target 目标 52 | * @param 数据源类型 53 | * @param 目标类型 54 | * @return 返回R类型的实例List集合 55 | */ 56 | public static List toList(@NonNull Collection source, @NonNull Class target) { 57 | return toList(source, target, null); 58 | } 59 | 60 | /** 61 | * 映射一个新的List 62 | * @param source 数据源 63 | * @param target 目标 64 | * @param biConsumer biConsumer 数据源字段与目标字段不同,可通过手动映射 65 | * @param 数据源类型 66 | * @param 目标类型 67 | * @return 返回R类型的实例List集合 68 | */ 69 | public static List toList(@NonNull Collection source, @NonNull Class target, BiConsumer biConsumer) { 70 | Assert.notNull(source, "Source must not be null"); 71 | Assert.notNull(target, "target must not be null"); 72 | List targetCollection = new ArrayList<>(); 73 | getInstanceCollection(targetCollection, source, target, biConsumer); 74 | return targetCollection; 75 | } 76 | 77 | /** 78 | * 映射一个新的范围List 79 | * @param source 数据源 80 | * @param target 目标 81 | * @param skip 跳过 82 | * @param 数据源类型 83 | * @param 目标类型 84 | * @return 返回R类型范围的实例List集合 85 | */ 86 | public static List toListRange(@NonNull List source, @NonNull Class target, @NonNull int skip) { 87 | return toListRange(source, target, skip, source.size()); 88 | } 89 | 90 | /** 91 | * 映射一个新的范围List 92 | * @param source 数据源 93 | * @param target 目标 94 | * @param skip 跳过 95 | * @param biConsumer biConsumer 数据源字段与目标字段不同,可通过手动映射 96 | * @param 数据源类型 97 | * @param 目标类型 98 | * @return 返回R类型范围的实例List集合 99 | */ 100 | public static List toListRange(@NonNull List source, @NonNull Class target, @NonNull int skip, BiConsumer biConsumer) { 101 | return toListRange(source, target, skip, source.size(), biConsumer); 102 | } 103 | 104 | /** 105 | * 映射一个新的范围List 106 | * @param source 数据源 107 | * @param target 目标 108 | * @param skip 跳过 109 | * @param limit 截止 110 | * @param 数据源类型 111 | * @param 目标类型 112 | * @return 返回R类型范围的实例List集合 113 | */ 114 | public static List toListRange(@NonNull List source, @NonNull Class target, @NonNull int skip, @NonNull int limit) { 115 | return toListRange(source, target, skip, limit, null); 116 | } 117 | 118 | /** 119 | * 映射一个新的范围List 120 | * @param source 数据源 121 | * @param target 目标 122 | * @param skip 跳过 123 | * @param limit 截止(包含) 124 | * @param biConsumer biConsumer 数据源字段与目标字段不同,可通过手动映射 125 | * @param 数据源类型 126 | * @param 目标类型 127 | * @return 返回R类型范围的实例List集合 128 | */ 129 | public static List toListRange(@NonNull List source, @NonNull Class target, @NonNull int skip, @NonNull int limit, BiConsumer biConsumer) { 130 | Assert.notNull(source, "Source must not be null"); 131 | Assert.notNull(target, "target must not be null"); 132 | if (skip > source.size() || limit < skip || limit < 0 || skip == limit) { 133 | return Collections.emptyList(); 134 | } 135 | List targetCollection = new ArrayList<>(); 136 | int end = limit > source.size() ? source.size() : limit; 137 | getInstanceCollection(targetCollection, source.subList(skip < 0 ? 0 : skip, end), target, biConsumer); 138 | toList(source, target, null); 139 | return targetCollection; 140 | } 141 | 142 | /** 143 | * 映射一个新的Set 144 | * @param source 数据源 145 | * @param target 目标 146 | * @param 数据源类型 147 | * @param 目标类型 148 | * @return 返回R类型的实例Set集合 149 | */ 150 | public static Set toSet(@NonNull Collection source, @NonNull Class target) { 151 | return toSet(source, target, null); 152 | } 153 | 154 | /** 155 | * 映射一个新的Set 156 | * @param source 数据源 157 | * @param target 目标 158 | * @param biConsumer biConsumer 数据源字段与目标字段不同,可通过手动映射 159 | * @param 数据源类型 160 | * @param 目标类型 161 | * @return 返回R类型的实例Set集合 162 | */ 163 | public static Set toSet(@NonNull Collection source, @NonNull Class target, BiConsumer biConsumer) { 164 | Assert.notNull(source, "Source must not be null"); 165 | Assert.notNull(target, "target must not be null"); 166 | Set targetCollection = new HashSet<>(); 167 | getInstanceCollection(targetCollection, source, target, biConsumer); 168 | return targetCollection; 169 | } 170 | 171 | /** 172 | * 映射一个新的JSON实例 173 | * @param source 数据源 174 | * @param target 目标 175 | * @param 数据源类型 176 | * @param 目标类型 177 | * @return 返回一个新的R类型的Json字符串 178 | */ 179 | public static String toJsonBean(@NonNull T source, @NonNull Class target) { 180 | return toJsonBean(source, target, null); 181 | } 182 | 183 | /** 184 | * 映射一个新的JSON字符串实例 185 | * @param source 数据源 186 | * @param target 目标 187 | * @param biConsumer 数据源字段与目标字段不同,可通过手动映射 188 | * @param 数据源类型 189 | * @param 目标类型 190 | * @return 返回R类型的Json字符串 191 | */ 192 | public static String toJsonBean(@NonNull T source, @NonNull Class target, BiConsumer biConsumer) { 193 | Assert.notNull(source, "Source must not be null"); 194 | Assert.notNull(target, "target must not be null"); 195 | return JSON.toJSONString(toBean(source, target, biConsumer)); 196 | } 197 | 198 | /** 199 | * 映射一个新的Json字符串List 200 | * @param source 数据源 201 | * @param target 目标 202 | * @param 数据源类型 203 | * @param 目标类型 204 | * @return 返回R类型的实例List集合的Json字符串 205 | */ 206 | public static String toJsonList(@NonNull Collection source, @NonNull Class target) { 207 | return toJsonList(source, target, null); 208 | } 209 | 210 | /** 211 | * 映射一个新的Json字符串List 212 | * @param source 数据源 213 | * @param target 目标 214 | * @param biConsumer 数据源字段与目标字段不同,可通过手动映射 215 | * @param 数据源类型 216 | * @param 目标类型 217 | * @return 返回R类型的实例List集合的Json字符串 218 | */ 219 | public static String toJsonList(@NonNull Collection source, @NonNull Class target, BiConsumer biConsumer) { 220 | Assert.notNull(source, "Source must not be null"); 221 | Assert.notNull(target, "target must not be null"); 222 | List targetCollection = new ArrayList<>(); 223 | getInstanceCollection(targetCollection, source, target, biConsumer); 224 | return JSON.toJSONString(targetCollection); 225 | } 226 | 227 | /** 228 | * 映射一个新的范围JsonList 229 | * @param source 数据源 230 | * @param target 目标 231 | * @param skip 跳过 232 | * @param 数据源类型 233 | * @param 目标类型 234 | * @return 返回R类型范围的实例JsonList集合 235 | */ 236 | public static String toJsonListRange(@NonNull List source, @NonNull Class target, @NonNull int skip) { 237 | return toJsonListRange(source, target, skip, source.size()); 238 | } 239 | 240 | /** 241 | * 映射一个新的范围JsonList 242 | * @param source 数据源 243 | * @param target 目标 244 | * @param skip 跳过 245 | * @param biConsumer biConsumer 数据源字段与目标字段不同,可通过手动映射 246 | * @param 数据源类型 247 | * @param 目标类型 248 | * @return 返回R类型范围的实例JsonList集合 249 | */ 250 | public static String toJsonListRange(@NonNull List source, @NonNull Class target, @NonNull int skip, BiConsumer biConsumer) { 251 | return toJsonListRange(source, target, skip, source.size(), biConsumer); 252 | } 253 | 254 | /** 255 | * 映射一个新的范围JsonList 256 | * @param source 数据源 257 | * @param target 目标 258 | * @param skip 跳过 259 | * @param limit 截止 260 | * @param 数据源类型 261 | * @param 目标类型 262 | * @return 返回R类型范围的实例JsonList集合 263 | */ 264 | public static String toJsonListRange(@NonNull List source, @NonNull Class target, @NonNull int skip, @NonNull int limit) { 265 | return toJsonListRange(source, target, skip, limit, null); 266 | } 267 | 268 | /** 269 | * 映射一个新的范围JsonList 270 | * @param source 数据源 271 | * @param target 目标 272 | * @param skip 跳过 273 | * @param limit 截止(包含) 274 | * @param biConsumer biConsumer 数据源字段与目标字段不同,可通过手动映射 275 | * @param 数据源类型 276 | * @param 目标类型 277 | * @return 返回R类型范围的实例JsonList集合 278 | */ 279 | public static String toJsonListRange(@NonNull List source, @NonNull Class target, @NonNull int skip, @NonNull int limit, BiConsumer biConsumer) { 280 | return JSON.toJSONString(toListRange(source, target, skip, limit, biConsumer)); 281 | } 282 | 283 | /** 284 | * 映射一个新的Json字符串Set 285 | * @param source 数据源 286 | * @param target 目标 287 | * @param 数据源类型 288 | * @param 目标类型 289 | * @return 返回R类型的实例Set集合的Json字符串 290 | */ 291 | public static String toJsonSet(@NonNull Collection source, @NonNull Class target) { 292 | return toJsonSet(source, target, null); 293 | } 294 | 295 | /** 296 | * 映射一个新的Json字符串Set 297 | * @param source 数据源 298 | * @param target 目标 299 | * @param biConsumer biConsumer 数据源字段与目标字段不同,可通过手动映射 300 | * @param 数据源类型 301 | * @param 目标类型 302 | * @return 返回R类型的实例Set集合的Json字符串 303 | */ 304 | public static String toJsonSet(@NonNull Collection source, @NonNull Class target, BiConsumer biConsumer) { 305 | Assert.notNull(source, "Source must not be null"); 306 | Assert.notNull(target, "target must not be null"); 307 | Set targetCollection = new HashSet<>(); 308 | getInstanceCollection(targetCollection, source, target, biConsumer); 309 | return JSON.toJSONString(targetCollection); 310 | } 311 | } 312 | -------------------------------------------------------------------------------- /src/test/java/BeanMappingDemo.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | import org.map.utils.BeanMapping; 3 | 4 | import java.time.LocalDateTime; 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | /** 9 | * @author Crazy.X 10 | */ 11 | public class BeanMappingDemo { 12 | 13 | /** 14 | * 自动映射相同对象字段 15 | * 返回实例对象 16 | */ 17 | @Test 18 | public void toBeanTest() { 19 | Student student = new Student("kobe", 18, "男", LocalDateTime.now()); 20 | Man man1 = BeanMapping.toBean(student, Man.class); 21 | Man man2 = BeanMapping.toBean(student, Man.class, (s, t) -> { 22 | t.setGender(s.getSex()); 23 | t.setAge(s.getAge().toString()); 24 | }); 25 | System.out.println(man1); 26 | System.out.println(man2); 27 | } 28 | 29 | /** 30 | * 自动映射相同对象字段 31 | * 手动弥补不同字段 32 | * 返回实例对象 33 | */ 34 | @Test 35 | public void toJsonBeanTest() { 36 | Student student = new Student("kobe", 18, "男", LocalDateTime.now()); 37 | String s1 = BeanMapping.toJsonBean(student, Man.class); 38 | String s2 = BeanMapping.toJsonBean(student, Man.class, (s, t) -> t.setGender(s.getSex())); 39 | System.out.println(s1); 40 | System.out.println(s2); 41 | } 42 | 43 | /** 44 | * 自动映射相同对象字段 45 | * 手动弥补不同字段 46 | * 返回实例对象List 47 | */ 48 | @Test 49 | public void toListTest() { 50 | Student s1 = new Student("kobe", 18, "男", LocalDateTime.now()); 51 | Student s2 = new Student("james", 18, "男", LocalDateTime.now()); 52 | List men1 = BeanMapping.toList(Arrays.asList(s1, s2), Man.class); 53 | List men2 = BeanMapping.toList(Arrays.asList(s1, s2), Man.class, (s, t) -> t.setAge(String.valueOf(s.getAge()))); 54 | System.out.println(men1); 55 | System.out.println(men2); 56 | } 57 | 58 | /** 59 | * 自动映射相同对象字段 60 | * 手动弥补不同字段 61 | * 返回实例对象List 62 | */ 63 | @Test 64 | public void toJsonListTest() { 65 | Student s1 = new Student("kobe", 18, "男", LocalDateTime.now()); 66 | Student s2 = new Student("james", 18, "男", LocalDateTime.now()); 67 | String s3 = BeanMapping.toJsonList(Arrays.asList(s1, s2), Man.class); 68 | String s4 = BeanMapping.toJsonList(Arrays.asList(s1, s2), Man.class, (s, t) -> t.setAge(String.valueOf(s.getAge()))); 69 | System.out.println(s3); 70 | System.out.println(s4); 71 | } 72 | 73 | 74 | /** 75 | * 自动映射相同对象字段 76 | * 手动弥补不同字段 77 | * 返回实例对象Set 78 | */ 79 | @Test 80 | public void fun4() { 81 | Student s1 = new Student("kobe", 28, "男", LocalDateTime.now()); 82 | Student s2 = new Student("james", 18, "男", LocalDateTime.now()); 83 | Student s3 = new Student("kobe", 28, "男", LocalDateTime.now()); 84 | String s4 = BeanMapping.toJsonSet(Arrays.asList(s1, s2, s3), Man.class); 85 | String s5 = BeanMapping.toJsonSet(Arrays.asList(s1, s2, s3), Man.class, (s, t) -> t.setAge(String.valueOf(s.getAge()))); 86 | System.out.println(s4); 87 | System.out.println(s5); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/test/java/Man.java: -------------------------------------------------------------------------------- 1 | import lombok.Data; 2 | 3 | import java.io.Serializable; 4 | import java.time.LocalDateTime; 5 | 6 | /** 7 | * @author Crazy.X 8 | */ 9 | @Data 10 | public class Man implements Serializable { 11 | private static final long serialVersionUID = 1L; 12 | 13 | private String userName; 14 | 15 | private String age; 16 | 17 | private String gender; 18 | 19 | private LocalDateTime localDateTime; 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/Student.java: -------------------------------------------------------------------------------- 1 | import lombok.AllArgsConstructor; 2 | import lombok.Data; 3 | 4 | import java.io.Serializable; 5 | import java.time.LocalDateTime; 6 | 7 | /** 8 | * @author Crazy.X 9 | */ 10 | @Data 11 | @AllArgsConstructor 12 | public class Student implements Serializable { 13 | 14 | private static final long serialVersionUID = 1L; 15 | 16 | private String userName; 17 | 18 | private Integer age; 19 | 20 | private String sex; 21 | 22 | private LocalDateTime localDateTime; 23 | } 24 | --------------------------------------------------------------------------------