1. 基础实现(手动转换)
package com.itheima.miniblog.vo; import lombok.Data; @Data public class UserVo { private Integer id; private String username; // 脱敏的手机号 private String mobile; private String avatar; // 状态文本 private String statusText; /** * 构造方法:从 User 实体转换 */ public UserVo(Integer id, String username, String mobile, String avatar, Integer status) { this.id = id; this.username = username; this.mobile = maskMobile(mobile); // 脱敏处理 this.avatar = avatar; this.statusText = convertStatusText(status); } /** * 手机号脱敏方法 * 将 13812345678 转换为 138****5678 */ private String maskMobile(String mobile) { if (mobile == null || mobile.length() != 11) { return mobile; } return mobile.substring(0, 3) + "****" + mobile.substring(7); } /** * 状态转换方法 */ private String convertStatusText(Integer status) { if (status == null) return "未知"; switch (status) { case 0: return "禁用"; case 1: return "正常"; case 2: return "锁定"; case 3: return "待激活"; default: return "未知"; } } }2. 使用 Builder 模式(推荐)
package com.itheima.miniblog.vo; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @Data @Builder @NoArgsConstructor @AllArgsConstructor public class UserVo { private Integer id; private String username; private String mobile; private String avatar; private String statusText; // 自定义 setter 用于脱敏 public void setMobile(String mobile) { this.mobile = maskMobile(mobile); } public void setStatus(Integer status) { this.statusText = convertStatusText(status); } // 静态工厂方法 public static UserVo fromEntity(com.itheima.miniblog.entity.User user) { return UserVo.builder() .id(user.getId()) .username(user.getUsername()) .mobile(maskMobile(user.getMobile())) .avatar(user.getAvatar()) .statusText(convertStatusText(user.getStatus())) .build(); } private static String maskMobile(String mobile) { if (mobile == null || mobile.length() != 11) { return mobile; } return mobile.substring(0, 3) + "****" + mobile.substring(7); } private static String convertStatusText(Integer status) { if (status == null) return "未知"; return switch (status) { case 0 -> "禁用"; case 1 -> "正常"; case 2 -> "锁定"; case 3 -> "待激活"; default -> "未知"; }; } }3. 使用枚举优化状态管理
package com.itheima.miniblog.vo; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.Getter; @Data @Builder public class UserVo { private Integer id; private String username; private String mobile; private String avatar; private String statusText; // 状态枚举 @Getter @AllArgsConstructor public enum UserStatus { DISABLED(0, "禁用"), NORMAL(1, "正常"), LOCKED(2, "锁定"), PENDING(3, "待激活"); private final Integer code; private final String text; public static String getTextByCode(Integer code) { if (code == null) return "未知"; for (UserStatus status : values()) { if (status.getCode().equals(code)) { return status.getText(); } } return "未知"; } } // 转换方法 public static UserVo fromEntity(com.itheima.miniblog.entity.User user) { String maskedMobile = user.getMobile() != null && user.getMobile().length() == 11 ? user.getMobile().substring(0, 3) + "****" + user.getMobile().substring(7) : user.getMobile(); return UserVo.builder() .id(user.getId()) .username(user.getUsername()) .mobile(maskedMobile) .avatar(user.getAvatar()) .statusText(UserStatus.getTextByCode(user.getStatus())) .build(); } }4. 使用方法示例
Service 层使用:
@Service public class UserService { public UserVo getUserById(Integer id) { User user = userRepository.findById(id).orElseThrow(); // 方式1:使用构造方法 // return new UserVo(user.getId(), user.getUsername(), // user.getMobile(), user.getAvatar(), user.getStatus()); // 方式2:使用静态工厂方法(推荐) return UserVo.fromEntity(user); } public List<UserVo> getAllUsers() { List<User> users = userRepository.findAll(); // 使用 Stream 转换 return users.stream() .map(UserVo::fromEntity) .collect(Collectors.toList()); } }Controller 层使用:
@RestController @RequestMapping("/users") public class UserController { @Autowired private UserService userService; @GetMapping("/{id}") public Result<UserVo> getUser(@PathVariable Integer id) { UserVo userVo = userService.getUserById(id); return Result.success(userVo); } @GetMapping public Result<List<UserVo>> getAllUsers() { List<UserVo> userVos = userService.getAllUsers(); return Result.success(userVos); } } // 统一返回结果类 @Data class Result<T> { private Integer code; private String message; private T data; public static <T> Result<T> success(T data) { Result<T> result = new Result<>(); result.setCode(200); result.setMessage("成功"); result.setData(data); return result; } }5. 使用 MapStruct 自动化转换(高级)
如果项目中有很多 VO 转换,建议使用 MapStruct:
// 1. 添加依赖 // Maven <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> <version>1.5.5.Final</version> </dependency> // 2. 创建转换器接口 @Mapper(componentModel = "spring") public interface UserConverter { UserConverter INSTANCE = Mappers.getMapper(UserConverter.class); @Mapping(target = "mobile", expression = "java(maskMobile(user.getMobile()))") @Mapping(target = "statusText", expression = "java(convertStatusText(user.getStatus()))") UserVo toVo(User user); default String maskMobile(String mobile) { if (mobile == null || mobile.length() != 11) { return mobile; } return mobile.substring(0, 3) + "****" + mobile.substring(7); } default String convertStatusText(Integer status) { // 转换逻辑 return UserVo.UserStatus.getTextByCode(status); } } // 3. 在 Service 中使用 @Service public class UserService { @Autowired private UserConverter userConverter; public UserVo getUserById(Integer id) { User user = userRepository.findById(id).orElseThrow(); return userConverter.toVo(user); } }总结建议
基础项目:使用第 2 种方式(Builder 模式 + 静态工厂方法)
复杂项目:使用第 4 种方式(MapStruct 自动化转换)
关键点:
确保敏感字段不暴露
脱敏处理要完整
状态转换要清晰
提供方便的转换方法