大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
public class Person {//javabean
private String name;//字段,当提供get或set方法,才叫做属性,class也是该类的属性,因为该类继承Object
private String password;
private int age;
private Date birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Person [name=" + name + ", password=" + password + ", age=" + age + ", birthday=" + birthday + "]";
}
}
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.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;
//BeanUtils操作bean
public class Demo1 {
@Test
public void test1() throws Exception {
Person person=new Person();
BeanUtils.setProperty(person, "age", 12);
System.out.println(person.getAge());
}
@Test
public void test2() throws Exception {
String name="name";
String password="password";
String age="13";
String birthday="1992-12-12";
//为了让日期能赋值到bean属性中,我们需要注册一个日期转换器
ConvertUtils.register(new Converter() {
public <T> T convert(Class<T> type, Object value) {
if(value==null){
return null;
}
if(!(value instanceof String)){
throw new ConversionException("转换异常");
}
String string=(String)value;
if(string.trim().equals("")){
return null;
}
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
try {
return (T) sdf.parse(string);
} catch (ParseException e) {
throw new RuntimeException(e);//异常链不能断
}
}
}, Date.class);
// ConvertUtils.register(new DateLocaleConverter(), Date.class);//BeanUtils自带的日期转换器,但是该转换器有bug,不能处理“”的情况
Person person=new Person();
BeanUtils.setProperty(person, "name", name);
BeanUtils.setProperty(person, "password", password);
BeanUtils.setProperty(person, "age", age);//只支持8种基本数据类型
BeanUtils.setProperty(person, "birthday", birthday);
System.out.println(person.getName());
System.out.println(person.getPassword());
System.out.println(person.getAge());
Date date = person.getBirthday();
System.out.println(date.toLocaleString());
}
@Test
public void test3() throws Exception {
Map<String, String> map=new HashMap<String, String>();
map.put("name", "name");
map.put("password", "password");
map.put("age", "12");
map.put("birthday", "1992-12-12");
ConvertUtils.register(new DateLocaleConverter(), Date.class);
Person person=new Person();
BeanUtils.populate(person, map);//用map填充Bean
System.out.println(person);
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/194902.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...