<dependency>
<groupId>commons-beanutilsgroupId>
<artifactId>commons-beanutilsartifactId>
<version>1.9.3version>
dependency>
注意
org.apache.commons.beanutils.BeanUtils 不推荐使用
copyProperties(final Object dest, final Object orig) 复制属性
copyProperty(final Object bean, final String name, final Object value) 给对象指定属性赋值
org.springframework.beans.BeanUtils 推荐使用
copyProperties(Object source, Object target) 复制属性
org.apache.commons.beanutils.ConvertUtils 推荐使用
String convert(final Object value) 将数据转为字符串类型
Object convert(final Object value, final Class> targetType) 将对象转为指定类型
存在问题:org.apache.commons.beanutils.ConversionException: No value specified for 'Date'
使用前添加注册器 ConvertUtils.register(new DateConverter(null), Date.class);
package com.yl.common.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.BigDecimalConverter;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.beanutils.converters.DoubleConverter;
import org.apache.commons.beanutils.converters.IntegerConverter;
import org.apache.commons.beanutils.converters.LongConverter;
import org.apache.commons.beanutils.converters.ShortConverter;
import org.springframework.beans.BeanUtils;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.util.*;
/**
* BeanUtil
*
* @author liuxb
* @date 2021/12/16 12:27
*/
@Slf4j
public class BeanUtil {
/**
* 解决org.apache.commons.beanutils.ConversionException: No value specified for 'Date'
*/
static {
ConvertUtils.register(new LongConverter(null), Long.class);
ConvertUtils.register(new ShortConverter(null), Short.class);
ConvertUtils.register(new IntegerConverter(null), Integer.class);
ConvertUtils.register(new DoubleConverter(null), Double.class);
ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
ConvertUtils.register(new DateConverter(null), Date.class);
}
/**
* 实体类对象之间相同属性名值copy
*
* @param source 原类对象
* @param target 目标类
* @return 返回目标对象
*/
public static <T> T sourceToTarget(Object source, Class<T> target) {
if (source == null) {
return null;
}
T targetObject = null;
try {
targetObject = target.newInstance();
BeanUtils.copyProperties(source, targetObject);
} catch (Exception e) {
log.error("convert error ", e);
}
return targetObject;
}
/**
* 将列表转为指定类的列表
* @param sourceList
* @param target 目标类
* @param
* @return
*/
public static <T> List<T> sourceToTarget(Collection<?> sourceList, Class<T> target){
if(sourceList == null){
return null;
}
List targetList = new ArrayList<>(sourceList.size());
try {
for(Object source : sourceList){
T targetObject = target.newInstance();
BeanUtils.copyProperties(source, targetObject);
targetList.add(targetObject);
}
}catch (Exception e){
log.error("convert error ", e);
}
return targetList;
}
/**
* 将实体类对象属性添加到map中
*
* @param t
* @param
* @return 返回map
*/
public static <T> Map<String, Object> beanToMap(T t){
if (t == null) {
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
try {
Field[] declaredFields = t.getClass().getDeclaredFields();
for (Field field : declaredFields) {
int mod = field.getModifiers();
if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
continue;
}
field.setAccessible(true);
map.put(field.getName(), field.get(t));
}
}catch (Exception e) {
throw new RuntimeException("添加到map异常", e);
}
return map;
}
/**
* 将对象属性添加到已有的map中
*
* @param t
* @param map map不能为null
* @param isCovered 是否可以覆盖添加map已存在key
* @param
* @throws IllegalAccessException
*/
public static <T> void copyPropertiesBeanToMap(T t, Map<String, Object> map, boolean isCovered) {
if (map == null) {
throw new RuntimeException("map不能为空");
}
Field[] fields = t.getClass().getDeclaredFields();
try {
for (Field field : fields) {
int mod = field.getModifiers();
if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
continue;
}
field.setAccessible(true);
if (map.containsKey(field.getName())) {
//对象属性 set 到 map中
if (isCovered) {
map.put(field.getName(), field.get(t));
}
} else {
//对象属性 set 到 map中
map.put(field.getName(), field.get(t));
}
}
} catch (Exception e) {
throw new RuntimeException("添加到map异常", e);
}
}
/**
* 将map中相同属性名的值copy到实体类中
*
* @param map
* @param clazz
* @param
* @return
* @throws Exception
*/
public static <T> T copyPropertiesMapToBean(Map<String, Object> map, Class<T> clazz) {
if (map == null) {
return null;
}
T obj = null;
try {
obj = clazz.newInstance();
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
int mod = field.getModifiers();
if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
continue;
}
field.setAccessible(true);
//防止类型转换异常,需要将map中值转为field的类型
Object o = ConvertUtils.convert(map.get(field.getName()), field.getType());
//map中相同属性名 set 到 对象属性
field.set(obj, o);
}
} catch (Exception e) {
return null;
}
return obj;
}
/**
* 根据对象,属性获取属性值
*
* @param bean 某个类的实例对象
* @param fieldName 属性名
* @return 属性值
* @throws Exception
*/
public static Object getFieldValue(Object bean, String fieldName) throws Exception {
Class<? extends Object> clazz = bean.getClass();
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
Object value = field.get(bean);
return value;
}
/**
* 给对象指定属性赋值
*
* @param bean 某个类的实例对象
* @param fieldName 属性
* @param value 属性值
* @return
*/
public static void setFieldValue(Object bean, String fieldName, Object value) throws Exception {
Class<? extends Object> clazz = bean.getClass();
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(bean, value);
}
}