BeanUtils_BeanUtils

BeanUtils_BeanUtils使用maven创建项目,pom文件<dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.3</version> </depende

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

使用maven创建项目,pom文件
<dependency>
	<groupId>commons-beanutils</groupId>
	<artifactId>commons-beanutils</artifactId>
	<version>1.9.3</version>
</dependency>
测试代码:
public class Person {//javabean
	private String name;//字段,当提供get或set方法,才叫做属性,class也是该类的属性,因为该类继承Object
	private String password;
	private int age;
	private Date birthday;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", password=" + password + ", age=" + age + ", birthday=" + birthday + "]";
	}
	
}
————————————————————————————————————————————————————————————
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

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;

//BeanUtils操作bean
public class Demo1 {
	@Test
	public void test1() throws Exception {
		Person person=new Person();
		BeanUtils.setProperty(person, "age", 12);
		System.out.println(person.getAge());
	}
	
	@Test
	public void test2() throws Exception {
		String name="name";
		String password="password";
		String age="13";
		String birthday="1992-12-12";
		
		//为了让日期能赋值到bean属性中,我们需要注册一个日期转换器
		ConvertUtils.register(new Converter() {

			public <T> T convert(Class<T> type, Object value) {
				if(value==null){
					return null;
				}
				if(!(value instanceof String)){
					throw new ConversionException("转换异常");
				}
				String string=(String)value;
				if(string.trim().equals("")){
					return null;
				}
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
				
				try {
					return (T) sdf.parse(string);
				} catch (ParseException e) {
					throw new RuntimeException(e);//异常链不能断
				}
			}
			
		}, Date.class);
		
//		ConvertUtils.register(new DateLocaleConverter(), Date.class);//BeanUtils自带的日期转换器,但是该转换器有bug,不能处理“”的情况
		
		Person person=new Person();
		BeanUtils.setProperty(person, "name", name);
		BeanUtils.setProperty(person, "password", password);
		BeanUtils.setProperty(person, "age", age);//只支持8种基本数据类型
		BeanUtils.setProperty(person, "birthday", birthday);
		
		System.out.println(person.getName());
		System.out.println(person.getPassword());
		System.out.println(person.getAge());
		Date date = person.getBirthday();
		
		System.out.println(date.toLocaleString());
		
	}
	
	@Test
	public void test3() throws Exception {
		Map<String, String> map=new HashMap<String, String>();
		map.put("name", "name");
		map.put("password", "password");
		map.put("age", "12");
		map.put("birthday", "1992-12-12");
		ConvertUtils.register(new DateLocaleConverter(), Date.class);
		Person person=new Person();
		BeanUtils.populate(person, map);//用map填充Bean
		
		System.out.println(person);
	}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/194902.html原文链接:https://javaforall.net

(0)
上一篇 2025年10月27日 下午7:43
下一篇 2025年10月27日 下午8:15


相关推荐

  • java 二维数组 数据库_java 二维数组如何存入数据库

    java 二维数组 数据库_java 二维数组如何存入数据库usingSystem;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Xml;usingSystem.Xml.Serialization;usingSystem.IO;namespaceWindowsFormsApplication1{publicpartialclassForm…

    2022年5月16日
    42
  • Spring学习总结(一)入门

    Spring学习总结(一)入门

    2021年11月30日
    46
  • ONF组织的SDN架构文档——概述(一)

    ONF组织的SDN架构文档——概述(一)1 适用范围 nbsp nbsp nbsp nbsp nbsp 这个文档描述了 SDN 架构 文档的目的是为 ONF 工作组未来的工作做详细指导和说明 同时也可以作为 ONF 对外交流的一个参考文档 它的姊妹文档 框架文档 描述了 ONF 想要达到的设计目标 此文档只是从一个较高的角度描述了达到此目标的一些方法 nbsp nbsp nbsp nbsp SDN 架构从一个较高的角度出发 给控制器指定了参考点和接口 架构描述了大量 SDN 控制器和 NE networke

    2026年3月19日
    2
  • java中JDBC是什么?[通俗易懂]

    java中JDBC是什么?[通俗易懂]JDBC是什么?JDBC即(javadatabaseconnectivity数据连接)。JDBC是Sun公司编的一堆类和方法,都封装在java.sql包中。你可以利用这堆类和方法来把你的程序和数据库连通。JDBC的优点:跨数据库性通过使用JDBC,开发人员可以将SQL语句传送给几乎任何一种数据库。不必单独写一个程序访问Sybase,Oracle,或Microsoft的SQLServ…

    2022年6月23日
    25
  • 成员变量和局部变量的区别

    成员变量和局部变量的区别成员变量和局部变量的区别 nbsp nbsp nbsp 1 在类中的位置不同 nbsp nbsp nbsp nbsp nbsp nbsp 成员变量 类中方法外 nbsp nbsp nbsp nbsp nbsp nbsp 局部变量 方法定义中或者方法声明上 nbsp nbsp nbsp 2 在内存中的位置不同 nbsp nbsp nbsp nbsp nbsp nbsp 成员变量 在堆中 nbsp nbsp nbsp nbsp nbsp nbsp 局部变量 在栈中 nbsp nbsp nbsp 3 生命周期不同 nbsp nbsp nbsp nbsp nbsp nbsp 成员变量 随着对象的创建而存在 随着对象的消失而消失 nbsp nbsp nbsp nbsp nbsp nbsp 局部变量 随着方法的调用而存在 随着

    2026年3月20日
    2
  • OpenClaw(Clawdbot / Moltbot)安装配置教程

    OpenClaw(Clawdbot / Moltbot)安装配置教程

    2026年3月13日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号