之前也写过BeanToMap、MapToBean的工具类,但这次算是了结了。
玩MapToBean、BeanToMap少不了反射和内省!
没错,就是你很熟悉的内容。
一、运行展示
1.1 先看JavaBean
1.2 再看运行代码
1.3 运行展示
二、代码解析
三、源代码
3.1 Student.java
package com.rt.pojo;
import java.sql.Date;
public class Student {
private Integer studentId;
private String name;
private Integer age;
private String gender;
private Date birthdate;
private String grade;
public Student(Integer studentId, String name, Integer age, String gender, Date birthdate, String grade) {
this.studentId = studentId;
this.name = name;
this.age = age;
this.gender = gender;
this.birthdate = birthdate;
this.grade = grade;
}
public Student() {
}
public Integer getStudentId() {
return studentId;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthdate() {
return birthdate;
}
public void setBirthdate(Date birthdate) {
this.birthdate = birthdate;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
@Override
public String toString()
{
return "Student{" +
"studentId=" + studentId +
", name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", birthdate=" + birthdate +
", grade='" + grade + '\'' +
'}';
}
}
3.2 BeanUtility.java
package com.rt.util;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class BeanUtility
{
private static final Map<String, List<Field>> fieldListMap;
static{
fieldListMap=new TreeMap<>();
}
private static List<Field> getBeanClassField(Class beanClass) {
if(fieldListMap.containsKey(beanClass.getName()))
{
System.out.println("Leave it alone--------from fieldListMap-------");
return fieldListMap.get(beanClass.getName());
}
List<Field> fieldList = null;
Method writeMethod;
Method readMethod;
Field beanClassField;
String beanClassFieldName;
try {
BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
fieldList = new ArrayList<>();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
readMethod = propertyDescriptor.getReadMethod();
writeMethod = propertyDescriptor.getWriteMethod();
if (readMethod != null && writeMethod != null)
{
beanClassFieldName = propertyDescriptor.getName();
beanClassField = beanClass.getDeclaredField(beanClassFieldName);
beanClassField.setAccessible(true);
fieldList.add(beanClassField);
}
}
} catch (IntrospectionException | NoSuchFieldException e) {
e.printStackTrace();
}
fieldListMap.put(beanClass.getName(),fieldList);
return fieldList;
}
public static <T> T mapToBean(Map<String,Object> beanMap,Class<T> beanClass) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException
{
if(beanMap.isEmpty())
return null;
List<Field> fieldList = getBeanClassField(beanClass);
T beanObject = beanClass.getConstructor().newInstance();
for (Field field : fieldList)
{
field.set(beanObject,beanMap.get(field.getName()));
}
return beanObject;
}
public static <T> Map<String,Object> beanToMap(T beanObject) throws IllegalAccessException {
List<Field> fieldList = getBeanClassField(beanObject.getClass());
Map<String, Object> beanMap = new TreeMap<>();
for (Field field : fieldList)
{
beanMap.put(field.getName(),field.get(beanObject));
}
return beanMap;
}
}
3.3 BeanUtilityTest.java
package com.rt.test;
import com.rt.pojo.Student;
import com.rt.util.BeanUtility;
import org.junit.Test;
import java.sql.Date;
import java.util.Map;
import java.util.Set;
public class BeanUtilityTest {
@Test
public void test1() throws Exception {
Student student = new Student();
student.setStudentId(2022001);
student.setName("杨甜甜");
student.setAge(24);
student.setGender("male");
student.setBirthdate(Date.valueOf("1993-10-03"));
student.setGrade("三年级");
System.out.println("--------------------------beanToMap--------------------------");
Map<String, Object> beanToMap = BeanUtility.beanToMap(student);
Set<Map.Entry<String, Object>> entries = beanToMap.entrySet();
for (Map.Entry<String, Object> entry : entries) {
System.out.println(entry.getKey()+"\t"+entry.getValue());
}
System.out.println("--------------------------mapToBean--------------------------");
Student bean = BeanUtility.mapToBean(beanToMap, student.getClass());
System.out.println(bean);
}
}
四、完结撒花
勤能补拙!熟能生巧!
|