BeanUtils和Junit的用法
来源:互联网 发布:sql 去重 编辑:程序博客网 时间:2024/06/05 22:04
在此项目中看到了BeanUtils这个神奇的类,看了半天知道它是复制对象的,其实真正的好处现在还不知道。上网百度,做了一下练习。
BeanUtils需要导入的jar包:
(1)common-logging-1.1.1.jar
(2)common-beanutils.jar
package testEpcc;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;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;public class TestBeanUtils { //设置属性 @Test public void test1() throws Exception{ Person p=new Person(); BeanUtils.setProperty(p, "name","xiazdong"); BeanUtils.setProperty(p, "age",20); BeanUtils.setProperty(p, "sex","女"); System.out.println(p.getName()); System.out.println(p.getAge()); System.out.println(p.getSex()); } //自定义转换器 @Test public void test2() throws Exception{ Person p=new Person(); ConvertUtils.register(new Converter(){ @Override public <T> T convert(Class<T> type, Object value) { // TODO Auto-generated method stub if(value==null){ return null; } if(!(value instanceof String)){ throw new ConversionException("conversion error"); } String str=(String)value; if(str.trim().equals("")){ return null; } SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); try { return (T) sdf.parse(str); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }, Date.class); BeanUtils.setProperty(p, "birth", "2011-10-10"); System.out.println(p.getBirth().toLocaleString()); } //使用内置转换器 @Test public void test3() throws Exception{ Person p=new Person(); ConvertUtils.register(new DateLocaleConverter(),Date.class); BeanUtils.setProperty(p, "name", "zqy"); BeanUtils.setProperty(p, "birth", "1995-04-18"); System.out.println(p.getName()); System.out.println(p.getBirth().toLocaleString()); } //使用内置的转换器 @Test public void test4(Person p1) throws Exception{ Person p=new Person(); ConvertUtils.register(new DateLocaleConverter(),Date.class); BeanUtils.setProperty(p, "name", "zqy"); BeanUtils.setProperty(p, "birth", "1995-04-18"); BeanUtils.copyProperties(p, p1); System.out.println(p.getBirth().toLocaleString()); }}
用单元测试需要导入的jar包
junit-4.9.jar
阅读全文
0 0
- BeanUtils和Junit的用法
- 关于BeanUtils.copyProperties的用法和优缺点
- 关于BeanUtils.copyProperties的用法和优缺点
- 关于BeanUtils.copyProperties的用法和优缺点
- BeanUtils.copyProperties()的用法
- BeanUtils.copyProperties()的用法
- BeanUtils.copyProperties()的用法
- BeanUtils.copyProperties()的用法
- BeanUtils.copyProperties()的用法
- BeanUtils工具包的用法
- BeanUtils.copyProperties()的用法
- Beanutils的基本用法
- BeanUtils.copyProperties()的用法
- BeanUtils.populate的用法
- BeanUtils的用法
- BeanUtils.copyProperties()的用法
- beanUtils的用法
- BeanUtils.copyProperties()的用法
- Shell脚本数组操作小结
- TensorFlow(五)CNN
- Cocos2d-lua 初识shader之三:模糊
- fiddle 本地css/js 替换线上代码调试
- 51 Nod1113 矩阵快速幂
- BeanUtils和Junit的用法
- JAVA学习笔记01——Linux常用命令详解
- Gastroplus v7.0-ISO 1CD(基于机制性生理模型的药代动力学、药效动力学(PBPK/PD)模拟软件)
- js 时间戳转换时间
- Vue-02
- 单例模式Singleton
- 伪造 X-Forwarded-For
- 向梦想出发
- Android app Module与library Module编译版本统一实现方法