BeanUtils之commons-beanutils
這是一個(gè)把Map轉(zhuǎn)為bean的工具
導(dǎo)包
?
Bean代碼,Student類
package com.lingaolu.bena;/*** @author 林高祿* @create 2020-07-08-20:01*/ public class Student {private String name;private int age;private Integer no;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 Integer getNo() {return no;}public void setNo(Integer no) {this.no = no;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", no=" + no +'}';} }測(cè)試類代碼Demo1
package com.lingaolu.test;import com.lingaolu.bena.Student; import org.apache.commons.beanutils.BeanUtils;import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map;/*** @author 林高祿* @create 2020-07-08-20:03*/ public class Demo1 {public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {Map<String,Object> map = new HashMap<>();map.put("name","林大帥");map.put("age",27);map.put("no",111);Student s = new Student();BeanUtils.populate(s,map);System.out.println(s);} }運(yùn)行如果輸出
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
?? ?at 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 org.apache.commons.beanutils.ContextClassLoaderLocal.get(ContextClassLoaderLocal.java:153)
?? ?at org.apache.commons.beanutils.BeanUtilsBean.getInstance(BeanUtilsBean.java:80)
?? ?at org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:433)
?? ?at com.lingaolu.test.Demo1.main(Demo1.java:23)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
?? ?at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
?? ?at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
?? ?at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
?? ?at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
?? ?... 7 more
缺少了 org/apache/commons/logging包
下載地址apache的logging包
?解壓
導(dǎo)入項(xiàng)目
?
?
?再次運(yùn)行輸出:
Student{name='林大帥', age=27, no=111}
我們把測(cè)試再代碼改一下
package com.lingaolu.test;import com.lingaolu.bena.Student; import org.apache.commons.beanutils.BeanUtils;import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map;/*** @author 林高祿* @create 2020-07-08-20:03*/ public class Demo1 {public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {Map<String,Object> map = new HashMap<>();map.put("name","林大帥");map.put("age","234");map.put("no","111");Student s = new Student();BeanUtils.populate(s,map);System.out.println(s);} }運(yùn)行輸出:
Student{name='林大帥', age=27, no=111}
?所以對(duì)于數(shù)值來(lái)說(shuō),字符串或者數(shù)字都行
?
總結(jié)
以上是生活随笔為你收集整理的BeanUtils之commons-beanutils的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 单片机的流水灯程序
- 下一篇: 恶意代码攻击实验(冰河木马和广外男孩的使