zoukankan      html  css  js  c++  java
  • 使用beanUtils操纵javabean

    1.Person类

    package cn.itcast.beanutils;

    import java.util.Date;

    public class Person { //javabean封装用户数据
    private String name;//字段
    private String password;//字段
    private int age;//字段
    private Date birthday;
    public String getAb() {//属性
    return null;
    }
    public String getName() {//属性
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    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 Date getBirthday() {
    return birthday;
    }
    public void setBirthday(Date birthday) {
    this.birthday = birthday;
    }

    }

    2.Demo1类

    package cn.itcast.beanutils;

    import java.lang.reflect.InvocationTargetException;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    import org.apache.commons.beanutils.BeanUtils;
    import org.apache.commons.beanutils.ConversionException;
    import org.apache.commons.beanutils.ConvertUtils;
    import org.apache.commons.beanutils.Converter;
    import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
    import org.junit.Test;

    //使用beanUtils操作bean的属性(第三方)
    public class Demo1 {

    @Test
    public void test1() throws IllegalAccessException,
    InvocationTargetException {

    Person p = new Person();
    BeanUtils.setProperty(p, "name", "xcc");

    System.out.println(p.getName());
    }

    //下面的代码是有问题的,因为beanUtils框架只支持基本数据类型转换
    @Test
    public void test2() throws IllegalAccessException,
    InvocationTargetException {

    String name = "aaaa";
    String password = "123";
    String age = "34";
    String birthday = "1980-09-09";

    Person p = new Person();
    BeanUtils.setProperty(p, "name", name);
    BeanUtils.setProperty(p, "password", password);
    BeanUtils.setProperty(p, "age", age);// 只支持8种基本数据类型
    BeanUtils.setProperty(p, "birthday", birthday);

    System.out.println(p.getName());
    System.out.println(p.getPassword());
    System.out.println(p.getAge());
    System.out.println(p.getBirthday());
    }

    @Test
    public void test3() throws IllegalAccessException,
    InvocationTargetException {

    String name = "aaaa";
    String password = "123";
    String age = "34";
    String birthday = "1980-09-09";

    // 为了让日期赋到bean的birthday属性上,我们给beanUtils注册一个日期转换器
    ConvertUtils.register(new Converter() {
    @Override
    public Object convert(Class type, Object value) {
    if (value == null)
    return null;

    if (!(value instanceof String))
    throw new ConversionException("只支持string类型的转换");
    String str = (String) value;
    if (str.trim().equals(""))
    return null;

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    try {
    return df.parse(str);
    } catch (ParseException e) {
    throw new RuntimeException(e);// 异常链不能断
    }
    }
    }, Date.class);

    Person p = new Person();
    BeanUtils.setProperty(p, "name", name);
    BeanUtils.setProperty(p, "password", password);
    BeanUtils.setProperty(p, "age", age);// 只支持8种基本数据类型
    BeanUtils.setProperty(p, "birthday", birthday);

    System.out.println(p.getName());
    System.out.println(p.getPassword());
    System.out.println(p.getAge());
    System.out.println(p.getBirthday());

    }

    @Test
    public void test4() throws IllegalAccessException,
    InvocationTargetException {

    String name = "aaaa";
    String password = "123";
    String age = "34";
    String birthday = "1980-09-09";

    // 为了让日期赋到bean的birthday属性上,我们给beanUtils注册一个日期转换器
    ConvertUtils.register(new DateLocaleConverter(), Date.class);

    Person p = new Person();
    BeanUtils.setProperty(p, "name", name);
    BeanUtils.setProperty(p, "password", password);
    BeanUtils.setProperty(p, "age", age);// 只支持8种基本数据类型
    BeanUtils.setProperty(p, "birthday", birthday);

    System.out.println(p.getName());
    System.out.println(p.getPassword());
    System.out.println(p.getAge());
    System.out.println(p.getBirthday());

    }

    public void test5() throws IllegalAccessException, InvocationTargetException{
    Map map=new HashMap();
    map.put("name", "aaa");
    map.put("password", "123");
    map.put("age", "23");
    map.put("birthday", "1980-09-09");
    // 为了让日期赋到bean的birthday属性上,我们给beanUtils注册一个日期转换器
    ConvertUtils.register(new DateLocaleConverter(), Date.class);
    Person bean=new Person();
    BeanUtils.populate(bean, map);//用map集合中的值,填充bean的属性

    System.out.println(bean.getName());
    System.out.println(bean.getPassword());
    System.out.println(bean.getAge());
    System.out.println(bean.getBirthday());



    }
    }

  • 相关阅读:
    能让你少写1000行代码的20个正则表达式
    无法识别特性“configProtectionProvider”的解决方案
    C# 对 App.config的appSettings节点数据进行加密
    SQL数据库分配权限
    在C#项目中需要用double类型操作MSSQL float类型数据(附C#数据类型和SQL数据类型对照)
    Linux一键安装web环境全攻略phpstudy版
    阿里云linux服务器到期后续费,网站打不开解决方法之一
    onethink上传到服务器(或者迁移)后台登录验证码错误问题
    PHPCMS网站迁移过程后,添加内容 报500错误解决方案
    css3 media媒体查询器用法总结
  • 原文地址:https://www.cnblogs.com/xiaohuihui123/p/4359055.html
Copyright © 2011-2022 走看看