BeanUtils组件
生活随笔
收集整理的這篇文章主要介紹了
BeanUtils组件
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
程序中對(duì)javabean的操作很頻繁, 所以apache提供了一套開源的api,方便對(duì)javabean的操作!即BeanUtils組件。
BeanUtils組件, ?作用是簡(jiǎn)化javabean的操作!
用戶可以從www.apache.org下載BeanUtils組件,然后再在項(xiàng)目中引入jar文件!
使用BenUtils組件:
?
如果缺少日志jar文件,報(bào)錯(cuò):
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. 對(duì)javabean的基本操作 @Testpublic void test1() throws Exception {// a. 基本操作Admin admin = new Admin(); // admin.setUserName("Jack"); // admin.setPwd("999");// b. BeanUtils組件實(shí)現(xiàn)對(duì)象屬性的拷貝BeanUtils.copyProperty(admin, "userName", "jack");BeanUtils.setProperty(admin, "age", 18);// 總結(jié)1: 對(duì)于基本數(shù)據(jù)類型,會(huì)自動(dòng)進(jìn)行類型轉(zhuǎn)換!// c. 對(duì)象的拷貝Admin newAdmin = new Admin();BeanUtils.copyProperties(newAdmin, admin);// d. map數(shù)據(jù),拷貝到對(duì)象中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);// 測(cè)試 System.out.println(adminMap.getUserName());System.out.println(adminMap.getAge());}//2. 自定義日期類型轉(zhuǎn)換器 @Testpublic void test2() throws Exception {// 模擬表單數(shù)據(jù)String name = "jack";String age = "20";String birth = "2016-05-13";// 對(duì)象Admin admin = new Admin();// 注冊(cè)日期類型轉(zhuǎn)換器:1, 自定義的方式ConvertUtils.register(new Converter() {// 轉(zhuǎn)換的內(nèi)部實(shí)現(xiàn)方法,需要重寫 @Overridepublic Object convert(Class type, Object value) {// 判斷if (type != Date.class) {return null;}if (value == null || "".equals(value.toString().trim())) {return null;}try {// 字符串轉(zhuǎn)換為日期SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");return sdf.parse(value.toString());} catch (ParseException e) {throw new RuntimeException(e);}}},Date.class);// 把表單提交的數(shù)據(jù),封裝到對(duì)象中BeanUtils.copyProperty(admin, "userName", name);BeanUtils.copyProperty(admin, "age", age);BeanUtils.copyProperty(admin, "birth", birth);//------ 測(cè)試------ System.out.println(admin);}//2. 使用提供的日期類型轉(zhuǎn)換器工具類 @Testpublic void test3() throws Exception {// 模擬表單數(shù)據(jù)String name = "userName";String age = "20";String birth = null;// 對(duì)象Admin admin = new Admin();// 注冊(cè)日期類型轉(zhuǎn)換器:2, 使用組件提供的轉(zhuǎn)換器工具類ConvertUtils.register(new DateLocaleConverter(), Date.class);// 把表單提交的數(shù)據(jù),封裝到對(duì)象中BeanUtils.copyProperty(admin, "userName", name);BeanUtils.copyProperty(admin, "age", age);BeanUtils.copyProperty(admin, "birth", birth);//------ 測(cè)試------ System.out.println(admin);} } package com.loaderman.demo.a_beans;import java.util.Date;/*** 1. 實(shí)體類設(shè)計(jì)* @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 + "]";}}?
轉(zhuǎn)載于:https://www.cnblogs.com/loaderman/p/10008626.html
總結(jié)
以上是生活随笔為你收集整理的BeanUtils组件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 双系统怎么装成单系统 如何将双系统变成单
- 下一篇: BZOJ 3786: 星系探索 欧拉游览