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 ...
随机推荐
- 我了解到的JavaScript异步编程
一. 一道面试题 前段时间面试,考察比较多的是js异步编程方面的相关知识点,如今,正好轮到自己分享技术,所以想把js异步编程学习下,做个总结. 下面这个demo 概括了大多数面试过程中遇到的问题: f ...
- 获取windows所有用户名
#include <LM.h> #pragma comment(lib, "netapi32.lib") // See more: http://msdn.micros ...
- 当使用composer安装组件时提示错误
这种情况可以重装一下fxp/composer-asset-plugin 具体命令: php composer.phar global require "fxp/composer-asset- ...
- 7.28.2 static关键字(静态和成员)
成员是对象级别的,访问成员必须用"引用.",如果用"类名."访问会报错!如果用空引用访问成员则会发生控空指针异常! 静态是类级别的,访问静态必须用类" ...
- 火币网现货API[Python3版]
火币 期货 现货 API [Python3版] 一.Util.py,基础类,包括参数配置.签名,HTTP 请求方法,发送信息到API #coding=utf-8 import hashlib impo ...
- BotVS开发基础—Python API
代码 import json def main(): # python API列表 https://www.botvs.com/bbs-topic/443 #状态信息 LogStatus(" ...
- jeecg 3.7.1 新版功能,集群定时任务动态发布模块 使用规则
jeecg 3.7.1 集群定时任务动态发布模块 使用规则 新版特性: 支持集群定时任务,支持分布式. 菜单路径: 系统监控-->定时任务 字段说明: 任务ID.任务说明:自定义即可 ...
- lsnrctl start 命令未找到 数据库连接报错“ORA-12541: TNS: 无监听程序”
1. lsnrctl start 命令未找到 或者bash:lsnrctl:command not found. su - oralce 切换用户的时候,中间要有-,而且-的两边有空格, ...
- 写给后端的前端笔记:定位(position)
写给后端的前端笔记:定位(position) 既然都写了一篇浮动布局,干脆把定位(position)也写了,这样后端基本上能学会css布局了. 类别 我们所说的定位position主要有三类:固定定位 ...
- 【译】StackExchange.Redis中文使用文档
StackExchange.Redis中文使用文档 Intro 最近想深入学习一些 Redis 相关的东西.于是看了看官方的项目StackExchange.Redis,发现里面有一份文档,于是打算翻译 ...