BeanUtils 的使用 | 学习笔记

简介: 快速学习 BeanUtils 的使用

开发者学堂课程【JSP 快速入门 BeanUtils 的使用】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/33/detail/725


BeanUtils 的使用


JavaBean 的规范

1.必须要有一个默认构造器

2. 提供 get/set 方法,如果只有 get 方法,那么这个属性是只读属性

3. 属性:有 get/set 方法的成员,述可以没有成员,只有 get/set 方法,属性名称由get/set 方法来决定!而不是成员名称!

4.方法名称满足一定的规范,那么他就是是属性!  boolean 类型的属性, 它的读方法可以是 is 开头,也可以是 get 开头!

例如:

public class Person {

private String username;

private int age;

private String gender;

private boolean bool ;

public boolean isBool() {

return bool;

}

public void setBool (boolean bool) {

this.bool = bool;

}

public string getId() {

retarn "fdsafdafdas";

}

public  String  getName(){


内省就是通过反射来操作 javabean ,但他比使用反射要方便些!

我们需要提供 javaBean 类!

例如:

内省:

内省类--> Bean 信息-->属性描述符--> 属性的 get/set 对应的 Method ! --- >可以反射了!

commons-beanutils , 它是依赖内省完成!

导包:

conmons -be anucils . jar

conmong- logging. jar

public class Demo1 l

@Test

public void fun1() throws Exception (

String className . "cn. itcast. domain. Person";

class clazz = class. forName (className) ;

object bean = clazz. newInstance() ;

BeanUtils. setProperty(bean, "name", "张三");

BeanUtils. setProperty(bean, "age", "23");

BeanUtilg. setProperty(bean,"gender",男");

BeanUtils. setProperty(bean, "xxx", "XXX");

BeanUcils. getProperty(bean, "age");

System. out. println (age);

System. out. println (bean);

把 map 中的属性直接封装到一个 bean 中

Map: ("username" :"zhangSan", "password", "123")

GTest

public void fun2 () {

Map map . new HashMap();

map.put ("username", " zhangSan") ;

map.put ("password", "123");

User user=new User() ;

BeanUtils.populate(user, map);

这就是数据的转换。

相关文章
|
4月前
|
Java Apache
BeanUtils.copyProperties()用法总结
BeanUtils.copyProperties()用法总结
|
4月前
|
前端开发 Java 数据处理
BeanUtils.copyProperties的用法
BeanUtils.copyProperties的用法
|
3月前
|
Java Apache 开发者
介绍BeanUtils.copyProperties方法
介绍BeanUtils.copyProperties方法
|
4月前
|
Java API
beanutils.copyproperties
beanutils.copyproperties
|
Java Apache 数据库
Spring的BeanUtils的copyProperties方法
项目中遇到的情况是: 文件解析完之后将文件放在一个pojo里面
120 0
|
Java 测试技术 Spring
为什么不推荐使用 BeanUtils ?
为什么不推荐使用 BeanUtils ?
313 0
为什么不推荐使用 BeanUtils ?
BeanUtils.copyProperties 使用注意
BeanUtils.copyProperties 使用注意
408 0
BeanUtils.copyProperties 使用注意
|
程序员 Apache
【BeanUtils】自己写的一个BeanUtils-代码方法详解(1)
【BeanUtils】自己写的一个BeanUtils-代码方法详解
182 0
【BeanUtils】自己写的一个BeanUtils-代码方法详解(1)