使用了BeanUtils的简单操作

直接获取对象的某个值
et.createCell(BeanUtils.getProperty(o, eh.getFieldName()));

 

简单为对象某个字段赋值
c.setCellValue(BeanUtils.getProperty(datas.get(i),headers.get(j).getFieldName()));

 beanUtils的底层是内省。下面是又一个小例子,实现了map和bean的映射注入

package com.itcast.domain;

public class Student {
    private String username;
    private String password;
    private int age;
    public Student() {
        super();
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Student(String username, String password, int age) {
        super();
        this.username = username;
        this.password = password;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [username=" + username + ", password=" + password
                + ", age=" + age + "]";
    }

}
package com.itcast.test;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;

import com.itcast.domain.Student;
import com.itcast.utils.CommonUtils;
@SuppressWarnings({"rawtypes","unused"})
public class Test01 {
    @Test
    public void fun1() throws Exception{
        
        Class cla=Class.forName("com.itcast.domain.Student");
        Object stu=cla.newInstance();
        
        BeanUtils.setProperty(stu, "username", "guodaxia");
        BeanUtils.setProperty(stu, "password", "12345");
        BeanUtils.setProperty(stu, "age", "21");//这个类会自动进行类型转换注入
        
        //System.out.println(stu);
        
        int age=Integer.valueOf(BeanUtils.getProperty(stu, "age"));//getProperty得到的都是String类型
        System.out.println(age);
    }
    /*
     *把map中的属性直接封装到一个bean中
     *
     * Map:{"username":"zhangsan","password","123"}
     * 我们要把map的数据封装到一个javaBean中,要求map的key域bean的属性名相同!
     */
    @Test
    public void fun2() throws Exception{//将Map的内容直接写入一个bean中
        Map<String,String> map=new HashMap<String,String>();
        map.put("username", "guodaxia");
        map.put("password","961012gz");
        map.put("age", "21");
        
        Student stu=new Student();
        BeanUtils.populate(stu, map);
        System.out.println(stu);
    }
    
    @Test
    public void fun3(){
        Map<String,String> map=new HashMap<String,String>();
        map.put("username", "guodaxia");
        map.put("password","961012gz");
        map.put("age", "21");
        
        Student stu=CommonUtils.toBean(map,Student.class);
        System.out.println(stu);
    }
}
package com.itcast.utils;

import java.util.Map;
import java.util.UUID;

import org.apache.commons.beanutils.BeanUtils;

public class CommonUtils {
    /**
     * 生成不重复的32位长的大写字符串
     * @return
     */
    public static String uuid(){
        return UUID.randomUUID().toString().replace("-", "").toUpperCase();
    }
    
    /**
     *  把map转换成指定类型的javabean对象
     * @param map
     * @param clazz
     * @return
     */
    public static <T> T toBean(Map<String, String> map,Class<T> clazz){
        
        try {
            T bean = clazz.newInstance();
            BeanUtils.populate(bean, map);
            return bean;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        
    }
}

 

posted @ 2016-07-12 11:36  guodaxia  阅读(318)  评论(0编辑  收藏  举报