欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Commons beanutils組件簡(jiǎn)介

 更新時(shí)間:2017年09月08日 09:14:03   作者:CharlieChenBlog  
這篇文章主要介紹了commons beanutils組件的相關(guān)內(nèi)容,以及部分實(shí)例和基本用法,需要的朋友可以參考下

        Commons Beanutils是Apache開源組織提供的用于操作JAVA BEAN的工具包。使用commons beanutils,我們可以很方便的對(duì)bean對(duì)象的屬性進(jìn)行操作。今天為大家介紹一下該包的常用方法。

1.什么是BeanUtils

        程序中對(duì)javabean的操作很頻繁, 所以apache提供了一套開源的api,方便對(duì)javabean的操作,即BeanUtils組件。

2.BeanUtils的作用

        簡(jiǎn)化javabean的操作。 

       在一般的寫bean組件的時(shí)候,都必須要寫setter和getter方法,當(dāng)然假如我們事先已經(jīng)知道bean的相關(guān)屬性和方法,寫bean是比較簡(jiǎn)單的。

3.BeanUtils依賴包
        用戶可以從www.apache.org下載BeanUtils組件,然后再在項(xiàng)目中引入jar文件。

(1) BeanUtils相關(guān)包
commons-beanutils-1.8.3.jar
commons-beanutils-1.8.3-javadoc.jar
commons-beanutils-1.8.3-javadoc.jar
commons-beanutils-bean-collections-1.8.3.jar
commons-beanutils-core-1.8.3.jar

(2) Logic4j相關(guān)包
commons-logging.jar
log4j.jar
注:如果缺少日志jar文件,報(bào)錯(cuò):

java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 
at org.apache.commons.beanutils.ConvertUtilsBean.(ConvertUtilsBean.java:157) 
at org.apache.commons.beanutils.BeanUtilsBean.(BeanUtilsBean.java:117) 
at org.apache.commons.beanutils.BeanUtilsBean$1.initialValue(BeanUtilsBean.java:68) 
at

二:實(shí)例—基本用法

用法1: 對(duì)象屬性的拷貝

BeanUtils.copyProperty(admin, "userName", "jack");
BeanUtils.setProperty(admin, "age", 18);

用法2:對(duì)象的拷貝

BeanUtils.copyProperties(newAdmin, admin);

用法3: map數(shù)據(jù)拷貝到j(luò)avabean中
注意:map中的key要與javabean的屬性名稱一致

BeanUtils.populate(adminMap, map);

代碼舉例

javabean類

package com.beanutils.test;
import java.util.Date;
/**
 * 1. bean類設(shè)計(jì)
 * @author Charlie.chen
 *
 */
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;
 }
 @Override
 public String toString() {
 return "Admin [age=" + age + ", birth=" + birth + ", id=" + id
 + ", pwd=" + pwd + ", userName=" + userName + "]";
 } 
}

通過BeanUtils對(duì)javabean的基本操作   

@Test
 public void test1() throws Exception {
 // a. 基本操作
 Admin admin = new Admin();
 //admin.setUserName("Charlie.chen");
 //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());
 }

三:實(shí)例—日期類型的拷貝

需要注冊(cè)日期類型轉(zhuǎn)換器,2種方式參見下面代碼:   

 //1.自定義日期類型轉(zhuǎn)換器
 @Test
 public void test2() throws Exception {
 // 模擬表單數(shù)據(jù)
 String name = "jack";
 String age = "20";
 String birth = " ";
 // 對(duì)象
 Admin admin = new Admin();
 // 注冊(cè)日期類型轉(zhuǎn)換器:1, 自定義的方式
 ConvertUtils.register(new Converter() {
 // 轉(zhuǎn)換的內(nèi)部實(shí)現(xiàn)方法,需要重寫
 @Override
 public 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)換器工具類
 @Test
 public void test3() throws Exception {
 // 模擬表單數(shù)據(jù)
 String name = "jack";
 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);
 }

總結(jié)

        以上所述是小編給大家介紹的Commons BeanUtils組件的相關(guān)內(nèi)容,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Java搭建RabbitMq消息中間件過程詳解

    Java搭建RabbitMq消息中間件過程詳解

    這篇文章主要介紹了Java搭建RabbitMq消息中間件過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • SpringBoot短鏈接跳轉(zhuǎn)的代碼實(shí)現(xiàn)

    SpringBoot短鏈接跳轉(zhuǎn)的代碼實(shí)現(xiàn)

    短鏈跳轉(zhuǎn)是一種通過將長(zhǎng)鏈接轉(zhuǎn)換為短鏈接的方式,以便在互聯(lián)網(wǎng)上進(jìn)行鏈接共享和傳播的技術(shù),短鏈將原始長(zhǎng)鏈接通過特定算法轉(zhuǎn)換為較短的鏈接,使得它更容易分享、傳播和展示,本文給大家介紹了SpringBoot短鏈接跳轉(zhuǎn)的代碼實(shí)現(xiàn),需要的朋友可以參考下
    2024-03-03
  • 關(guān)于Mybatis-plus設(shè)置字段為空的正確寫法

    關(guān)于Mybatis-plus設(shè)置字段為空的正確寫法

    這篇文章主要介紹了關(guān)于Mybatis-plus設(shè)置字段為空的正確寫法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • struts2靜態(tài)資源映射代碼示例

    struts2靜態(tài)資源映射代碼示例

    這篇文章主要介紹了struts2靜態(tài)資源映射的相關(guān)內(nèi)容,涉及了具體代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-09-09
  • SpringBoot2.0+阿里巴巴Sentinel動(dòng)態(tài)限流實(shí)戰(zhàn)(附源碼)

    SpringBoot2.0+阿里巴巴Sentinel動(dòng)態(tài)限流實(shí)戰(zhàn)(附源碼)

    這篇文章主要介紹了SpringBoot2.0+阿里巴巴Sentinel動(dòng)態(tài)限流實(shí)戰(zhàn)(附源碼),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • java實(shí)現(xiàn)基于TCP協(xié)議網(wǎng)絡(luò)socket編程(C/S通信)

    java實(shí)現(xiàn)基于TCP協(xié)議網(wǎng)絡(luò)socket編程(C/S通信)

    這篇文章主要介紹了java實(shí)現(xiàn)基于TCP協(xié)議網(wǎng)絡(luò)socket編程(C/S通信),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • java實(shí)現(xiàn)發(fā)送手機(jī)短信

    java實(shí)現(xiàn)發(fā)送手機(jī)短信

    這篇文章主要介紹了java實(shí)現(xiàn)發(fā)送手機(jī)短信,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-03-03
  • Spring AOP中使用args表達(dá)式的方法示例

    Spring AOP中使用args表達(dá)式的方法示例

    這篇文章主要介紹了Spring AOP中使用args表達(dá)式的方法,結(jié)合實(shí)例形式分析了spring面向切面AOP中使用args表達(dá)式具體步驟、相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下
    2020-01-01
  • Java圖書管理系統(tǒng),課程設(shè)計(jì)必用(源碼+文檔)

    Java圖書管理系統(tǒng),課程設(shè)計(jì)必用(源碼+文檔)

    本系統(tǒng)采用Java,MySQL 作為系統(tǒng)數(shù)據(jù)庫(kù),重點(diǎn)開發(fā)并實(shí)現(xiàn)了系統(tǒng)各個(gè)核心功能模塊,包括采編模塊、典藏模塊、基礎(chǔ)信息模塊、流通模塊、期刊模塊、查詢模塊、評(píng)論模塊、系統(tǒng)統(tǒng)計(jì)模塊以及幫助功能模塊
    2021-06-06
  • Springboot-dubbo-fescar 阿里分布式事務(wù)的實(shí)現(xiàn)方法

    Springboot-dubbo-fescar 阿里分布式事務(wù)的實(shí)現(xiàn)方法

    這篇文章主要介紹了Springboot-dubbo-fescar 阿里分布式事務(wù)的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2019-03-03

最新評(píng)論