BeanUtils组件
生活随笔
收集整理的這篇文章主要介紹了
BeanUtils组件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
程序中對javabean的操作很頻繁, 所以apache提供了一套開源的api,方便對javabean的操作!即BeanUtils組件。
BeanUtils組件, ?作用是簡化javabean的操作!
用戶可以從www.apache.org下載BeanUtils組件,然后再在項目中引入jar文件!
使用BenUtils組件:
?
如果缺少日志jar文件,報錯:
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactoryat org.apache.commons.beanutils.ConvertUtilsBean.<init>(ConvertUtilsBean.java:157)at org.apache.commons.beanutils.BeanUtilsBean.<init>(BeanUtilsBean.java:117)at org.apache.commons.beanutils.BeanUtilsBean$1.initialValue(BeanUtilsBean.java:68)at案例:
package com.loaderman.demo.a_beans; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map;import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.Converter; import org.apache.commons.beanutils.locale.converters.DateLocaleConverter; import org.junit.Test;public class App {//1. 對javabean的基本操作 @Testpublic void test1() throws Exception {// a. 基本操作Admin admin = new Admin(); // admin.setUserName("Jack"); // admin.setPwd("999");// b. BeanUtils組件實現對象屬性的拷貝BeanUtils.copyProperty(admin, "userName", "jack");BeanUtils.setProperty(admin, "age", 18);// 總結1: 對于基本數據類型,會自動進行類型轉換!// c. 對象的拷貝Admin newAdmin = new Admin();BeanUtils.copyProperties(newAdmin, admin);// d. map數據,拷貝到對象中Admin adminMap = new Admin();Map<String,Object> map = new HashMap<String,Object>();map.put("userName", "Jerry");map.put("age", 29);// 注意:map中的key要與javabean的屬性名稱一致 BeanUtils.populate(adminMap, map);// 測試 System.out.println(adminMap.getUserName());System.out.println(adminMap.getAge());}//2. 自定義日期類型轉換器 @Testpublic void test2() throws Exception {// 模擬表單數據String name = "jack";String age = "20";String birth = "2016-05-13";// 對象Admin admin = new Admin();// 注冊日期類型轉換器:1, 自定義的方式ConvertUtils.register(new Converter() {// 轉換的內部實現方法,需要重寫 @Overridepublic Object convert(Class type, Object value) {// 判斷if (type != Date.class) {return null;}if (value == null || "".equals(value.toString().trim())) {return null;}try {// 字符串轉換為日期SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");return sdf.parse(value.toString());} catch (ParseException e) {throw new RuntimeException(e);}}},Date.class);// 把表單提交的數據,封裝到對象中BeanUtils.copyProperty(admin, "userName", name);BeanUtils.copyProperty(admin, "age", age);BeanUtils.copyProperty(admin, "birth", birth);//------ 測試------ System.out.println(admin);}//2. 使用提供的日期類型轉換器工具類 @Testpublic void test3() throws Exception {// 模擬表單數據String name = "userName";String age = "20";String birth = null;// 對象Admin admin = new Admin();// 注冊日期類型轉換器:2, 使用組件提供的轉換器工具類ConvertUtils.register(new DateLocaleConverter(), Date.class);// 把表單提交的數據,封裝到對象中BeanUtils.copyProperty(admin, "userName", name);BeanUtils.copyProperty(admin, "age", age);BeanUtils.copyProperty(admin, "birth", birth);//------ 測試------ System.out.println(admin);} } package com.loaderman.demo.a_beans;import java.util.Date;/*** 1. 實體類設計* @author Jie.Yuan**/ public class Admin {private int id;private String userName;private String pwd;private int age;private Date birth;public Date getBirth() {return birth;}public void setBirth(Date birth) {this.birth = birth;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}@Overridepublic String toString() {return "Admin [age=" + age + ", birth=" + birth + ", id=" + id+ ", pwd=" + pwd + ", userName=" + userName + "]";}}?
轉載于:https://www.cnblogs.com/loaderman/p/10008626.html
總結
以上是生活随笔為你收集整理的BeanUtils组件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 双系统怎么装成单系统 如何将双系统变成单
- 下一篇: BZOJ 3786: 星系探索 欧拉游览