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 ...
随机推荐
- JSP 标准标签库(JSTL)(JSP Standard Tag Library)
[1] JSTL简介 > JSTL是JSP的标准标签库 > JSTL为我们提供了一些常用的标签,供我们日常开发使用(if . if...else .遍历 . 日期格式化) ...
- css div旋转之后自适应
css: .rotate90deg { transform: rotate(90deg); -ms-transform: rotate(90deg); /* IE 9 */ -moz-transfor ...
- Vue2.0父子组件之间和兄弟组件之间的数据交互
熟悉了Vue.js的同级组件之间通信,写此文章,以便记录. Vue是一个轻量级的渐进式框架,对于它的一些特性和优点,请在官网上进行查看,不再赘述. 使用NPM及相关命令行工具初始化的Vue工程,目录结 ...
- git 生成公钥、私钥方法与clone使用方法
我的配置流程 Git配置 Git安装完之后,需做git配置.打开git bash,分别执行以下两句命令 git config --global user.name "用户名" gi ...
- hdu 6171---Admiral(双向搜索)
题目链接 Problem Description Suppose that you are an admiral of a famous naval troop. Our naval forces h ...
- java web面试技巧,数据库面试,java web轻量级开发面试教程
我最近看到一本比较好的讲java web方面面试的书,java web轻量级开发面试教程. 其中不仅用案例和视频讲述了Spring MVC,Hibernate, ORM等方面的技巧,而且还实际讲到了面 ...
- Struts2使用小问题-NoSuchFieldException
五月 12, 2017 4:55:14 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger warn 警告: couldn't clear to ...
- C#/VB.NET对EXCEL图片添加超链接
在日常工作中,在编辑文档时,为了方便自己或者Boss能够实时查看到需要的网页或者文档是,需要对在Excel中输入的相关文字进行超链接,那么对于一些在Excel中插入的图片我们该怎么实现超链接呢,下面给 ...
- linux系统下C语言调用lapack ,blas库
在利用C语言编程,经常调用其他的软件包,其中lapack,blas库是最常用的两个库,这里讲下在linux系统下,C语言编程如何调用这两个库: 1.首先讲下blas库的调用,这里以两个向量内积函数为例 ...
- 用webgl打造自己的3D迷宫游戏
用webgl打造自己的3D迷宫游戏 2016/09/19 · JavaScript · WebGL 原文出处: AlloyTeam 背景:前段时间自己居然迷路了,有感而发就想到写一个可以让人迷路 ...