好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java中VO的使用解析

java中VO的使用

场景

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

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;

     @ApiModelProperty (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列表:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

List<User> users = userServiceImpl.findUsersByIds(userIds);

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

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

//给UserVO添加角色属性

wrapRole(circleUserVOs,circleId);

wrapRole(List<CircleUserVO> circleUserVOs,Long circleId){

     //所有角色权限

     List<CircleUserVO> circleUserVOList = circleMemberRoleMapper.selectAllMemberRoleByCircleId(circleId);

     for (CircleUserVO circleUserVO:circleUserVOList){

         for (CircleUserVO circleUserVO1:circleUserVOs){

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

                 circleUserVO1.setRole(circleUserVO.getRole());

             }

         }

     }

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

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();

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

package com.dachen.util;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;

import java.util.List;

public class BeanUtil {

      public static <T> T copy(Object poObj, final Class <T>voClass)

      {

          T voObj = null ;

          try {

              voObj = voClass.newInstance();

              BeanUtils.copyProperties(poObj, voObj);

              return voObj;

          } catch (InstantiationException | IllegalAccessException e) {

              e.printStackTrace();

          }

          return null ;

      }

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

         List<T> voList= new ArrayList<T>();

         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一般是来做值的存储与传递。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/G0_hw/article/details/78326359

查看更多关于java中VO的使用解析的详细内容...

  阅读:44次