java中VO的使用
場(chǎng)景
現(xiàn)在我們需要從數(shù)據(jù)庫中查詢用戶列表t_user,對(duì)應(yīng)的實(shí)體類如下:
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; /** * 狀態(tài)參考 UserStatus */ @ApiModelProperty (value = "用戶狀態(tài) 1已認(rèn)證,2 認(rèn)證中,3未通過認(rèn)證,7未提交認(rèn)證" ) private Integer status; @ApiModelProperty (value = "頭像地址" ) private String headPicFileName; @ApiModelProperty (value = "手機(jī)號(hào)" ) private String telephone; /** * 用戶級(jí)別 0到期 1游客 2臨時(shí)用戶 3認(rèn)證用戶 參考health com.dachen.health.commons.vo.User */ private Integer userLevel; @ApiModelProperty (value = "醫(yī)生信息" ) 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 ; } } |
但是前端頁面需要展示更多個(gè)關(guān)于用戶的消息,如用戶的角色Role,而User實(shí)體類中的信息不全,為了返回更多的信息,
有兩種做法:
- 1.直接在user類中添加需要的信息屬性
- 2.創(chuàng)建一個(gè)新類UserVO extends User,只在UserVO中添加更多屬性,而且以VO結(jié)尾表示用于返回前端的數(shù)據(jù)
如果采用第一種做法,User類可能會(huì)越來越大,也不方便后期維護(hù),而且User類作為對(duì)數(shù)據(jù)庫表的映射,添加冗余的屬性反而會(huì)破壞這種映射關(guān)系,采取第二種方法,更加清晰明確.
現(xiàn)在采用第二種方法,假設(shè)從數(shù)據(jù)中查出user列表:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
List<User> users = userServiceImpl.findUsersByIds(userIds); //將User列表轉(zhuǎn)換為UserVO列表: List<CircleUserVO> circleUserVOs=BeanUtil.copyList(users,CircleUserVO. class ); //給UserVO添加角色屬性 wrapRole(circleUserVOs,circleId); wrapRole(List<CircleUserVO> circleUserVOs,Long circleId){ //所有角色權(quán)限 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:圈主(負(fù)責(zé)人)3:顧問 逗號(hào)拼接 多個(gè)角色 可同時(shí)為管理員,圈主,顧問" ) private String role; @ApiModelProperty (value = "0否 1是 永久免費(fèi)" ) private Integer permanentFree; @ApiModelProperty (value = "1正常 2欠費(fèi)" ) private Integer arrearageStatus; @ApiModelProperty (value = "過期時(shí)間 月數(shù)" ) private Integer expirationMonth; @ApiModelProperty (value = "醫(yī)院名稱" ) private String hospital; @ApiModelProperty (value = "科室" ) private String departments; @ApiModelProperty (value = "職稱" ) private String title; @ApiModelProperty (value = "簡(jiǎn)介" ) 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 持久對(duì)象
可以看成是與數(shù)據(jù)庫中的表相映射的java對(duì)象。使用Hibernate來生成PO是不錯(cuò)的選擇。
2、VO:value object值對(duì)象
通常用于業(yè)務(wù)層之間的數(shù)據(jù)傳遞,和PO一樣也是僅僅包含數(shù)據(jù)而已。但應(yīng)是抽象出的業(yè)務(wù)對(duì)象
可以和表對(duì)應(yīng),也可以不,這根據(jù)業(yè)務(wù)的需要.
有一種觀點(diǎn)就是:PO只能用在數(shù)據(jù)層,VO用在商業(yè)邏輯層和表示層。各層操作屬于該層自己的數(shù)據(jù)對(duì)象
這樣就可以降低各層之間的耦合,便于以后系統(tǒng)的維護(hù)和擴(kuò)展。
如果將PO用在各個(gè)層中就相當(dāng)于我們使用全局變量,我們知道在OO設(shè)計(jì)非常不贊成使用全局變量。
但是每次都得進(jìn)行VO-PO的轉(zhuǎn)換,也確實(shí)很煩。我覺得有時(shí)候也可以在某個(gè)商業(yè)邏輯或者表示層使用PO
此時(shí)在這個(gè)商業(yè)邏輯的過程中PO的狀態(tài)是不發(fā)生變化的,比如顯示一條商品詳細(xì)信息的商業(yè)邏輯。
在開發(fā)過的項(xiàng)目中,規(guī)模都很小,我一直都把PO當(dāng)VO用,因?yàn)镻O確實(shí)很方便,結(jié)合Hibernate的DAO
我使用JAVA的集合對(duì)象作為值傳遞的載體,當(dāng)然Struts也是我的不二之選。
我認(rèn)為:在一些直觀的,簡(jiǎn)單的,不易發(fā)生變化的,不需要涉及多個(gè)PO時(shí),傳遞值還是使用PO好
這樣可以減少大量的工作量(也就意味著減少bug,減少風(fēng)險(xiǎn)),也不需要擔(dān)心未來的維護(hù)工作!
vo:value object,值對(duì)象
一般在java中用的多的是pojo:plain oriented java object
原始java對(duì)象,pojo一般和數(shù)據(jù)庫中的表是一一對(duì)應(yīng)的。
vo一般是來做值的存儲(chǔ)與傳遞。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/G0_hw/article/details/78326359