package com.yjp.util;import com.DozerBeanMapperBuilder;
import com.Mapper;
import com.MappingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;import flect.Method;
import java.util.*;/*** Created by yjp on 2018/9/17.*/
public class ObjectMapperUtils {public static Logger logger = Logger(ObjectMapperUtils2.class);private static final Mapper mapper = DozerBeanMapperBuilder.buildDefault();public static <T, S> T convert(S source, T target) throws MappingException {if (source == null) {return null;}Null(target, "target instance required");mapper.map(source, target);return target;}public static <T, S> T convert(S source, Class<T> targetClass) throws MappingException {if (source == null) {return null;}Null(targetClass, "targetClass required");T target = mapper.map(source, targetClass);return target;}public static <T, S> List<T> convert(List<S> source, Class<T> targetClass) throws MappingException {if (source == null) {return null;}List<T> targetList = new ArrayList<T>();Iterator<S> iterator = source.iterator();while (iterator.hasNext()) {T target = mapper.(), targetClass);targetList.add(target);}return targetList;}public static Map<String, Object> objectToMap(Object javaBean) throws Exception {Map<String, Object> map = new HashMap<>();Method[] methods = Class().getMethods(); // 获取所有方法for (Method method : methods) {if (Name().startsWith("get")) {String field = Name(); // 拼接属性名field = field.substring(field.indexOf("get") + 3);field = LowerCase().charAt(0) + field.substring(1);Object value = method.invoke(javaBean, (Object[]) null); // 执行方法map.put(field, value);}}return map;}}
本文发布于:2024-02-03 06:32:02,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170691312249263.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |