JAVA中常用的工具类

Published on 2020-12-20 21:15 in 分类: 博客 with 狂盗一枝梅
分类: 博客

1. Map和Bean的相互转化

首先需要引入apache common beanutils maven依赖

<dependency>
	<groupId>commons-beanutils</groupId>
	<artifactId>commons-beanutils</artifactId>
	<version>1.9.2</version>
</dependency>

map2Bean

public static <T> T map2Bean(Map<String, ? extends Object> properties, Class<T> clazz) {
	if (CollectionUtils.isEmpty(properties)) {
		return null;
	}
	try {
		T newInstance = clazz.newInstance();
		BeanUtils.populate(newInstance, properties);
		return newInstance;
	} catch (Exception e) {
		logger.warn("", e);
		return null;
	}
}

bean2Map

public static <T> Map<?, ?> Bean2Map(T t) {
	return new BeanMap(t);
}

#java #工具类
目录