BeanUtils使用
1.BeanUtils.populate
可以把一个map中的属性拷贝到实体javaBean,例子:
Student:
package com.cy.model; import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle; public class Student{
private String id;
private String name;
private String stuNo;
private String address; public Student(){ }
public Student(String id, String name, String stuNo, String address) {
this.id = id;
this.name = name;
this.stuNo = stuNo;
this.address = address;
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getStuNo() {
return stuNo;
} public void setStuNo(String stuNo) {
this.stuNo = stuNo;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} @Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
package com.cy.test; import com.cy.model.Student;
import org.apache.commons.beanutils.BeanUtils;
import java.util.HashMap;
import java.util.Map; public class BeanUtilTest { public static void main(String[] args) throws Exception {
//1.BeanUtils.populate
Student s1 = new Student();
Map<String, String> map = new HashMap<String, String>();
map.put("id", "1");
map.put("name", "zhangsan");
BeanUtils.populate(s1, map);
System.out.println(s1);
}
}
Student[id=1,name=zhangsan,stuNo=<null>,address=<null>]
2.BeanUtils.copypropertis(Object dest, Object orig)
将源实体属性拷贝到目标dest属性中。
StudentVo:
package com.cy.vo; import com.cy.model.Student;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle; public class StudentVo {
private String name;
private String stuNo; public StudentVo(){ } public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getStuNo() {
return stuNo;
} public void setStuNo(String stuNo) {
this.stuNo = stuNo;
} @Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
package com.cy.test; import com.cy.model.Student;
import com.cy.vo.StudentVo;
import org.apache.commons.beanutils.BeanUtils; import java.util.HashMap;
import java.util.Map; public class BeanUtilTest { public static void main(String[] args) throws Exception {
Student s = new Student();
s.setId("1");
s.setName("xiaoming");
s.setStuNo("001"); //2.BeanUtils.copypropertis
StudentVo sv = new StudentVo();
BeanUtils.copyProperties(sv, s);
System.out.println(sv);
}
}
StudentVo[name=xiaoming,stuNo=001]
3.但是如果两个类的属性名不一样,就不能复制属性了。如:
StudentDto:
package com.cy.dto; import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle; public class StudentDto {
private String student_id;
private String name;
private String stu_no;
private String address; public String getStudent_id() {
return student_id;
} public void setStudent_id(String student_id) {
this.student_id = student_id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getStu_no() {
return stu_no;
} public void setStu_no(String stu_no) {
this.stu_no = stu_no;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} @Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
package com.cy.test; import com.cy.dto.StudentDto;
import com.cy.model.Student;
import com.cy.vo.StudentVo;
import org.apache.commons.beanutils.BeanUtils; import java.util.HashMap;
import java.util.Map; public class BeanUtilTest { public static void main(String[] args) throws Exception {
StudentDto st = new StudentDto();
st.setStudent_id("1");
st.setStu_no("001");
st.setName("xiaoming");
st.setAddress("beijing"); Student s = new Student();
BeanUtils.copyProperties(s, st);
System.out.println(s);
}
}
Student[id=<null>,name=xiaoming,stuNo=<null>,address=beijing]
4.属性类型不一样,也是可以转化成功的:
StudentDto:
package com.cy.dto; import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle; public class StudentDto {
private Integer id;
private String name;
private String stu_no;
private String address;
private Boolean good; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public Boolean getGood() {
return good;
} public void setGood(Boolean good) {
this.good = good;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getStu_no() {
return stu_no;
} public void setStu_no(String stu_no) {
this.stu_no = stu_no;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} @Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
Student:
package com.cy.model; import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle; public class Student{
private String id;
private String name;
private String stuNo;
private String address;
private String good; public Student(){ } public String getGood() {
return good;
} public void setGood(String good) {
this.good = good;
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getStuNo() {
return stuNo;
} public void setStuNo(String stuNo) {
this.stuNo = stuNo;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} @Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
package com.cy.test; import com.cy.dto.StudentDto;
import com.cy.model.Student;
import com.cy.vo.StudentVo;
import org.apache.commons.beanutils.BeanUtils; import java.util.HashMap;
import java.util.Map; public class BeanUtilTest { public static void main(String[] args) throws Exception {
StudentDto st = new StudentDto();
st.setId(1);
st.setStu_no("001");
st.setName("xiaoming");
st.setAddress("beijing");
st.setGood(true); Student s = new Student();
BeanUtils.copyProperties(s, st);
System.out.println(s);
}
}
Student[id=1,name=xiaoming,stuNo=<null>,address=beijing,good=true]
BeanUtils使用的更多相关文章
- java jdbc的优化之BeanUtils组件
1. BeanUtils组件 1.1 简介 程序中对javabean的操作很频繁, 所以apache提供了一套开源的api,方便对javabean的操作!即BeanUtils组件. BeanUtils ...
- BeanUtils.populate(obj, map);
public static void populate(Object bean, Map<String, ? extends Object> properties) throws Ille ...
- Beanutils基本用法
Beanutils用了魔术般的反射技术,实现了很多夸张有用的功能,都是C/C++时代不敢想的.无论谁的项目,始终一天都会用得上它.我算是后知后觉了,第一回看到它的时候居然错过. 1.属性的动态gett ...
- BeanUtils
BeanUtils包的使用 BeanUtils工具包是由Apache公司所开发,主要是方便程序员对Bean类能够进行简便的操作. 在使用BeanUtils工具包之前我们需要的Jar包有以下几种: ...
- BeanUtils.copyProperties()方法和PropertyUtils.copyProperties()的区别
首先两者来源于同一个包: import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.Prop ...
- Apache Commons BeanUtils
http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.2/apidocs/org/apache/commons/beanut ...
- 利用BeanUtils在对象间复制属性
commons-beanutils是jakarta commons子项目中的一个软件包,其主要目的是利用反射机制对JavaBean的属性进行处理.我们知道,一个JavaBean通常包含了大量的属性,很 ...
- BeanUtils.copyProperties() 用法
BeanUtils提供对Java反射和自省API的包装.其主要目的是利用反射机制对JavaBean的属性进行处理.我们知道,一个JavaBean通常包含了大量的属性,很多情况下,对JavaBean的处 ...
- BeanUtils.populate的作用
它是在org.apache.commons.beanutils.BeanUtils包中的一个方法. 方法的作用:将一些 key-value 的值(例如 hashmap)映射到 bean 中的属性. ...
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性
本文转载自:http://blog.sina.com.cn/s/blog_5d65a16901011kom.html 关键字:内省.JavaBean.PropertyDescriptor类.Int ...
随机推荐
- Python使用requests库,requests.post格式会引发参数不存在问题
Content-Type: application/json #############↓↓↓成功↓↓↓############# import requests url = 'http://loca ...
- 如何用core自动创建model,与数据库连接
打开vs.点击视图->其他->程序包管理器控制台->选择默认项目->输入 Scaffold-DbContext “Data Source=XXX;Initial Catalog ...
- php 浏览记录功能
/** * 历史浏览记录 * $data array 浏览记录里作品的必要信息 */ private function _history($data){ if(!$data || !is_array( ...
- s21day19 python笔记
s21day19 python笔记 一.面向对象的基本知识 1.1 基本格式 # 定义类 class 类名: def 方法名(self,name): print(name) return 123 de ...
- 百战程序员——EL、JSTL
1.EL表达式可以操作作用域中的属性,也可以操作普通的局部变量.对吗? 不对 El表达式一般支持作用域(application.session.request.pagecontext)中的属性.EL变 ...
- 04PHP HTML状态保持
HTTP无状态:会话时没有储存数据 HTTP状态保持: 1.Cookie:保存在浏览器 $_COOKIE[ ] 超全局变量 数组 不安全,用户可清楚数据时把Cookie清除 ==目的:多页面之间 ...
- 运行Tomcat报错 解决方法
The APR based Apache Tomcat Native library which allows optimal performance in production environmen ...
- python之 类对象 类方法 实例对象 实例方法 静态方法
实例对象1. 创建的时间:使用 类名()的时候,就创建一个实例对象2. 实例属性:怎样添加 只要是一个变量能够指向这个实例对象,那么这个变量.xxxx = 111就是给其添加一个实例属性 特点: 跟着 ...
- Fish 下报错 Unsupported use of '||'. In fish, please use 'COMMAND; or COMMAND'.
在用fish激活virualenv虚拟环境时,使用命令: source ./venv/bin/activate 报错 ./venv/bin/activate (line 23): Unsupporte ...
- 转载一篇阿里云Terraform 开发指南
连接:https://www.jianshu.com/p/0aebea117cae 是一个比较详细的开发指南