java开发BeanUtils类解决实体对象间赋值
作者:guoyp2126 时间:2022-08-25 06:31:25
实体对象之间相互传值,如:VO对象的值赋给Entity对象,是代码中常用功能,如果通过get、set相互赋值,则很麻烦,借助工具类BeanUtils可以轻松地完成操作。
BeanUtils依赖包导入
BeanUtils 是 Apache commons组件的成员之一,主要用于简化JavaBean封装数据的操作。使用BeanUtils必须导入相应的jar包,BeanUtils的maven坐标为
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
示例
将前端传来的学生排名信息(StudentVo对象)分别赋给学生对象(StudentEntity)和排名对象(RankingEntity),这三个类代码如下:
@Data
public class StudentVo {
private String sno;
private String sname;
private Integer ranking;
private String schoolTerm;
public String toString(){
return "studentVo对象的值 sno:"+getSno()+" sname:"+getSname()+" ranking:"+getRanking().toString()+" schoolTerm:"+getSchoolTerm();
}
}
@Data
public class StudentEntity {
private String sno;
private String sname;
private Integer sage;
public String toString(){
return "studentEntity对象的值 sno:"+getSno()+" sname:"+getSname()+" sage:"+getSage();
}
}
@Data
public class RankingEntity {
private String sno;
private Integer ranking;
private String schoolTerm;
public String toString(){
return "rankingEntity对象的值 学号:"+getSno()+" 名次:"+getRanking().toString()+" 学期:"+getSchoolTerm();
}
}
将VO对象的值赋给实体对象,通过BeanUtils.copyProperties(目标,源),将源实体对象的数据赋给目标对象,只把属性名相同的数据赋值,目标中的属性如果在源中不存在,给null值,测试代码如下:
public class App
{
public static void main( String[] args ) throws InvocationTargetException, IllegalAccessException {
StudentVo studentVo = new StudentVo();
studentVo.setSno("1");
studentVo.setRanking(20);
studentVo.setSname("胡成");
studentVo.setSchoolTerm("第三学期");
System.out.println(studentVo.toString());
StudentEntity studentEntity = new StudentEntity();
BeanUtils.copyProperties(studentEntity,studentVo);
System.out.println(studentEntity.toString());
RankingEntity rankingEntity = new RankingEntity();
BeanUtils.copyProperties(rankingEntity,studentVo);
System.out.println(rankingEntity.toString());
}
}
运行结果:
StudentVo 中不存在sage属性,获得studentEntity对象的sage的值为null
来源:https://blog.csdn.net/guoyp2126/article/details/116381031
标签:BeanUtils,实体对象,赋值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Mybatis分页插件PageHelper的配置和简单使用方法(推荐)
2022-03-20 06:34:55
Springboot自带定时任务实现动态配置Cron参数方式
2023-11-10 10:21:31
![](https://img.aspxhome.com/file/2023/1/59161_0s.jpg)
SpringBoot整合TomCat实现本地图片服务器代码解析
2021-07-30 16:34:19
Java中多媒体文件上传及页面回显的操作代码
2021-11-21 09:45:31
![](https://img.aspxhome.com/file/2023/2/66202_0s.png)
Mybatis中 mapper-locations和@MapperScan的作用
2023-07-13 08:08:34
![](https://img.aspxhome.com/file/2023/0/57700_0s.png)
java客户端Jedis操作Redis Sentinel 连接池的实现方法
2023-08-19 10:55:19
Java实现天天酷跑小游戏完整代码(附源码)
2021-08-03 09:43:43
![](https://img.aspxhome.com/file/2023/3/60833_0s.png)
BeanDefinitionRegistryPostProcessor如何动态注册Bean到Spring
2023-11-24 12:56:16
![](https://img.aspxhome.com/file/2023/1/59941_0s.jpg)
java 创建线程的几种方式
2023-10-29 19:40:16
![](https://img.aspxhome.com/file/2023/5/58695_0s.png)
学生视角带你了解Java内部类
2022-09-29 17:51:04
![](https://img.aspxhome.com/file/2023/9/62429_0s.png)
解决Mybatis的@Param()注解导致分页失效的问题
2022-05-01 13:24:53
![](https://img.aspxhome.com/file/2023/0/63400_0s.jpg)
java中@SuppressWarnings注解用法详解
2023-09-20 23:11:40
Java毕业设计实战之图片展览馆管理系统的实现
2021-06-16 08:23:18
![](https://img.aspxhome.com/file/2023/1/62521_0s.jpg)
springboot后端配置多个数据源、Mysql数据库的便捷方法
2022-05-01 07:21:37
Java 程序设计总复习题(java基础代码)
2022-05-11 07:02:31
Android四大组件之broadcast广播使用讲解
2023-07-27 12:41:50
![](https://img.aspxhome.com/file/2023/8/86008_0s.png)
在eclipse中中文汉字乱码的解决方案
2023-11-25 10:13:27
![](https://img.aspxhome.com/file/2023/6/60146_0s.png)
如何解决java压缩文件乱码问题
2022-05-20 09:15:57
详解Java包装类及自动装箱拆箱
2023-11-14 21:13:23
SpringMVC整合mybatis实例代码
2022-09-24 08:22:03