From c68d66f01a507290942b8fda60058eb1aa37b555 Mon Sep 17 00:00:00 2001 From: Cxx0822 <1556464090@qq.com> Date: Fri, 12 Dec 2025 10:41:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=96=B0=E5=A2=9EJsonView=E5=8D=9A?= =?UTF-8?q?=E5=AE=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/.vitepress/config.mts | 67 ++++-- docs/Web-Backend/{ => Others}/Flyway.md | 0 docs/Web-Backend/Others/JsonView-MapStruct.md | 194 ++++++++++++++++++ docs/Web-Backend/{ => Others}/MyBatis.md | 0 .../{ => Others}/MySQL-Knowledge.md | 0 docs/Web-Backend/{ => Others}/RustFS.md | 0 docs/Web-Backend/index.md | 31 +-- .../{ => Others}/JavaScript-Guide.md | 0 docs/Web-Front/{ => Vue}/Vue3-Common.md | 0 docs/Web-Front/index.md | 6 +- 10 files changed, 269 insertions(+), 29 deletions(-) rename docs/Web-Backend/{ => Others}/Flyway.md (100%) create mode 100644 docs/Web-Backend/Others/JsonView-MapStruct.md rename docs/Web-Backend/{ => Others}/MyBatis.md (100%) rename docs/Web-Backend/{ => Others}/MySQL-Knowledge.md (100%) rename docs/Web-Backend/{ => Others}/RustFS.md (100%) rename docs/Web-Front/{ => Others}/JavaScript-Guide.md (100%) rename docs/Web-Front/{ => Vue}/Vue3-Common.md (100%) diff --git a/docs/.vitepress/config.mts b/docs/.vitepress/config.mts index c36cba2..b0de066 100644 --- a/docs/.vitepress/config.mts +++ b/docs/.vitepress/config.mts @@ -22,16 +22,22 @@ export default defineConfig({ { text: 'Home', link: '/' }, { text: 'Web前端', items: [ - { text: 'Vue3-Common', link: '/Web-Front/Vue3-Common' }, - { text: 'JavaScript知识点整理', link: '/Web-Front/JavaScript-Guide' } + { + text: 'Vue', + items: [ + { text: 'Vue3-Common', link: '/Web-Front/Vue/Vue3-Common' }, + ] + }, + { + text: '其他', + items: [ + { text: 'JavaScript知识点整理', link: '/Web-Front/Others/JavaScript-Guide' } + ] + }, ] }, { text: 'Web后端', items: [ - { text: 'MySQL知识点', link: '/Web-Backend/MySQL-Knowledge' }, - { text: 'Flyway简单使用', link: '/Web-Backend/Flyway' }, - { text: 'MyBatis简介和使用', link: '/Web-Backend/MyBatis' }, - { text: 'RustFS简介和使用', link: '/Web-Backend/RustFS' }, { text: 'SpringBoot', items: [ @@ -49,7 +55,17 @@ export default defineConfig({ { text: 'FastAPI基础教程', link: '/Web-Backend/FastAPI/FastAPI-Guide' }, { text: '基于OAuth2的安全验证', link: '/Web-Backend/FastAPI/OAuth2' } ] - } + }, + { + text: '其他', + items: [ + { text: 'MySQL知识点', link: '/Web-Backend/Others/MySQL-Knowledge' }, + { text: 'Flyway简单使用', link: '/Web-Backend/Others/Flyway' }, + { text: 'MyBatis简介和使用', link: '/Web-Backend/Others/MyBatis' }, + { text: 'RustFS简介和使用', link: '/Web-Backend/Others/RustFS' }, + { text: 'JsonView和MapStruct使用', link: '/Web-Backend/Others/JsonView-MapStruct' }, + ] + }, ] }, { text: 'DevOps', @@ -72,8 +88,18 @@ export default defineConfig({ { text: 'Web前端', items: [ - { text: 'Vue3-Common', link: '/Web-Front/Vue3-Common' }, - { text: 'JavaScript知识点整理', link: '/Web-Front/JavaScript-Guide' } + { + text: 'Vue', + items: [ + { text: 'Vue3-Common', link: '/Web-Front/Vue/Vue3-Common' } + ] + }, + { + text: 'Others', + items: [ + { text: 'JavaScript知识点整理', link: '/Web-Front/Others/JavaScript-Guide' } + ] + }, ] }, ], @@ -81,10 +107,6 @@ export default defineConfig({ { text: 'Web后端', items: [ - { text: 'MySQL知识点', link: '/Web-Backend/MySQL-Knowledge' }, - { text: 'Flyway简单使用', link: '/Web-Backend/Flyway' }, - { text: 'MyBatis简介和使用', link: '/Web-Backend/MyBatis' }, - { text: 'RustFS简介和使用', link: '/Web-Backend/RustFS' }, { text: 'SpringBoot', items: [ { text: 'Spring IOC简介', link: '/Web-Backend/SpringBoot/SpringIOC' }, @@ -100,6 +122,15 @@ export default defineConfig({ { text: 'FastAPI基础教程', link: '/Web-Backend/FastAPI/FastAPI-Guide' }, { text: '基于OAuth2的安全验证', link: '/Web-Backend/FastAPI/OAuth2' } ] + }, + { text: '其他', + items: [ + { text: 'MySQL知识点', link: '/Web-Backend/Others/MySQL-Knowledge' }, + { text: 'Flyway简单使用', link: '/Web-Backend/Others/Flyway' }, + { text: 'MyBatis简介和使用', link: '/Web-Backend/Others/MyBatis' }, + { text: 'RustFS简介和使用', link: '/Web-Backend/Others/RustFS' }, + { text: 'JsonView和MapStruct使用', link: '/Web-Backend/Others/JsonView-MapStruct' }, + ] } ], }, @@ -113,6 +144,16 @@ export default defineConfig({ { text: 'ElasticSearch部署和使用', link: '/DevOps/ElasticSearch' } ] } + ], + '/Flutter/': [ + { + text: 'Flutter', + items: [ + { text: 'Dart基础教程', link: '/Flutter/DartGuide' }, + { text: 'Flutter基础教程', link: '/Flutter/FlutterGuide' }, + { text: '实战技巧', link: '/Flutter/Practice' } + ] + } ] }, diff --git a/docs/Web-Backend/Flyway.md b/docs/Web-Backend/Others/Flyway.md similarity index 100% rename from docs/Web-Backend/Flyway.md rename to docs/Web-Backend/Others/Flyway.md diff --git a/docs/Web-Backend/Others/JsonView-MapStruct.md b/docs/Web-Backend/Others/JsonView-MapStruct.md new file mode 100644 index 0000000..fee09ce --- /dev/null +++ b/docs/Web-Backend/Others/JsonView-MapStruct.md @@ -0,0 +1,194 @@ +# 一、JsonView +  JsonView 是 Jackson 提供的注解,用于控制对象序列化/反序列化时包含哪些字段。可以实现: +- 不同接口返回不同字段 +- 敏感字段过滤 +- 前后端数据分离 + +## 1.1 添加依赖 +```xml + + + + org.springframework.boot + spring-boot-starter-web + + +``` + +## 1.2 使用 +1. 定义视图接口 +```java +// JsonView 视图定义 +public class Views { + // 基础视图 - 所有接口都包含的字段 + public interface Basic {} + + // 返回给前端时使用 + public interface Response extends Basic {} + + // 接收前端数据时使用 + public interface Request extends Basic {} + + // 管理视图 - 包含所有字段 + public interface AdminView extends ReadView {} +} +``` + +2. Dto对象使用JsonView +```java +@Data +public class UserDTO { + @JsonView(Views.Response.class) + private Long id; + + @JsonView(Views.Request.class) + @NotBlank(message = "密码不能为空") + @Size(min = 6, message = "密码至少6位") + private String password; + + @JsonView(Views.Basic.class) + @NotBlank(message = "用户名不能为空") + private String username; + + @JsonView(Views.Basic.class) + @Email(message = "邮箱格式不正确") + private String email; + + @JsonView(Views.Basic.class) + private String nickname; + + @JsonView(AdminView.class) + private Boolean isActive; +} +``` + +3. Controller中使用 +```java +@RestController +@RequestMapping("/api/users") +public class UserController { + + @Autowired + private UserService userService; + + @PostMapping + @JsonView(UserDTO.Response.class) + public UserDTO createUser(@RequestBody @JsonView(UserDTO.Request.class) UserDTO userDTO) { + return userService.createUser(userDTO); + } + + @PutMapping("/{id}") + @JsonView(UserDTO.Response.class) + public UserDTO updateUser(@PathVariable Long id, + @RequestBody @JsonView(UserDTO.Request.class) UserDTO userDTO) { + return userService.updateUser(id, userDTO); + } + + @GetMapping("/{id}") + @JsonView(UserDTO.Response.class) + public UserDTO getUser(@PathVariable Long id) { + return userService.getUserById(id); + } + + @GetMapping("/{id}/admin") + @JsonView(UserDTO.AdminView.class) + public UserDTO getAdminUser(@PathVariable Long id) { + return userService.getAdminUserById(id); + } +} +``` + +# 二、MapStruct +  MapStruct 是一个 Java 注解处理器,用于生成类型安全的 Bean 映射代码: +- 编译时生成映射代码,无运行时性能损失 +- 类型安全 +- 支持复杂映射 + +## 2.1 添加依赖 +```xml + + 1.5.5.Final + + + + + org.mapstruct + mapstruct + ${org.mapstruct.version} + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.11.0 + + + + org.mapstruct + mapstruct-processor + ${org.mapstruct.version} + + + + + + +``` + +## 2.2 使用 +```java +import org.mapstruct.*; + +import java.util.List; +import java.time.format.DateTimeFormatter; + +@Mapper +public interface UserMapper { + + @Mapping(target = "name", source = "userName") // 字段名不同 + @Mapping(target = "phoneNumber", source = "phone") // 字段名不同 + @Mapping(target = "description", source = "remark") // 字段名不同 + @Mapping(target = "statusDesc", ignore = true) // 需要特殊处理 + @Mapping(target = "createTime", ignore = true) // 需要格式化 + UserDTO toDTO(User user); + + @Mapping(target = "userName", source = "name") + @Mapping(target = "phone", source = "phoneNumber") + @Mapping(target = "remark", source = "description") + @Mapping(target = "password", ignore = true) // 密码不映射 + User toEntity(UserDTO dto); + + // 列表映射 + List toDTOList(List users); + + // 带默认值的映射 + @Mapping(target = "name", source = "userName") + @Mapping(target = "phoneNumber", source = "phone") + @Mapping(target = "description", source = "remark", defaultValue = "暂无描述") + @Mapping(target = "statusDesc", expression = "java(convertStatus(user.getStatus()))") + @Mapping(target = "createTime", expression = "java(formatTime(user.getCreateTime()))") + UserDTO toDTOWithDefault(User user); + + // 自定义转换方法 + default String convertStatus(Integer status) { + if (status == null) return "未知"; + switch (status) { + case 0: return "禁用"; + case 1: return "正常"; + case 2: return "锁定"; + default: return "未知"; + } + } + + // 时间格式化方法 + default String formatTime(LocalDateTime time) { + if (time == null) return ""; + return time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); + } +} +``` + +  需要执行`mvn compile`后检查`target/generated-sources`文件夹下是否存在生成的转换代码。 \ No newline at end of file diff --git a/docs/Web-Backend/MyBatis.md b/docs/Web-Backend/Others/MyBatis.md similarity index 100% rename from docs/Web-Backend/MyBatis.md rename to docs/Web-Backend/Others/MyBatis.md diff --git a/docs/Web-Backend/MySQL-Knowledge.md b/docs/Web-Backend/Others/MySQL-Knowledge.md similarity index 100% rename from docs/Web-Backend/MySQL-Knowledge.md rename to docs/Web-Backend/Others/MySQL-Knowledge.md diff --git a/docs/Web-Backend/RustFS.md b/docs/Web-Backend/Others/RustFS.md similarity index 100% rename from docs/Web-Backend/RustFS.md rename to docs/Web-Backend/Others/RustFS.md diff --git a/docs/Web-Backend/index.md b/docs/Web-Backend/index.md index b87bdbe..d39e6c2 100644 --- a/docs/Web-Backend/index.md +++ b/docs/Web-Backend/index.md @@ -8,25 +8,26 @@ description: Web后端开发技术文档,包含数据库、框架、中间件 后端开发技术栈的学习笔记和总结。 -## 主要内容 - -- [MySQL知识点](/Web-Backend/MySQL-Knowledge) -- [Flyway简单使用](/Web-Backend/Flyway) -- [MyBatis简介和使用](/Web-Backend/MyBatis) -- [RustFS简介和使用](/Web-Backend/RustFS) - ### 🍃 SpringBoot SpringBoot 框架学习系列: -- [Spring IOC简介](/SpringBoot/SpringIOC) -- [SpringBoot启动流程](/SpringBoot/SpringBoot-Start-Process) -- [Spring MVC简介](/SpringBoot/SpringMVC) -- [SpringBoot Starter原理](/SpringBoot/SpringBootStarter) -- [SpringBoot Bean简介](/SpringBoot/SpringBootBean) -- [SpringBoot3原生镜像](/SpringBoot/SpringBoot3-GraalVM) +- [Spring IOC简介](/Web-Backend/SpringBoot/SpringIOC) +- [SpringBoot启动流程](/Web-Backend/SpringBoot/SpringBoot-Start-Process) +- [Spring MVC简介](/Web-Backend/SpringBoot/SpringMVC) +- [SpringBoot Starter原理](/Web-Backend/SpringBoot/SpringBootStarter) +- [SpringBoot Bean简介](/Web-Backend/SpringBoot/SpringBootBean) +- [SpringBoot3原生镜像](/Web-Backend/SpringBoot/SpringBoot3-GraalVM) ### 🐍 FastAPI Python FastAPI 框架学习: -- [FastAPI基础教程](/FastAPI/FastAPI-Guide) -- [基于OAuth2的FastApi安全验证](/FastAPI/OAuth2) \ No newline at end of file +- [FastAPI基础教程](/Web-Backend/FastAPI/FastAPI-Guide) +- [基于OAuth2的FastApi安全验证](/Web-Backend/FastAPI/OAuth2) + +### 🔄 其他 + +- [MySQL知识点](/Web-Backend/Others/MySQL-Knowledge) +- [Flyway简单使用](/Web-Backend/Others/Flyway) +- [MyBatis简介和使用](/Web-Backend/Others/MyBatis) +- [RustFS简介和使用](/Web-Backend/Others/RustFS) +- [JsonView和MapStruct使用](/Web-Backend/SpringBoot/JsonView-MapStruct) \ No newline at end of file diff --git a/docs/Web-Front/JavaScript-Guide.md b/docs/Web-Front/Others/JavaScript-Guide.md similarity index 100% rename from docs/Web-Front/JavaScript-Guide.md rename to docs/Web-Front/Others/JavaScript-Guide.md diff --git a/docs/Web-Front/Vue3-Common.md b/docs/Web-Front/Vue/Vue3-Common.md similarity index 100% rename from docs/Web-Front/Vue3-Common.md rename to docs/Web-Front/Vue/Vue3-Common.md diff --git a/docs/Web-Front/index.md b/docs/Web-Front/index.md index 8c2b07c..fb3b573 100644 --- a/docs/Web-Front/index.md +++ b/docs/Web-Front/index.md @@ -8,7 +8,11 @@ description: Web前端开发技术文档 前端开发技术栈的学习笔记和总结。 -## 主要内容 +### 🌿 Vue +Vue 框架学习: - [Vue3-Common](/Web-Front/Vue3-Common) + +### 🔄 其他 + - [JavaScript知识点整理](/Web-Front/JavaScript-Guide) \ No newline at end of file