What is Beanutils
Beanutils mainly provides a variety of operations for JavaBean.
the role of Beanutils
In general writing JavaBean components, you must write the setter and getter methods, of course, if we already know the relevant genus of JavaBean
Sex and method, writing JavaBean is relatively simple, but when there are too many components, repetition is often tedious and tedious to write. But there are times when I
When you need to invoke the properties of a dynamic object, how do we set and get the object's properties? Beanutils can help us to solve this
A problem. It requires support for jakarta-commons-collections packages and jakarta-commons-logging packages.
org.apache.commons.beanutils
This package mainly provides tool classes for manipulating JavaBean, and Jakarta-common-beanutils's main functions are implemented in this package.
Beanutils can directly get and set the value of a property. It divides the property into 3 different types:
1 simple--Simple type, such as STIRNG, Int ...
(For simple types, the second argument is directly a property name, as detailed in the code)
2) indexed--index type, such as array, arrayList ...
(for indexed, the property name [index value] ", note that this can be done in the same way for ArrayList and arrays, as detailed in the code)
3 maped--This needless to say also should know, refers to map, such as HashMap ... (For the map type, it needs to be in the form of "Property name (key value)", as detailed in the code)
accessing different types of data can call functions GetProperty and setproperty directly. They all have 2 parameters, the first is the JavaBean object,
The second is the name of the property to be manipulated.
Example
Person entity Class Code:
Package com.domain;
/** * Person
entity class
* @author Administrator
* @date August 16, 2016 * * Public
class person
{
private String name;
private int age;
private String gender;
Public person () {
super ();
}
Public person (string name, int age, string gender) {
super ();
this.name = name;
This.age = age;
This.gender = gender;
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
public int getage () {return age
;
}
public void Setage (int age) {
this.age = age;
}
Public String Getgender () {return
gender;
}
public void Setgender (String gender) {
this.gender = gender;
}
@Override public
String toString () {return
' person [name=] + name +, age= ' + Age + ', gender= ' + gender + '] ' ;
}
}
User entity Class Code:
Package com.domain;
/** *
User entity class
* @author Administrator
* @date August 16, 2016 * * Public
class User {
private String Username;
private String password;
Public User () {
super ();
}
Public User (string Username, string password) {
super ();
This.username = Username;
This.password = password;
}
Public String GetUserName () {return
username;
}
public void Setusername (String username) {
this.username = username;
}
Public String GetPassword () {return
password;
}
public void SetPassword (String password) {
this.password = password;
}
@Override public
String toString () {return
"User [username=" + Username + ", password=" + password + "]";
}
}
Demo code to use JUnit tests:
Package com.domain;
Import Java.util.HashMap;
Import Java.util.Map;
Import Org.apache.commons.beanutils.BeanUtils;
Import Org.junit.Test;
Import Com.utils.CommonUtils;
public class Demo {@Test the public void Fun1 () throws exception{String className = "Com.domain.Person";
Class Class1 = Class.forName (ClassName);
Object bean = class1.newinstance ();
Beanutils.setproperty (Bean, "name", "Li Sisi");
Beanutils.setproperty (Bean, "age", "19");
Beanutils.setproperty (Bean, "gender", "female");
System.out.println (Bean);
String age = beanutils.getproperty (Beans, "age");
System.out.println (age); /** * The attributes in the map are directly divided into one bean * @throws Exception * @throws Ill/@Test public void fun2 () throws Exception
{map<string, string> Map = new hashmap<string,string> ();
Map.put ("username", "Lian Xin");
Map.put ("Password", "888888");
User user = new user ();
Beanutils.populate (user, map);
SYSTEM.OUT.PRINTLN (user); @Test public void Fun3 () {Map<string, string> map = new hashmap<string,string> ();
Map.put ("username", "Lian Zi");
Map.put ("Password", "666666");
User user = Commonutils.tobean (map, User.class);
SYSTEM.OUT.PRINTLN (user); }
}
Test results:
Subsequent continuous learning will also be updated to increase the current use of these.