Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use BeanUtils in Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report