apache 自己为程序员们封装了一个专门用于处理的工具类,其功能有(数据类型会自动转成与JavaBean相关的)

map转javabean

javabean转map

javabean对象复制

获取javabean对象属性值

设置javabean对象属性值…………

两个相关jar包文件 Build Path到项目当中去

commons-beanutils-1.9.2.jar

commons-logging-1.2.jar

1.将Map转换成JavaBean对象

/**
* 刘诗华
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception { Map<String, Object> m=new HashMap<String, Object>();
m.put("id", "28");
m.put("userName", "刘诗华");
m.put("password", "123456"); User user=new User(); //BeanUtils.copyProperties(dest, orig);  dest:目标 orig:源
BeanUtils.copyProperties(user,m);
System.out.println(user); //结果:User(id=28, userName=刘诗华, password=123456) Integer id = user.getId(); //我们设置给Map集合的时候,给的是一个字符串,BeanUtils工具自动帮我们转换成包装类Integer类型
System.out.println(id);
}

2.JavaBean对象复制数据

/**
* 刘诗华
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
User orig=new User(28,"罗小胖","456456"); //源对象
User dest=new User(); //目标对象 空对象
BeanUtils.copyProperties(dest,orig); //JavaBean对象复制数据
System.out.println(dest);
//User(id=28, userName=罗小胖, password=456456)
}

3.设置Date时间格式转换

/**
* 申请注册时间格式
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//源数据
Map<String, Object> m=new HashMap<String, Object>();
m.put("id", "");
m.put("userName", "刘诗华");
m.put("password", "123456");
m.put("hireDate", "2018/11/19"); //目标数据
User user=new User(); //时间数据格式对象
DateConverter converter=new DateConverter(); //converter.setPattern("yyyy-MM-dd HH:mm:ss"); //单个数据格式
//一组时间格式
String[] pattern=new String[3];
pattern[0]="yyyy-MM-dd HH:mm:ss";
pattern[1]="yyyy-MM-dd";
pattern[2]="yyyy/MM/dd";
converter.setPatterns(pattern); //如果Id上面没有数据,则设置为null
IntegerConverter integerConverter=new IntegerConverter(null);
ConvertUtils.register(integerConverter, Integer.class); //注册Date时间对象格式
ConvertUtils.register(converter, Date.class);
//开始复制数据信息
BeanUtils.copyProperties(user, m); System.out.println(user);
//User(id=null, userName=刘诗华, password=123456, hireDate=Mon Nov 19 00:00:00 CST 2018)
}

04-体验一下apache组织封装的BeanUtil工具包的更多相关文章

  1. 如何在Ubuntu 18.04上安装Apache Web服务器

    一. apt库安装 1.在终端输入更新检查命令,sudo apt-get update 2. 在更新完成后(如果不想检查更新,也可直接输入此步)输入:sudo apt-get install apac ...

  2. Log4J是Apache组织的开源一个开源项目,通过Log4J,可以指定日志信息输出的目的地,如console、file等。Log4J采用日志级别机制,请按照输出级别由低到高的顺序写出日志输出级别。

    Log4J是Apache组织的开源一个开源项目,通过Log4J,可以指定日志信息输出的目的地,如console.file等.Log4J采用日志级别机制,请按照输出级别由低到高的顺序写出日志输出级别. ...

  3. Ubuntu 18.04安装配置Apache Ant

    Ubuntu 18.04安装配置Apache Ant 文章目录 Ubuntu 18.04安装配置Apache Ant 下载 执行以下命令 `/etc/profile`中配置环境变量 载入配置 测试 执 ...

  4. 如何在Ubuntu 16.04上安装Apache Web服务器

    转载自:https://www.howtoing.com/how-to-install-the-apache-web-server-on-ubuntu-16-04 介绍 Apache HTTP服务器是 ...

  5. Ubuntu 16.04 集成安装Apache+PHP+Kerberos+LDAP+phpLDAPadmin

    一.安装Apache 1.1.安装Apache apt-get update apt-get install apache2 过程如下: root@duke01:~# apt-get update命中 ...

  6. 百度云的ubuntu16.04.1部署Apache服务器+Django项目

    使用Apache和mod_wsgi部署Django 是一种久经考验的将Django投入生产的方法. mod_wsgi是一个Apache模块,可以托管任何Python WSGI应用程序,包括Django ...

  7. Ubuntu 18.04上安装Apache, MySQL, PHP, LAMP

    1.安装 Apache $ sudo apt update && sudo apt install apache2 中间会遇到停顿询问是否继续, 输入 y 然后 回车. 2.测试 Ap ...

  8. Ubuntu20.04 体验和美化

    Ubuntu20.04美化和体验 windows用久了,换下系统也挺好的.ubuntu20.04优化后,用起来蛮舒服的. 系统配置 1.修改软件源 Ubuntu默认是国外的软件源, 我们可以手动切换为 ...

  9. Ubuntu12.04 下修改Apache端口号

    1:$sudo vim /etc/apache2/ports.conf NameVirtualHost *:80Listen 8090 #将此行的80修改成8090 2:sudo vim /etc/a ...

随机推荐

  1. angular学习2

    1.为了在angular里面使用bootstrap,可以如下操作 (1)停止正在运行的终端指令:ctrl+c (2)在终端里面输入:npm install bootstrap --save (3)在V ...

  2. 脚本自动部署及监控 web

    1.编写脚本自动部署反向代理.web.nfs: I.部署nginx反向代理两个web服务,调度算法使用加权轮询 II.所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性: ...

  3. 项目中Java Resources有红叉,其它没有,解决办法

    说起这个这个地方,我课改了好久 起初,我把原先项目的JDK版本改了,右击项目Build Path,然后换掉里面的JRE,没用, 然后右击项目,点击properties,找到在Project Facet ...

  4. [转] C++ 和 python之间的互相调用

    转载自:https://www.cnblogs.com/apexchu/p/5015961.html 一.Python调用C/C++ 1.Python调用C动态链接库 Python调用C库比较简单,不 ...

  5. C# 时间戳 整理

    以前遇到时间戳,都是那公共类里面的方法来用.未曾理解过它的原理. C# 时间类型枚举     分为local.utc.以及Unspecified local:当地时间,例如我们所在的东八区,所采用的北 ...

  6. 【Keil5 MDK】armar工具的基本用法(armar --help)

    ARM Librarian, 5.03 [Build 76] - archive creation and maintenance tool Command format: armar options ...

  7. RabbitMq入门以及使用教程

    祭出原帖:https://blog.csdn.net/lyhkmm/article/details/78772919 原文转载:http://blog.csdn.net/whycold/article ...

  8. java.lang.ClassNotFoundException: org.apache.http.conn.UnsupportedSchemeException

    加入了阿里云的消息服务后,就一直之前报java.lang.ClassNotFoundException: org.apache.http.conn.UnsupportedSchemeException ...

  9. LeetCode - Maximum Frequency Stack

    Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack ...

  10. 安装httpd服务

    1.yum安装httpd服务 2.启动httpd服务端口被占用 3.修改端口 4.启动httpd服务 5.输入网址是否正常能访问