集册 Java实例教程 映射到Java Bean

映射到Java Bean

欢马劈雪     最近更新时间:2020-01-02 10:19:05

424
映射到Java Bean
/* 来 自 时 代 J a v a - nowjava.com*/


//package com.nowjava;

import java.beans.BeanInfo;

import java.beans.IntrospectionException;

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import java.lang.reflect.InvocationTargetException;


import java.util.Map;


public class Main {

    public static <T> T toJavaBean(Map<String, ?> map,

            Class<T> javaBeanClazz) throws IntrospectionException,

            IllegalAccessException, InstantiationException,

            InvocationTargetException {

        BeanInfo beanInfo = Introspector.getBeanInfo(javaBeanClazz);

        PropertyDescriptor[] propertyDescriptors = beanInfo

                .getPropertyDescriptors();

        T obj = javaBeanClazz.newInstance();

        for (int i = 0; i < propertyDescriptors.length; i++) {

            PropertyDescriptor descriptor = propertyDescriptors[i];//nowjava - 时  代  Java 提供

            String propertyName = descriptor.getName();

            
展开阅读全文