java中VO的使用解析

网友投稿 331 2022-11-29

java中VO的使用解析

目录java中VO的使用场景java里VO是什么1、PO:persistant object 持久对象2、VO:value object值对象

java中VO的使用

场景

现在我们需要从数据库中查询用户列表tTpmuyDyNv_user,对应的实体类如下:

import io.swagger.annotations.ApiModelProperty;

public class User {

@ApiModelProperty(value = "用户id")

private String userId;

@ApiModelProperty(value = "用户名称")

private String name;

/**

* 状态参考 UserStatus

*/

@ApiModelProperty(value = "用户状态 1已认证,2 认证中,3未通过认证,7未提交认证")

private Integer status;

@ApiModelProperthttp://y(value = "头像地址")

private String headPicFileName;

@ApiModelProperty(value = "手机号")

private String telephone;

/**

* 用户级别 0到期 1游客 2临时用户 3认证用户 参考health com.dachen.health.commons.vo.User

*/

private Integer userLevel;

@ApiModelProperty(value = "医生信息")

private Doctor doctor;

get/setXxx()....

@Override

public boolean equals(Object o) {

if (this == o) {

return true;

}

if (o == null || getClass() != o.getClass()) {

return false;

}

User user = (User) o;

return userId != null ? userId.equals(user.userId) : user.userId == null;

}

}

但是前端页面需要展示更多个关于用户的消息,如用户的角色Role,而User实体类中的信息不全,为了返回更多的信息,

有两种做法:

1.直接在user类中添加需要的信息属性

2.创建一个新类UserVO extends User,只在UserVO中添加更多属性,而且以VO结尾表示用于返回前端的数据

如果采用第一种做法,User类可能会越来越大,也不方便后期维护,而且User类作为对数据库表的映射,添加冗余的属性反而会破坏这种映射关系,采取第二种方法,更加清晰明确.

现在采用第二种方法,假设从数据中查出user列表:

List users = userServiceImpl.findUsersByIds(userIds);

//将User列表转换为UserVO列表:

List circleUserVOs=BeanUtil.copyList(users,CircleUserVO.class);

//给UserVO添加角色属性

wrapRole(circleUserVOs,circleId);

wrapRole(List circleUserVOs,Long circleId){

//所有角色权限

List circleUserVOList = circleMemberRoleMapper.selectAllMemberRoleByCircleId(circleId);

for (CircleUserVO circleUserVO:circleUserVOList){

for (CircleUserVO circleUserVO1:circleUserVOs){

if(circleUserVO.getUserId().equals(circleUserVO1.getUserId())){

circleUserVO1.setRole(circleUserVO.getRole());

}

}

}

}

package com.dachen.circle.model.vo;

import com.dachen.circle.model.inner.User;

import io.swagger.annotations.ApiModelProperty;

public class CircleUserVO extends User{

@ApiModelProperty(value = "在该圈子的角色1:管理员 2:圈主(负责人)3:顾问 逗号拼接 多个角色 可同时为管理员,圈主,顾问")

private String role;

@ApiModelProperty(value = "0否 1是 永久免费")

private Integer permanentFree;

@ApiModelProperty(value = "1正常 2欠费")

private Integer arrearageStatus;

@ApiModelProperty(value = "过期时间 月数")

private Integer expirationMonth;

@ApiModelProperty(value = "医院名称")

private String hospital;

@ApiModelProperty(value = "科室")

private String departments;

@ApiModelProperty(value = "职称")

private String title;

@ApiModelProperty(value = "简介")

private String introduction;

@ApiModelProperty(value = "排序字母 A-Z Z1为#")

private String letter;

get/setXxx();

}

package com.dachen.util;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;

import java.util.List;

public class BeanUtil {

public static Thttp:// copy(Object poObj,final Class voClass)

{

T voObj =null;

try {

voObj = voClass.newInstance();

BeanUtils.copyProperties(poObj, voObj);

return voObj;

} catch (InstantiationException | IllegalAccessException e) {

e.printStackTrace();

}

return null;

}

public static List copyList(List extends Object> poList ,final Class voClass){

List voList=new ArrayList();

T voObj =null;

for(Object poObj:poList){

try {

voObj = voClass.newInstance();

BeanUtils.copyProperties(poObj, voObj);

voList.add(voObj);

} catch (InstantiationException | IllegalAccessException e) {

e.printStackTrace();

}

System.out.println(voObj);

}

return voList;

}

}

java里VO是什么

1、PO:persistant object 持久对象

可以看成是与数据库中的表相映射的java对象。使用Hibernate来生成PO是不错的选择。

2、VO:value object值对象

通常用于业务层之间的数据传递,和PO一样也是仅仅包含数据而已。但应是抽象出的业务对象

可以和表对应,也可以不,这根据业务的需要.

有一种观点就是:PO只能用在数据层,VO用在商业逻辑层和表示层。各层操作属于该层自己的数据对象

这样就可以降低各层之间的耦合,便于以后系统的维护和扩展。

如果将PO用在各个层中就相当于我们使用全局变量,我们知道在OO设计非常不赞成使用全局变量。

但是每次都得进行VO-PO的转换,也确实很烦。我觉得有时候也可以在某个商业逻辑或者表示层使用PO

此时在这个商业逻辑的过程中PO的状态是不发生变化的,比如显示一条商品详细信息的商业逻辑。

在开发过的项目中,规模都很小,我一直都把PO当VO用,因为PO确实很方便,结合Hibernate的DAO

我使用JAVA的集合对象作为值传递的载体,当然Struts也是我的不二之选。

我认为:在一些直观的,简单的,不易发生变化的,不需要涉及多个PO时,传递值还是使用PO好

这样可以减少大量的工作量(也就意味着减少bug,减少风险),也不需要担心未来的维护工作!

vo:value object,值对象

一般在java中用的多的是pojo:plain oriented java object

原始java对象,pojo一般和数据库中的表是一一对应的。

vo一般是来做值的存储与传递。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:stirling 数
下一篇:hdu 1316 How Many Fibs?(Fibonacci+高精度+二分)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~