Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use BeanUtils in Java

Shulou Source: shulou.com Published: 2022-06-02 18:47:18 10月01日 Update

This article mainly introduces how to use BeanUtils in Java. It is very detailed and has a certain reference value. Friends who are interested must finish it!

Steps for using BeanUtils

Step 1: import the commons-beanutils.jar package into the project

Step 2: some operations related to javaBean can be used in the project, such as:

1)。 Assign values to objects: BeanUtils.setProperty (object, property name, property value)

2)。 Copy the properties of one javaBean to another javaBean object: BeanUtils.copyProperties (source object, new object)

...

Common methods of BeanUtils

Common method description copyProperties (Object dest, Object orig) object copy copyProperty (Object bean, String name, Object value) specified attribute copied to specified object Clone populate (Object bean, Map properties) of cloneBean (Object bean) object copy map data into javabean (key of map should be the same as javabean's property name) setProperty (Object bean, String name, Object value) sets the value of the specified property

Case

1. Create a student entity class (Student.java)

Public class Student {

Private String name; private int age; private String sex; public Student () {

} getXxx and setXxx omit. }

two。 Test common usage of BeanUtils

@ Test

Public void BeanUtilsTest () throws Exception {

Map map = new HashMap ()

Map.put ("name", "Zhang San")

Map.put ("age", 12)

Map.put ("sex", "male")

/ / copy map data to Student

Student stu= new Student ()

BeanUtils.populate (stu, map)

/ / Student [name= Zhang San, age=12, sex= male]

System.out.println (stu)

/ / copy of object

Student stu1 = new Student ()

BeanUtils.copyProperties (stu, stu1)

/ / Student [name= Zhang San, age=12, sex= male]

System.out.println (stu1)

/ / copy the specified attributes

Student stu2 = new Student ()

BeanUtils.copyProperty (stu2, "name", "Li Si")

/ / Student [name= Li Si, age=0, sex=null]

System.out.println (stu2)

/ / set the specified properties

BeanUtils.setProperty (stu2, "sex", "female")

/ / Student [name= Li Si, age=0, sex= female]

System.out.println ((stu2)

/ / Clone object

Object object = BeanUtils.cloneBean (stu2)

/ / Animal [name= Li Si, age=0, sex= female]

System.out.println (object)

}

Note: when copying data, BeanUtils will directly overwrite it regardless of whether the data has a value or not, so that some requirements cannot be met. For example, when the value of the copy is null, overwriting is not allowed, and you can do the following:

Public static String [] getNullPropertyNames (Object source) {final BeanWrapper src = new BeanWrapperImpl (source); java.beans.PropertyDescriptor [] pds = src.getPropertyDescriptors (); Set emptyNames = new HashSet (); for (java.beans.PropertyDescriptor pd: pds) {Object srcValue = src.getPropertyValue (pd.getName ()); if (srcValue = = null) emptyNames.add (pd.getName ()) } String [] result = new String [emptyNames.size ()]; return emptyNames.toArray (result);}

BeanUtils.copyProperties (source object, new object, getNullPropertyNames (source object))

The above is all the contents of the article "how to use BeanUtils in Java". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Object copy attribute data Li Si step Zhang San content Common use method Article Project consistency rather item value interest name Student entity Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Xiaomi Docker vpn Shulou Tech Info