java BeanUtils工具类

第1章    BeanUtils工具类

BeanUtils 是 Apache commons组件的成员之一,主要用于简化JavaBean封装数据的操作。它可以给JavaBean封装一个字符串数据,也可以将一个表单提交的所有数据封装到JavaBean中。

         使用第三方工具,需要导入jar包:

l  供JavaBean User ,并提供对应的构造方法

public class User {

    privateString id;

    privateString username;

    privateString pwd;

    //省略setter和getter

    @Override

    publicString toString() {

        return"User [id=" + id + ", username=" + username + ",pwd=" + pwd + "]";

    }

l  功能1:设置属性

@Test

public void demo01() throws Exception{

    User user= new User();

   

    //设置属性

    BeanUtils.setProperty(user,"id", "u001");

    BeanUtils.setProperty(user,"username", "jack");

    BeanUtils.setProperty(user,"pwd", "1234");

   

   

    System.out.println(user);

   

    /* 结果:

     * User[id=u001, username=jack, pwd=1234]

     */

}

l  功能2:获得属性

@Test

public void demo02() throws Exception{

    User user= new User("u001","jack","1234");

   

    //设置属性

    String id= BeanUtils.getProperty(user, "id");

    Stringusername = BeanUtils.getProperty(user, "username");

   

    System.out.println(id);

    System.out.println(username);

   

    /* 结果:

     * u001

     *  jack

     */

}

l  功能3:封装表单数据,使用Map 模拟 request.getParameterMap()

@Test

public void demo03() throws Exception{

   

    Map<String,String[]> map = new HashMap<>();

    map.put("id",new String[]{"u001"});

    map.put("username",new String[]{"jack"});

    map.put("pwd",new String[]{"1234"});

   

   

    //将数据封装到javabean中

    User user= new User();

    BeanUtils.populate(user,map);

   

    System.out.println(user);

    /* 结果:

     * User[id=u001, username=jack, pwd=1234]

     */

}

l  功能3:编写工具类

public class MyBeanUtils {

   

    /**

     * 将数据封装给JavaBean

     * @param user

     * @param properties

     */

    publicstatic void populate0(Object user, Map<String,String[]> properties){

        try {

            //封装数据

            BeanUtils.populate(user,properties);

        }catch (Exception e) {

            thrownew RuntimeException(e);

        }

    }

    /**

     * 高级封装,不需要new javabean

     * @param beanClass

     * @param properties

     * @return

     */

    publicstatic Object populate1(Class beanClass, Map<String,String[]>properties){

        try {

            //1使用反射创建实例

            Objectbean = beanClass.newInstance();

            //3封装数据

            BeanUtils.populate(bean,properties);

           

            returnbean;

        }catch (Exception e) {

            thrownew RuntimeException(e);

        }

    }

   

l 使用

@Test

publicvoid demo01() throwsException{

    //工具类使用

    Map<String,String[]>properties = new HashMap<String,String[]>();

    properties.put("id", new String[]{"18"});

    properties.put("username", new String[]{"jack"});

    properties.put("pwd", new String[]{"1234"});

   

    User user = new User();

    MyBeanUtils.populate0(user, properties);

   

    System.out.println(user);

   

    /* 结果:

     * User [id=18, username=jack, pwd=1234]

     */

}

 

@Test

publicvoiddemo02() throws Exception{

    //工具类使用

    Map<String,String[]>properties = new HashMap<String,String[]>();

    properties.put("id", new String[]{"18"});

    properties.put("username", new String[]{"jack"});

    properties.put("pwd", new String[]{"1234"});

   

    Object obj = MyBeanUtils.populate1(User.class, properties);

   

    System.out.println(obj);

   

    /* 结果:

     * User [id=18, username=jack, pwd=1234]

     */

}

 

第1章    综合案例

  读取XML中的配置文件信息,使用BeanUtils工具类创建JavaBean对象,将XML中的数据保存到JavaBean类的属性中

@Test

    publicvoid demo01()throws Exception{

        SAXReader sax = new SAXReader();

        //读取XML文档,获取Document对象

        Document document = sax.read("bean.xml");

        //获取根标签 beans

        Element element = document.getRootElement();

        //获取beans根标签下的子标签 <bean>

        List<Element> beanElement = element.elements();

        //遍历集合,获取集合中的bean标签

        for(Element beanEle : beanElement){

            //获取bean标签中的属性className的属性值

            String className = beanEle.attributeValue("className");

            Class clazz = Class.forName(className);

            Object obj = clazz.newInstance();

            //获取bean标签子标签property

            List<Element> proElement = beanEle.elements();

            //遍历集合,获取出子标签

            for(Element proEle : proElement){

                //获取子标签的属性name,value的值

                String name = proEle.attributeValue("name");

                String value = proEle.attributeValue("value");

                BeanUtils.setProperty(obj, name, value);

            }

            System.out.println(obj);

           

        }

    }


猜你喜欢

转载自blog.csdn.net/shitianhang123/article/details/79091234