BeanUtils tool class of java object-oriented programming 3

Posted by jami3 on Wed, 01 Apr 2020 21:54:40 +0200

BeanUtils

1, Introduction & Overview & Introduction

BeanUtils make it easier and faster to encapsulate JavaBean data.

For example, it can be used in band parameter construction, Set method, reflection.

 

Getting started:

Import jar package:

public static void main(String[] args) throws Exception {
    //Populating Phone objects with data using the BeanUtils tool
    //1. Create Phone object
    Phone p = new Phone();
    //2. Fill in data: setproperty (the object to be filled in, the member variable to be filled in, and the data to be filled in);
    BeanUtils.setProperty(p,"brand","HUAWEI");//p.setBrand("Huawei");
    BeanUtils.setProperty(p,"price",8000.0);
    BeanUtils.setProperty(p,"color","blue");
    //3. Get data: getproperty (which object to get data from and which member variable data to get);
    System.out.println(BeanUtils.getProperty(p,"brand"));// p.getBrand();
}

Be careful:

 

 

 

2, Advanced use: fill JavaBean with Map [key]

The Map collection populates the JavaBean.

Map < string, string [] >

Map<String,Object>

 

  • Fill in simple data:

Class Car:

public class Car implements Serializable {
    private String name;
    private double price;
    //Ignore other methods
}

 

Class Demo:

public static void main(String[] args) throws Exception {
    //Map populating JavaBean s
    //1. Create a Map collection, which stores data
    Map<String, String[]> map = new LinkedHashMap<>();
    //Map key must be consistent with JavaBean's member variables
    map.put("name",new String[]{"red flag"});
    map.put("price",new String[]{"400000"});
    //2. Creating JavaBean objects
    Car car = new Car();
    //3. Fill in data: populate (which object to fill in data, map set);
    BeanUtils.populate(car,map);
    System.out.println(car);
}

 

 

  • Fill in complex data: array

Class Car

public class Car implements Serializable {
    private String name;
    private double price;
    private String[] doors;
    //Ignore other methods
}

 

Class Demo:

public static void main(String[] args) throws Exception {
    //Map populating JavaBean s
    //1. Create a Map collection, which stores data
    Map<String, String[]> map = new LinkedHashMap<>();
    //Map key must be consistent with JavaBean's member variables
    map.put("name",new String[]{"red flag"});
    map.put("price",new String[]{"400000"});
    map.put("doors",new String[]{"Cab door","Auxiliary door","Trunk door"});
    //2. Creating JavaBean objects
    Car car = new Car();
    //3. Fill in data: populate (which object to fill in data, map set);
    BeanUtils.populate(car,map);
    System.out.println(car);
}

 

 

matters needing attention:

 

  1. The key of the Map must be consistent with the name of the member variable (if not, ignore the corresponding member variable and do not fill in it)

2. If data conversion is required during filling: String can be converted to basic data type (integer, decimal, Boolean, character)

3. Member variable is a non array type. If multiple values are filled in, only the first one will be taken

 

 

 

3, Advanced use: Date Converter [key]

Date class: java.util.Date

Map :String =====>Date

Cast:

 

Solution:

Class Person:

public class Person implements Serializable{
    private String name;
    private Date birthday;
    //Other methods ignored
}

 

Class Demo:

public static void main(String[] args) throws Exception {
    //1. Define Map set and save data
    LinkedHashMap<String, String[]> map = new LinkedHashMap<>();
    map.put("name",new String[]{"Zhang San"});
    map.put("birthday",new String[]{"2001-10-01"});
    //2. Create Person object
    Person p = new Person();

    //Register a date converter and bind java.util.Date with the date converter
    //1. Create Date Converter
    DateConverter dc = new DateConverter();
    //2. Format match
    dc.setPatterns(new String[]{"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss","yyyy/MM/dd"});
    //3. Binding: java.util.Date and Date Converter
    //register registration / binding
    ConvertUtils.register(dc,java.util.Date.class);
    //3. Fill in data
    BeanUtils.populate(p,map);
    System.out.println(p);
}

 

  1. Set the matching format
  2. Must bind before populating data

 

 

 

4, Extended use: Custom MyBeanUtils

Define a Java class: MyBeanUtils

Requires writing a method: populate

1. The method is required to be static and can be called directly through the class name.

2. Define the generic T on the method.

3. The return value type is required to be T type

4. Method parameter: class < T > object

Map < string, string [] > object

5. Create the JavaBean instance object through the Class object passed in, and fill the data in the Map into the JavaBean.

Finally, return the JavaBean object filled with data

 

Code implementation:

/**
 *  Fill data method
 * @param clazz     Class object of JavaBean to be filled
 * @param map       Save the data to be filled
 * @param <T>       return type
 * @return          Return the JavaBean object filled with data
 * @throws Exception    Exception not handled internally, all thrown
 */
public static <T> T populate(Class<T> clazz,Map<String,String[]> map) throws Exception {
    T t = null;
    //Create the JavaBean object passed in to clazz
    t = clazz.newInstance();
    //Fill data (Date Converter)
    //Bind Date Converter
    DateConverter dc = new DateConverter();
    dc.setPatterns(new String[]{"yyyy-MM-dd"});
    ConvertUtils.register(dc,java.util.Date.class);
    //Fill data
    BeanUtils.populate(t,map);
    //Return JavaBean object
    return t;
}

 

 

 

Today's summary:

BeanUtils: to simplify the encapsulation of JavaBean data

 

Setproperty (object, property name, property value)//

Getproperty (object, "property name");

Populate (object, map);

 

Map<String,String[]>

Map<String,Object>

 

  1. The name of a JavaBean's member variable must be consistent with the Map's key

2. String ======= Date set Date converter

 

Date Converter settings: must be set before filling

 

//1. Create Date Converter

DateConverter  dc = new DateConverter();

//2. Format match

dc.setPatterns(new String[]{"yyyy-MM-dd"});

//3. Binding Date Converter with java.util.Date

ConvertUtils.register(dc,java.util.Date.class);

​​​​​​​

 

 

 

 

Please give yourself a compliment!

Make a little progress every day`~~~~~

​​​​​​​

Topics: Programming Java