方法 | 作用 |
public static String getProperty(Object obj,String name) | 获取obj对象的name变量的值 |
public static void setProperty(Object obj,String name,Object value) | 将value的值,赋值给obj对象的name变量 |
public static void populate(Object obj, Map |
将Map集合的值,赋值给obj对象(键为变量的名字,值为变量的值) |
User.java
/**
* @Title: User
* @Description:标准的JavaBean类
* @date 2018年7月21日
*/
public class User {
private String id;
private String username;
private String password;
public User() {
super();
}
public User(String id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
get/set方法...(必须有)
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
}
}
TestBeanUtilsGetProperty.java
import org.apache.commons.beanutils.BeanUtils;
public class TestBeanUtilsGetProperty {
public static void main(String[] args) throws Exception {
User user = new User("it001","liuliu","123456");
String id = BeanUtils.getProperty(user, "id");
String username = BeanUtils.getProperty(user, "username");
String password = BeanUtils.getProperty(user, "password");
System.out.println(id+"\t"+username+"\t"+password);
}
}
import org.apache.commons.beanutils.BeanUtils;
public class TestBeanUtilsSetProperty {
public static void main(String[] args) throws Exception {
User user = new User();
BeanUtils.setProperty(user, "id", "it001");
BeanUtils.setProperty(user, "username", "lf");
BeanUtils.setProperty(user, "password", "123456");
System.out.println(user);
}
}
User.java
public class User {
private String name;
private String age;
private String [] hobbies;
public User() {
super();
}
public User(String name, String age, String[] hobbies) {
super();
this.name = name;
this.age = age;
this.hobbies = hobbies;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String[] getHobbies() {
return hobbies;
}
public void setHobbies(String[] hobbies) {
this.hobbies = hobbies;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", hobbies=" + Arrays.toString(hobbies) + "]";
}
}
TestBeanUtilsPopulate.java
import java.util.HashMap;
import org.apache.commons.beanutils.BeanUtils;
public class TestBeanUtilsPopulate {
public static void main(String[] args) throws Exception {
// 1.创建User对象
User user = new User();
// 2.创建集合
HashMap hm = new HashMap();
// 3.向集合中添加元素
hm.put("name", new String[] { "张三" });
hm.put("age", new String[] { "35" });
hm.put("hobbies", new String[] { "抽烟", "喝酒", "烫头" });
// 4.调用方法给对象赋值
BeanUtils.populate(user, hm);
// 5.打印
System.out.println(user);
}
}
MyBeanUtils.java
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
public class MyBeanUtils {
public static Object populate(Class> clazz, Map properties) {
Object obj = null;
try {
// 创建类对象
obj = clazz.newInstance();
// 调用方法给对象赋值
BeanUtils.populate(obj, properties);
} catch (Exception e) {
// 发生异常,直接挂掉
// 把编译时异常转换成运行时异常
throw new RuntimeException();
}
// 将赋值之后的对象返回
return obj;
}
}
TestMyBeanUtilsPopulate.java
import java.util.HashMap;
public class TestMyBeanUtilsPopulate {
public static void main(String[] args) {
// 1.创建集合
HashMap hm = new HashMap();
// 2.向集合中添加元素
hm.put("name", new String[] { "张三" });
hm.put("age", new String[] { "35" });
hm.put("hobbies", new String[] { "抽烟", "喝酒", "烫头" });
// 3.调用方法给对象赋值
User user = (User) MyBeanUtils.populate(User.class, hm);
// 4.打印
System.out.println(user);
}
}
data.xml
User.java
package pack1;
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 + "]";
}
}
Person.java
package pack2;
public class Person {
private String name;
private String age;
public Person() {
super();
}
public Person(String name, String age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
TestDemo.java
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class TestDemo {
public static void main(String[] args) throws Exception {
// 1.解析XML文件
// 1.1创建SAXReader对象
SAXReader sr = new SAXReader();
// 1.2将XML文件读取到内存
Document document = sr.read("data.xml");
// 1.3获取根元素(beans)
Element rootElement = document.getRootElement();
// 1.4获取beans子元素
List elements = rootElement.elements();
for (Element element : elements) {
// 1.5获取bean的属性值
String className = element.attributeValue("className");
// 2.创建相关类的对象
Class> clazz = Class.forName(className);
Object obj = clazz.newInstance();
// 3.给对象的变量赋值
// 3.1获取bean的子元素property
List property = element.elements();
for (Element e : property) {
// 3.2获取property属性name的值
String name = e.attributeValue("name");
// 3.3获取property属性value的值
String value = e.attributeValue("value");
// 3.3给name变量赋值成value
BeanUtils.setProperty(obj, name, value);
}
// 4.打印对象
System.out.println(obj);
}
}
}
输出结果: