场景:现在我们需要从数据库中查询用户列表t_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;
@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列表:
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());
}
}
}
}
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 <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;
}
}