apache-beanutil工具类的使用
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();
}
}
}
apache-beanutil工具类的使用的更多相关文章
- Apache Commons 工具类介绍及简单使用
转自:http://www.cnblogs.com/younggun/p/3247261.html Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下 ...
- linkin大话数据结构--apache commons工具类
Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动. 一.Commons BeanUtils 说明:针对Bean的一个工具集.由于Bean往往是有一堆ge ...
- Apache Commons 工具类简单使用
Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中自己用过的工具类做简单介绍. 组件 功能介绍 BeanUtils 提供了对于 ...
- BeanUtil工具类的使用
BeanUtils的使用 1.commons-beanutils的介绍 commons-beanutils是Apache组织下的一个基础的开源库,它提供了对Java反射和内省的API的包装,依赖内省, ...
- Apache Commons 工具类介绍及简单使用(转载)
原文链接 http://www.cnblogs.com/younggun/p/3247261.html Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动 ...
- Java:Apache Commons 工具类介绍及简单使用
Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中自己用过的工具类做简单介绍. Commons简介 组件 功能介绍 commo ...
- JavaWeb基础Day17 (JSP EL表达式 jstl标签库 beanutil工具类)
JSP jsp的实质就是指在html界面中嵌入Java代码 jsp脚本 <% Java代码 %> 相当于写在service方法中. <%=java 变量或者表达式 %> ...
- apache StringUtils 工具类
// org.apache.commons.lang3.StringUtils // 1.IsEmpty/IsBlank - checks if a String contains text 检查是否 ...
- apache ArrayUtils 工具类
org.apache.commons.lang3.ArrayUtils // 1.add():将给定的数据添加到指定的数组中,返回一个新的数组. int[] arr = { 1, 2, 3 }; in ...
- 03-封装BeanUtil工具类(javabean转map和map转javabean对象)
package com.oa.test; import java.beans.BeanInfo; import java.beans.IntrospectionException; import ja ...
随机推荐
- 设计模式(7)--Bridge(桥接模式)--结构型
1.模式定义: 桥接模式是对象的结构模式.又称为柄体(Handle and Body)模式或接口(Interface)模式.桥接模式的用意是“将抽象化(Abstraction)与实现化(Impleme ...
- 基于hdp2.5升级phoenix版本为4.8
hdp2.5自带的phoenix是4.7的,而客户的驾驶舱项目跑在4.7的phoenix上是有问题的,如:如果表中没有数据,执行select count(*) from 表,返回的是空,这时导致驾驶舱 ...
- MySQL之数据的备份与还原
备份db_book:这里用到了cmd,以管理员方式运行,不然后面使用mysqldump会被拒绝访问!!! 1.启动cmd,输入命令,切换到MySQL的bin文件: 2.然后输入备份命令: 备份好的sq ...
- Python学习笔记3
__slots__ 如果我们想要限制class的属性怎么办?比如,只允许对Student实例添加name和age属性. 为了达到限制的目的,Python允许在定义class的时候,定义一个特殊的__s ...
- [2016-09-23]远程安装、更新windows服务bat脚本分享
话不多说,有兴趣的自己可以仔细研究下涉及的命令:net use.sc.robocopy 脚本 set BuildConfig=[ENV] set BuildExeName=[your_exe_name ...
- Day-6: 函数式编程
函数式编程就是封装成一个个函数,一次调用来完成复杂任务. 函数式编程的一个特点是,允许把函数本身作为参数传入另一个函数,还允许返回一个函数! 高阶函数 高阶函数就是将函数的变量名作为参数传入,内部再对 ...
- DotNetCore跨平台~Quartz定时单次任务
之前写过一篇文件<DotNetCore跨平台~Quartz热部署的福音-监控文件夹的变化>,今天主要把框架优化了一下,支持外部触发,并支持外部将参数以JobDataMap形式进行输入,然后 ...
- 1011. A+B和C (15)
/*1011. A+B和C (15) 时间限制150 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者HOU, Qiming给定区间[-231, 231]内的3个整数A ...
- TP 3.2 笔记 (1)
1.配置文件分布在好多子模块中 2.I方法 使用指定过滤方法来过滤变量,第三个参数如果是函数名,则会调用该函数进行过滤,(在变量是数组的情况下自动使用array_map进行过滤处理),否则会调用 PH ...
- PHP之CI框架第一课