`
Franciswmf
  • 浏览: 797078 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

java基础-02-JavaBean、Final关键字等等

 
阅读更多
【JavaBean】
参考博客:

---- JavaBean是什么
http://blog.csdn.net/tayanxunhua/article/details/19845837

----javabean是什么和总结javabean的两种使用方式06-7-23
http://www.blogjava.net/flysky19/articles/88180.html

【Final关键字】
--JAVA方法中的参数用final来修饰的效果
http://www.cnblogs.com/xiohao/p/4861500.html

【Map】
--Map使用Iterator遍历输出
http://blog.csdn.net/qq_24877569/article/details/52040018

public class BeanToMapTest {
	public static void main(String[] args) {
		Student stu=new Student();
		stu.setGender("男");
		stu.setName("王小明");
		stu.setScore(100);
		stu.setStuNO("200809010003");
		Map<String, Object> map=new HashMap<>(0);
		//bean同步转成map对象
		map=ObjectHandler.convertBeanToMap(stu);
		//遍历map
		Iterator<Map.Entry<String, Object>> it=map.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, Object> entry = it.next();
			System.out.println(entry.getKey()+",值="+entry.getValue());
		}
	}
}

import java.beans.PropertyDescriptor;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ObjectHandler{
    private static Logger log = LoggerFactory.getLogger(ObjectUtils.class);

    public static Map<String, Object> convertBeanToMap(final Object obj) {
        final Map<String, Object> params = new HashMap<String, Object>(0);
        try {
            final PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
            final PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(obj);
            for (int i = 0; i < descriptors.length; i++) {
                final String name = descriptors[i].getName();
                if (!StringUtils.equals(name, "class")) {
                    params.put(name, propertyUtilsBean.getNestedProperty(obj, name));
                }
            }
        }
        catch (final Exception e) {
            log.error("bean转map出现异常" + e);
        }
        return params;
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics