日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

PropertyUtils的使用

發布時間:2024/8/1 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PropertyUtils的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言:本博客內容由張孝祥Java高新技術整理而來


在上節:對javaBean簡單的內省操作?中我們寫了兩個靜態的方法,用來獲取和賦值javaBean屬性。其實這些apache早已經幫我們封裝好了,我們直接使用即可。


準備工作:兩個jar包

1.commons-beanutils.jar

2.commons-logging.jar


直接上實例


po實體類


package com.dao.chu.movie;public class Student {private int id;private String name;private int age;public Student() {}public Student(int id, String name, int age) {super();this.id = id;this.name = name;this.age = age;}public int getId() {return id;}public void setId(int id) {this.id = id;}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;}}

vo實體類


package com.dao.chu.movie;public class StudentVo {private int id;private String name;private int age;private float grade;@Overridepublic String toString() {return "StudentVo [id=" + id + ", name=" + name + ", age=" + age+ ", grade=" + grade + "]";}public int getId() {return id;}public void setId(int id) {this.id = id;}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 float getGrade() {return grade;}public void setGrade(float grade) {this.grade = grade;}}

測試類

package com.dao.chu.movie;import java.beans.IntrospectionException; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map;import org.apache.commons.beanutils.PropertyUtils;public class PropertyUtilTest {@SuppressWarnings("unchecked") public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, NoSuchMethodException {Student student = new Student(1, "張三", 18);StudentVo studentVo = new StudentVo();//設置屬性 同方法 BeanUtils.setPropertyPropertyUtils.setProperty(student, "name", "張三三");//獲取屬性 同方法 BeanUtils.getPropertyObject name = PropertyUtils.getProperty(student, "name");System.out.println("getName is:"+name);//vo po轉換System.out.println(studentVo);PropertyUtils.copyProperties(studentVo, student);System.out.println("vo is:"+studentVo);//po轉換為map 同BeanUtils.describeMap<String, Object> map = new HashMap<String, Object>();map=PropertyUtils.describe(studentVo);System.out.println("map is:"+map.toString());//map賦值 PropertyUtils.setProperty(map, "father", "張五五");System.out.println("now map is:"+map.toString());} }

運行結果:

getName is:張三三
StudentVo [id=0, name=null, age=0, grade=0.0]
vo is:StudentVo [id=1, name=張三三, age=18, grade=0.0]
map is:{grade=0.0, name=張三三, id=1, class=class com.dao.chu.movie.StudentVo, age=18}
now map is:{grade=0.0, father=張五五, name=張三三, id=1, class=class com.dao.chu.movie.StudentVo, age=18}







總結

以上是生活随笔為你收集整理的PropertyUtils的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。