BeanUtil工具类是apache commons中的项目
使用BeanUtil除了需要 commons-beanutils-1.8.3.jar 外,可能需要记录错误日志信息,再加入 commons-logging-1.1.3.jar(也是apache的) 即可
下面着重看一些例子
// 实体类User Point,这里就省去get,set方法
package com.yangwei.model;
import java.util.Date;
public class User {
private String name;
private int age;
private Date birth;
private Point point;
}
public class Point {
private int x;
private int y;
}
package com.yangwei.test;
import static org.junit.Assert.fail;
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;
import com.yangwei.model.User;
public class TestBeanUtil {
@Test
public void test01() {
try {
User u=new User();
//假设要为name设置值"zhangsan"
String key="name";
String value="zhangsan";
//以前我们使用Method 调用它的invoke方法完成操作
//现在我们使用BeanUtils的copyProperty完成设值操作
BeanUtils.copyProperty(u, key, value);
System.out.println(u.getName());//zhangsan
//拷贝不认识的属性,也不会报错
BeanUtils.copyProperty(u, "yyy", value);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
两个转换器类 ,实现Converter接口
package com.yangwei.model;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.beanutils.Converter;
public class DateConverter implements Converter{
/**
* 第一个参数表示要转换的类型, 第二个参数表示要转换的值
* 比如要拷贝一个字符串到日期中,第一个参数就是日期,第二个参数是字符串值
*/
SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd");
@Override
public Object convert(Class clz, Object obj) {
if(clz!=Date.class){
return null;
}
try {
if(obj instanceof String){
return f.parse((String) obj);
}
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
package com.yangwei.model;
import org.apache.commons.beanutils.Converter;
public class PointConverter implements Converter {
/**
* 将传递过来的值转为Point类
*/
@Override
public Object convert(Class clz, Object obj) {
if(clz!=Point.class){
return null;
}
if(obj instanceof String){
String value=(String)obj;
String strArr[]=value.split(",");
if(strArr!=null && strArr.length==2){
Point point=new Point();
point.setX(Integer.parseInt(strArr[0]));
point.setY(Integer.parseInt(strArr[1]));
return point;
}
}
return null;
}
}
package com.yangwei.test;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.junit.Test;
import com.yangwei.model.DateConverter;
import com.yangwei.model.Point;
import com.yangwei.model.PointConverter;
import com.yangwei.model.User;
public class TestBeanUtil {
@Test
public void test01() {
try {
User u=new User();
//假设要为name设置值"zhangsan"
String key="name";
String value="zhangsan";
//以前我们使用Method 调用它的invoke方法完成操作
//现在我们使用BeanUtils的copyProperty完成设值操作
BeanUtils.copyProperty(u, key, value);
System.out.println(u.getName());//zhangsan
//拷贝不认识的属性,也不会报错
BeanUtils.copyProperty(u, "yyy", value);
/**
* 完整拷贝一个对象,此时会有错误,因为它不知道将Date转为何种类型
* 日期是有很多中格式情况的,如 1977-10-10 1977/10/10等
* 此时,如何处理呢???
* 需要定义转换器 定义转换器的步骤:
* 1, 创建一个类,实现Converter接口
* 2,重写convert方法,实现转换
* 3,在拷贝属性之前,注册转换器
*/
ConvertUtils.register(new DateConverter(), Date.class);
BeanUtils.copyProperty(u, "birth", "1988-11-20");
ConvertUtils.register(new PointConverter(), Point.class);
BeanUtils.copyProperty(u, "point", "12,23");
User u2=new User();
BeanUtils.copyProperties(u2, u);
System.out.println(u.getName()+u.getBirth()+u.getPoint());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com