Compare commits
37 Commits
1d6a08a16f
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 6bf9a3295a | |||
| 0a7e38f931 | |||
| fc9cb14daf | |||
| 4abb65129b | |||
| 8ef18a8e85 | |||
| 22811e4adb | |||
| 28bcc1a118 | |||
| e9b0e79d48 | |||
| 18cd1dbed8 | |||
| 2db84152b5 | |||
| 28baf5600b | |||
| 015016a2da | |||
| 7b555492ee | |||
| e7fa23a365 | |||
| 2adf4951f7 | |||
| 3692657b64 | |||
| f81217f14a | |||
| 611988d7ab | |||
| 324d646b27 | |||
| d1392f89a3 | |||
| 93eded44cb | |||
| 53d94c5f4c | |||
| af4d13d76c | |||
| a8e8ec0ef2 | |||
| 02b9636fec | |||
| 54c12deb25 | |||
| e6a48d8e88 | |||
| c839dfc4e3 | |||
| 1b48081d5b | |||
| e4c0dc4884 | |||
| 8fc53e6fda | |||
| 919664ba84 | |||
| f439381e04 | |||
| 657f9593ba | |||
| af4be9db8f | |||
| d08f9db9ac | |||
| 5bf6e9ecdc |
11
.gitignore
vendored
11
.gitignore
vendored
@@ -54,12 +54,7 @@ buildNumber.properties
|
||||
*.rar
|
||||
hs_err_pid*
|
||||
replay_pid*
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
!.vscode/*.code-snippets
|
||||
.vscode/
|
||||
.history/
|
||||
*.vsix
|
||||
.metadata
|
||||
@@ -132,3 +127,7 @@ Icon
|
||||
Network Trash Folder
|
||||
Temporary Items
|
||||
.apdisk
|
||||
.opencode
|
||||
.claude
|
||||
.kilocode
|
||||
openspec/changes/archive
|
||||
506
README.md
506
README.md
@@ -1,469 +1,109 @@
|
||||
# Spring Boot Service Template
|
||||
|
||||
这是一个基于 Spring Boot 的服务模板项目,旨在为开发者提供一个标准化的微服务基础结构,简化新项目的搭建过程,提高开发效率。
|
||||
微服务快速开发能力模板,v1.1.0-SNAPSHOT。Java 17, Spring Boot 4.0.0, Spring Cloud 2025.1.0。
|
||||
|
||||
## 目录
|
||||
GroupId: `com.lanyuanxiaoyao`,根包: `com.lanyuanxiaoyao.service.template`。
|
||||
|
||||
- [1. 项目概述](#1-项目概述)
|
||||
- [2. 技术架构](#2-技术架构)
|
||||
- [3. 项目结构](#3-项目结构)
|
||||
- [4. 核心功能](#4-核心功能)
|
||||
- [5. 使用指南](#5-使用指南)
|
||||
- [6. 开发规范](#6-开发规范)
|
||||
- [7. 项目集成指南](#7-项目集成指南)
|
||||
## 模块
|
||||
|
||||
## 1. 项目概述
|
||||
|
||||
### 1.1 项目简介
|
||||
|
||||
Spring Boot Service Template 是一个标准化的微服务基础结构模板,专为 Java 开发者和微服务架构设计者打造。该项目提供了一套完整的 CRUD 操作框架,通过泛型支持不同类型的数据转换,大大减少了重复代码的编写。
|
||||
|
||||
该模板内置了完善的实体审计机制,自动维护实体的创建时间和修改时间。同时,提供了强大的查询功能,支持多种条件查询、分页和排序功能。
|
||||
|
||||
### 1.2 项目目标
|
||||
|
||||
提供统一的服务模板,简化新项目的搭建过程,提高开发效率。通过封装通用的业务逻辑,让开发者能够专注于核心业务功能的实现。
|
||||
|
||||
### 1.3 核心特性
|
||||
|
||||
- 标准化的项目结构:遵循业界最佳实践的目录结构和代码组织方式
|
||||
- 简化依赖管理和构建流程:基于 Maven 的依赖管理,清晰的构建配置
|
||||
- 支持快速构建和部署微服务:提供完整的微服务基础组件
|
||||
- 泛型支持:通过泛型实现不同类型间的数据转换
|
||||
- 完善的审计机制:自动维护实体的创建时间和修改时间
|
||||
- 强大的查询功能:支持多种条件查询、分页和排序
|
||||
- 灵活的扩展机制:易于定制和扩展的架构设计
|
||||
|
||||
### 1.4 适用场景
|
||||
|
||||
适用于需要快速搭建 Spring Boot 微服务的项目,特别是那些需要大量 CRUD 操作的业务系统。
|
||||
|
||||
## 2. 技术架构
|
||||
|
||||
### 2.1 技术选型
|
||||
|
||||
#### 2.1.1 后端技术栈
|
||||
- Java 17
|
||||
- Spring Boot 3.4.3
|
||||
- Spring Data JPA
|
||||
- QueryDSL 7.0
|
||||
- Lombok
|
||||
- Fenix Spring Boot Starter 3.1.0
|
||||
|
||||
#### 2.1.2 核心框架
|
||||
- Spring Boot 作为核心框架,提供自动配置和快速开发能力
|
||||
- Spring Data JPA 用于数据访问,简化数据库操作
|
||||
- QueryDSL 用于类型安全的查询构建,避免运行时错误
|
||||
- Fenix 用于复杂动态查询,提供更灵活的查询能力
|
||||
- Lombok 减少样板代码,提高开发效率
|
||||
|
||||
#### 2.1.3 数据库技术
|
||||
- H2 Database (测试环境)
|
||||
|
||||
### 2.2 架构设计
|
||||
|
||||
#### 2.2.1 整体架构图
|
||||
```
|
||||
┌─────────────────────────────────────┐
|
||||
│ Controller Layer │
|
||||
│ (处理HTTP请求,数据转换与响应) │
|
||||
├─────────────────────────────────────┤
|
||||
│ Service Layer │
|
||||
│ (业务逻辑处理,事务管理) │
|
||||
├─────────────────────────────────────┤
|
||||
│ Repository Layer │
|
||||
│ (数据访问,数据库交互) │
|
||||
├─────────────────────────────────────┤
|
||||
│ Entity Layer │
|
||||
│ (数据模型定义,实体映射) │
|
||||
└─────────────────────────────────────┘
|
||||
spring-boot-service-template/ (根 POM, packaging=pom)
|
||||
├── spring-boot-service-template-common/ (jar — 通用工具)
|
||||
└── spring-boot-service-template-database/ (jar — JPA 数据库能力)
|
||||
```
|
||||
|
||||
#### 2.2.2 模块划分
|
||||
- controller: 控制层,处理 HTTP 请求,包括接口定义和支持类
|
||||
- entity: 实体层,定义数据模型和数据库映射
|
||||
- service: 服务层,处理业务逻辑和事务管理
|
||||
- repository: 仓储层,处理数据访问和数据库交互
|
||||
- helper: 辅助类模块,提供工具类和通用方法
|
||||
模块命名规则: `spring-boot-service-template-{capability}[-{impl}]`
|
||||
|
||||
#### 2.2.3 设计模式
|
||||
- 模板方法模式:通过抽象类定义通用操作流程
|
||||
- 策略模式:通过函数式接口实现数据转换策略
|
||||
- 仓储模式:封装数据访问逻辑,提供统一的数据操作接口
|
||||
## 包结构
|
||||
|
||||
## 3. 项目结构
|
||||
|
||||
### 3.1 目录结构说明
|
||||
```
|
||||
src/
|
||||
├── main/
|
||||
│ └── java/
|
||||
│ └── com/lanyuanxiaoyao/service/template/
|
||||
│ ├── controller/
|
||||
│ ├── entity/
|
||||
│ ├── helper/
|
||||
│ ├── repository/
|
||||
│ └── service/
|
||||
└── test/
|
||||
└── java/
|
||||
└── com/lanyuanxiaoyao/service/template/
|
||||
├── controller/
|
||||
├── entity/
|
||||
├── repository/
|
||||
└── service/
|
||||
com.lanyuanxiaoyao.service.template.{module}/
|
||||
configuration/ # @Configuration(用 configuration 不用 config)
|
||||
controller/ # REST 控制器 + 接口
|
||||
entity/ # JPA 实体、Query、GlobalResponse 等数据对象
|
||||
exception/ # 自定义异常(用 exception 不用 exceptions)
|
||||
helper/ # 工具类(用 helper 不用 util/utils)
|
||||
repository/ # Spring Data 仓库
|
||||
service/ # 业务服务
|
||||
```
|
||||
|
||||
### 3.2 核心模块介绍
|
||||
无独立 DTO/VO 包,DTO 作为 Controller 内部 record。
|
||||
|
||||
#### 3.2.1 controller模块
|
||||
提供基础的 CRUD 操作控制器接口和支持类。主要包含:
|
||||
- [SimpleController](src/main/java/com/lanyuanxiaoyao/service/template/controller/SimpleController.java):定义基础 CRUD 接口
|
||||
- [SimpleControllerSupport](src/main/java/com/lanyuanxiaoyao/service/template/controller/SimpleControllerSupport.java):实现基础 CRUD 功能
|
||||
- [Query](src/main/java/com/lanyuanxiaoyao/service/template/controller/Query.java):查询条件封装类
|
||||
- 其他辅助接口如 SaveController、ListController、DetailController、RemoveController
|
||||
## database 模块
|
||||
|
||||
#### 3.2.2 entity模块
|
||||
定义基础实体类,包含审计字段。主要包含:
|
||||
- [IdOnlyEntity](src/main/java/com/lanyuanxiaoyao/service/template/entity/IdOnlyEntity.java):仅包含 ID 的基础实体
|
||||
- [SimpleEntity](src/main/java/com/lanyuanxiaoyao/service/template/entity/SimpleEntity.java):包含基础字段的实体类,继承自 IdOnlyEntity
|
||||
单表 CRUD → REST 接口快速实现框架。基于 JPA + Fenix + QueryDSL + MapStruct。
|
||||
|
||||
#### 3.2.3 service模块
|
||||
提供基础服务接口和支持类。主要包含:
|
||||
- [SimpleService](src/main/java/com/lanyuanxiaoyao/service/template/service/SimpleService.java):定义基础服务接口
|
||||
- [SimpleServiceSupport](src/main/java/com/lanyuanxiaoyao/service/template/service/SimpleServiceSupport.java):实现基础服务功能
|
||||
**文档**:
|
||||
- [开发指南](docs/database-development.md) - 模块架构、核心设计、技术实现、扩展指南
|
||||
- [使用指南](docs/database-usage.md) - 快速开始、API 接口、查询条件、高级用法
|
||||
|
||||
#### 3.2.4 repository模块
|
||||
定义数据访问仓储接口。主要包含:
|
||||
- [SimpleRepository](src/main/java/com/lanyuanxiaoyao/service/template/repository/SimpleRepository.java):基础仓储接口
|
||||
## 开发规范
|
||||
|
||||
#### 3.2.5 helper模块
|
||||
提供辅助工具类。主要包含:
|
||||
- [ObjectHelper](src/main/java/com/lanyuanxiaoyao/service/template/helper/ObjectHelper.java):对象帮助类
|
||||
### 命名
|
||||
|
||||
### 3.3 测试模块结构
|
||||
测试模块包含完整的测试用例,覆盖 controller、entity、repository 和 service 各层。通过实际的业务实体(如 Employee、Company、Report)演示如何使用模板。
|
||||
| 类别 | 模式 | 示例 |
|
||||
| --------------- | ---------------------------------------- | ---------------------------------- |
|
||||
| 实体基类 | `*Entity` | `IdOnlyEntity`, `SimpleEntity` |
|
||||
| 业务实体 | 纯名词 | `Employee` |
|
||||
| 控制器接口 | `*Controller` | `SaveController` |
|
||||
| 控制器抽象基类 | `*ControllerSupport` | `SimpleControllerSupport` |
|
||||
| 服务接口 | `*Service` | `SaveService` |
|
||||
| 服务抽象基类 | `*ServiceSupport` | `SimpleServiceSupport` |
|
||||
| 仓库 | `*Repository` | `EmployeeRepository` |
|
||||
| 工具类 | `*Helper` | `ObjectHelper` |
|
||||
| 配置类 | `*Configuration` | `QueryDSLConfiguration` |
|
||||
| 异常 | `*Exception` | `IdNotFoundException` |
|
||||
| 内部 DTO record | `SaveItem`/`ListItem`/`DetailItem` | Controller 内部 |
|
||||
|
||||
## 4. 核心功能
|
||||
- 接口不加 `I` 前缀
|
||||
- 方法: 动词开头 `save()`/`list()`/`detail()`/`remove()`;布尔用 `is`/`has` 前缀
|
||||
- 常量 UPPER_SNAKE_CASE;字段常量用 `@FieldNameConstants` 生成 `{Class}.Fields.{field}`
|
||||
- 泛型: 简单用 `T`/`E`,领域用 `ENTITY`/`SAVE_ITEM`/`LIST_ITEM`/`DETAIL_ITEM`
|
||||
|
||||
### 4.1 基础CRUD操作
|
||||
### 代码风格
|
||||
|
||||
#### 4.1.1 创建(Create)
|
||||
支持通过 POST 请求创建实体对象。通过 save 接口实现,接收 SAVE_ITEM 类型的参数,通过 Mapper 转换为实体对象后保存。
|
||||
- 2 空格缩进,K&R 大括号,不用 Tab
|
||||
- Import 排序: `java.*` → `jakarta.*` → 第三方 → `com.lanyuanxiaoyao.*`,不用通配符
|
||||
- 积极使用 Java 17 特性: `record`(DTO) / `var` / `instanceof` 模式匹配 / Text Blocks / `String.formatted()`
|
||||
- JavaDoc 用中文,含 `@param`/`@return`/`@see`,可用 `<p>`/`<h3>`/`<ul>`/`<pre>`
|
||||
- 不写 `//` 行内注释,除非必要
|
||||
|
||||
#### 4.1.2 查询(Retrieve)
|
||||
支持多种查询方式:
|
||||
- 列表查询:获取所有实体对象
|
||||
- 条件查询:根据指定条件查询实体对象
|
||||
- 详情查询:根据 ID 获取单个实体对象
|
||||
### 注解
|
||||
|
||||
#### 4.1.3 更新(Update)
|
||||
支持通过 POST 请求更新实体对象。通过 save 接口实现,当传入包含 ID 的对象时执行更新操作。
|
||||
用:
|
||||
|
||||
#### 4.1.4 删除(Delete)
|
||||
支持通过 GET 请求删除实体对象。通过 remove 接口实现,根据 ID 删除指定实体。
|
||||
- 实体: `@Getter` `@Setter` `@ToString` `@FieldNameConstants`,子类加 `@ToString(callSuper = true)`
|
||||
- 日志: `@Slf4j`
|
||||
- 注入: `@RequiredArgsConstructor(access = AccessLevel.PROTECTED)`(构造器注入)
|
||||
- 事务: 写 `@Transactional(rollbackFor = Throwable.class)`,读 `@Transactional(readOnly = true)`
|
||||
- 实体基类: `@MappedSuperclass` `@EntityListeners(AuditingEntityListener.class)`
|
||||
|
||||
### 4.2 查询功能详解
|
||||
不用:
|
||||
|
||||
#### 4.2.1 简单查询
|
||||
支持基于 ID 的简单查询,通过 detail 接口实现。
|
||||
- 不用 `@Valid`/`@NotNull` 等校验注解
|
||||
- 不用 Swagger/OpenAPI 注解
|
||||
- 不用 `@Autowired` 字段注入
|
||||
- 不写代码注释(`//`),除非必要
|
||||
|
||||
#### 4.2.2 条件查询
|
||||
支持基于多种条件的复杂查询,通过 list 接口实现,支持以下查询条件:
|
||||
- nullEqual: 字段值为null的条件
|
||||
- notNullEqual: 字段值不为null的条件
|
||||
- empty: 字段值为空的条件
|
||||
- notEmpty: 字段值不为空的条件
|
||||
- equal: 字段值相等的条件
|
||||
- notEqual: 字段值不相等的条件
|
||||
- like: 字段值模糊匹配的条件
|
||||
- notLike: 字段值不模糊匹配的条件
|
||||
- great: 字段值大于的条件
|
||||
- less: 字段值小于的条件
|
||||
- greatEqual: 字段值大于等于的条件
|
||||
- lessEqual: 字段值小于等于的条件
|
||||
- in: 字段值在指定范围内的条件
|
||||
- notIn: 字段值不在指定范围内的条件
|
||||
- between: 字段值在指定区间内的条件
|
||||
- notBetween: 字段值不在指定区间内的条件
|
||||
### 异常
|
||||
|
||||
#### 4.2.3 分页查询
|
||||
支持分页查询功能,通过 page 配置实现分页参数设置。
|
||||
继承 `RuntimeException`,构造器用 `String.formatted()`。查无数据用 `Optional.orElseThrow(() -> new IdNotFoundException(id))`。无全局 `@ControllerAdvice`。
|
||||
|
||||
#### 4.2.4 排序查询
|
||||
支持排序查询功能,通过 sort 配置实现排序参数设置。
|
||||
### 依赖管理
|
||||
|
||||
### 4.3 数据实体设计
|
||||
版本集中在根 POM `<properties>` + `<dependencyManagement>`,子模块不声明版本。
|
||||
全局继承: `lombok` / `jspecify:1.0.0`
|
||||
核心: `spring-boot-starter-data-jpa` / `fenix-spring-boot-starter:4.0.0` / `querydsl-jpa:7.1` / `mapstruct:1.6.3`
|
||||
database 模块注解处理器: lombok → hibernate-jpamodelgen → querydsl-apt(jpa) → jakarta.persistence-api → mapstruct-processor
|
||||
|
||||
#### 4.3.1 基础实体类
|
||||
定义通用的实体基类,包括 IdOnlyEntity 和 SimpleEntity。
|
||||
### 通用原则
|
||||
|
||||
#### 4.3.2 审计字段
|
||||
包含创建时间(createdTime)和修改时间(modifiedTime)等审计字段,通过 Spring Data JPA 的审计功能自动维护。
|
||||
- 文档/注释/commit 用中文,代码标识符用英文
|
||||
- 不引入新依赖前先复用已有组件,优先 JDK 和 Spring 内置能力
|
||||
- 构造器注入,不使用 `@Autowired` 字段注入
|
||||
|
||||
#### 4.3.3 实体关系
|
||||
支持常见的实体关系映射,如一对一、一对多、多对多等。
|
||||
### 构建
|
||||
|
||||
## 5. 使用指南
|
||||
|
||||
### 5.1 环境准备
|
||||
|
||||
#### 5.1.1 JDK安装
|
||||
需要安装 JDK 17 或更高版本。
|
||||
|
||||
#### 5.1.2 Maven配置
|
||||
需要配置 Maven 3.x 环境。
|
||||
|
||||
### 5.2 项目构建
|
||||
|
||||
#### 5.2.1 依赖管理
|
||||
通过 Maven 管理项目依赖。
|
||||
|
||||
#### 5.2.2 编译打包
|
||||
使用 `mvn clean package` 命令编译打包项目。
|
||||
|
||||
### 5.3 运行部署
|
||||
|
||||
#### 5.3.1 本地运行
|
||||
可以使用 `mvn spring-boot:run` 命令运行项目。
|
||||
|
||||
#### 5.3.2 生产部署
|
||||
可通过生成的 JAR 文件直接运行或部署到服务器,使用命令:
|
||||
`java -jar target/spring-boot-service-template-1.0-SNAPSHOT.jar`
|
||||
|
||||
## 6. 开发规范
|
||||
|
||||
### 6.1 代码规范
|
||||
遵循 Java 标准编码规范和 Spring Boot 最佳实践。
|
||||
|
||||
### 6.2 接口规范
|
||||
RESTful API 设计规范,统一的响应格式。
|
||||
|
||||
### 6.3 注释规范
|
||||
遵循标准的 JavaDoc 格式编写注释,详细说明类、方法、参数和返回值的含义。
|
||||
|
||||
### 6.4 异常处理
|
||||
统一异常处理机制,提供友好的错误信息。
|
||||
|
||||
## 7. 项目集成指南
|
||||
|
||||
本章节详细介绍如何在现有项目中集成 Spring Boot Service Template 的能力。
|
||||
|
||||
### 7.1 集成方式
|
||||
|
||||
通过 Maven 依赖引入(推荐)
|
||||
|
||||
### 7.2 Maven 依赖引入方式
|
||||
|
||||
#### 7.2.1 添加依赖
|
||||
|
||||
在您的项目 pom.xml 文件中添加以下依赖:
|
||||
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>com.lanyuanxiaoyao</groupId>
|
||||
<artifactId>spring-boot-service-template</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
```bash
|
||||
mvn clean package
|
||||
```
|
||||
|
||||
#### 7.2.2 配置依赖管理
|
||||
|
||||
确保您的项目中包含以下依赖管理配置:
|
||||
|
||||
```xml
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<!-- spring boot 相关依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring-cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
```
|
||||
|
||||
#### 7.2.3 配置编译插件
|
||||
|
||||
确保您的项目中包含以下编译插件配置:
|
||||
|
||||
```xml
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.14.0</version>
|
||||
<configuration>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.36</version>
|
||||
</path>
|
||||
<path>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-jpamodelgen</artifactId>
|
||||
<version>6.6.3.Final</version>
|
||||
</path>
|
||||
<path>
|
||||
<groupId>io.github.openfeign.querydsl</groupId>
|
||||
<artifactId>querydsl-jpa</artifactId>
|
||||
<version>7.0</version>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
```
|
||||
|
||||
### 7.4 创建业务模块
|
||||
|
||||
创建业务模块的步骤如下:
|
||||
|
||||
#### 7.4.1 创建实体类
|
||||
|
||||
创建实体类并继承 [SimpleEntity](src/main/java/com/lanyuanxiaoyao/service/template/entity/SimpleEntity.java):
|
||||
|
||||
```java
|
||||
@Entity
|
||||
@Table(name = "employee")
|
||||
public class Employee extends SimpleEntity {
|
||||
private String name;
|
||||
private Integer age;
|
||||
// 其他字段和方法
|
||||
}
|
||||
```
|
||||
|
||||
#### 7.4.2 创建仓储接口
|
||||
|
||||
创建仓储接口并继承 [SimpleRepository](src/main/java/com/lanyuanxiaoyao/service/template/repository/SimpleRepository.java):
|
||||
|
||||
```java
|
||||
@Repository
|
||||
public interface EmployeeRepository extends SimpleRepository<Employee> {
|
||||
// 自定义查询方法
|
||||
}
|
||||
```
|
||||
|
||||
#### 7.4.3 创建服务类
|
||||
|
||||
创建服务类并继承 [SimpleServiceSupport](src/main/java/com/lanyuanxiaoyao/service/template/service/SimpleServiceSupport.java):
|
||||
|
||||
```java
|
||||
@Service
|
||||
public class EmployeeService extends SimpleServiceSupport<Employee> {
|
||||
public EmployeeService(EmployeeRepository repository) {
|
||||
super(repository);
|
||||
}
|
||||
// 自定义业务方法
|
||||
}
|
||||
```
|
||||
|
||||
#### 7.4.4 创建控制器类
|
||||
|
||||
创建控制器类并继承 [SimpleControllerSupport](src/main/java/com/lanyuanxiaoyao/service/template/controller/SimpleControllerSupport.java):
|
||||
|
||||
```java
|
||||
@RestController
|
||||
@RequestMapping("employee")
|
||||
public class EmployeeController extends SimpleControllerSupport<Employee, EmployeeSaveItem, EmployeeListItem, EmployeeDetailItem> {
|
||||
public EmployeeController(EmployeeService service) {
|
||||
super(service);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<EmployeeSaveItem, Employee> saveItemMapper() {
|
||||
// 实现保存项转换逻辑
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<Employee, EmployeeListItem> listItemMapper() {
|
||||
// 实现列表项转换逻辑
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<Employee, EmployeeDetailItem> detailItemMapper() {
|
||||
// 实现详情项转换逻辑
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### 7.4.5 创建数据传输对象
|
||||
|
||||
创建用于数据传输的对象:
|
||||
|
||||
```java
|
||||
// 保存项
|
||||
public class EmployeeSaveItem {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Integer age;
|
||||
// getter和setter方法
|
||||
}
|
||||
|
||||
// 列表项
|
||||
public class EmployeeListItem {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Integer age;
|
||||
// getter和setter方法
|
||||
}
|
||||
|
||||
// 详情项
|
||||
public class EmployeeDetailItem {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Integer age;
|
||||
private LocalDateTime createdTime;
|
||||
private LocalDateTime modifiedTime;
|
||||
// getter和setter方法
|
||||
}
|
||||
```
|
||||
|
||||
### 7.5 配置启用 JPA 审计
|
||||
|
||||
在您的主应用类上添加 [@EnableJpaAuditing](https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/config/EnableJpaAuditing.html) 注解以启用 JPA 审计功能:
|
||||
|
||||
```java
|
||||
@SpringBootApplication
|
||||
@EnableJpaAuditing
|
||||
public class YourApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(YourApplication.class, args);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 7.6 配置 Fenix
|
||||
|
||||
在您的 application.yml 或 application.properties 文件中添加 Fenix 配置:
|
||||
|
||||
```yaml
|
||||
fenix:
|
||||
# 是否开启 Fenix
|
||||
enabled: true
|
||||
# SQL 执行后的输出格式,console: 控制台彩色输出; html: 彩色 HTML 输出; text: 纯文本输出
|
||||
output-format: console
|
||||
```
|
||||
|
||||
### 7.7 测试集成效果
|
||||
|
||||
完成以上步骤后,您可以运行您的应用程序并测试以下功能:
|
||||
|
||||
1. 创建实体:POST /employee/save
|
||||
2. 查询列表:GET /employee/list 或 POST /employee/list(带条件)
|
||||
3. 查询详情:GET /employee/detail/{id}
|
||||
4. 删除实体:GET /employee/remove/{id}
|
||||
|
||||
通过以上步骤,您就可以成功在现有项目中集成 Spring Boot Service Template 的能力,快速实现 CRUD 功能。
|
||||
205
docs/database-development.md
Normal file
205
docs/database-development.md
Normal file
@@ -0,0 +1,205 @@
|
||||
# Database 模块开发指南
|
||||
|
||||
单表 CRUD → REST 接口快速实现框架。基于 JPA + Fenix + QueryDSL + MapStruct。
|
||||
|
||||
## 架构
|
||||
|
||||
```
|
||||
Controller (REST) → Service (业务) → Repository (数据访问) → Entity (模型)
|
||||
```
|
||||
|
||||
### 组件结构
|
||||
|
||||
| 包 | 组件 | 职责 |
|
||||
|---|---|---|
|
||||
| entity | IdOnlyEntity, SimpleEntity | 实体基类 |
|
||||
| entity | SnowflakeId, SnowflakeIdGenerator | ID 生成 |
|
||||
| entity | Query, GlobalResponse, Page | 查询/响应封装 |
|
||||
| repository | SimpleRepository | 统一数据访问接口 |
|
||||
| service | SaveService, QueryService, RemoveService | 功能接口 |
|
||||
| service | SimpleService, SimpleServiceSupport | 组合接口与实现 |
|
||||
| service | QueryParser | 查询条件解析 |
|
||||
| controller | SaveController, QueryController, RemoveController | REST 接口 |
|
||||
| controller | SimpleController, SimpleControllerSupport | 组合接口与实现 |
|
||||
| helper | DatabaseHelper, SnowflakeHelper | 工具类 |
|
||||
| exception | *Exception | 异常定义 |
|
||||
|
||||
## 核心设计
|
||||
|
||||
### 实体继承
|
||||
|
||||
```
|
||||
IdOnlyEntity (id: Long, @SnowflakeId)
|
||||
↑
|
||||
SimpleEntity (+ createdTime, modifiedTime)
|
||||
↑
|
||||
业务实体 (@Entity)
|
||||
```
|
||||
|
||||
**实现要点**:
|
||||
- `@MappedSuperclass` 标记基类
|
||||
- `@SnowflakeId` 触发 `SnowflakeIdGenerator` 生成 ID
|
||||
- `@CreatedDate/@LastModifiedDate` + `AuditingEntityListener` 自动填充时间
|
||||
|
||||
### Repository
|
||||
|
||||
```java
|
||||
@NoRepositoryBean
|
||||
public interface SimpleRepository<E> extends
|
||||
FenixJpaRepository<E, Long>, // CRUD + Fenix
|
||||
FenixJpaSpecificationExecutor<E>, // Specification
|
||||
ListQueryByExampleExecutor<E>, // Example
|
||||
ListQuerydslPredicateExecutor<E> {} // QueryDSL
|
||||
```
|
||||
|
||||
**核心能力**:
|
||||
- `saveOrUpdateByNotNullProperties()` - 部分字段更新
|
||||
- Specification - 动态条件查询
|
||||
- QueryDSL - 类型安全查询
|
||||
|
||||
### Service 接口组合
|
||||
|
||||
```java
|
||||
SaveService<ENTITY> // save(entity), save(entities)
|
||||
QueryService<ENTITY> // detail(id), list(), list(query), count()
|
||||
RemoveService<ENTITY> // remove(id), remove(ids)
|
||||
|
||||
SimpleService<ENTITY> extends SaveService, QueryService, RemoveService
|
||||
```
|
||||
|
||||
**SimpleServiceSupport 实现**:
|
||||
- 保存:Fenix `saveOrUpdateByNotNullProperties()`
|
||||
- 查询:JPA Criteria + Specification
|
||||
- 删除:`deleteBatchByIds()`
|
||||
- 扩展点:重写 `commonPredicates()` 添加全局过滤条件
|
||||
|
||||
### Controller 接口组合
|
||||
|
||||
```java
|
||||
SaveController<SAVE_ITEM> // POST /save
|
||||
QueryController<LIST_ITEM, DETAIL_ITEM> // GET/POST /list, GET /detail/{id}
|
||||
RemoveController // GET /remove/{id}
|
||||
|
||||
SimpleController<SAVE_ITEM, LIST_ITEM, DETAIL_ITEM>
|
||||
```
|
||||
|
||||
**SimpleControllerSupport 实现**:
|
||||
- 调用 Service 方法
|
||||
- 通过 Mapper 转换 DTO ↔ Entity
|
||||
- 封装 GlobalResponse
|
||||
- 扩展点:实现 `saveItemMapper()`, `listItemMapper()`, `detailItemMapper()`
|
||||
|
||||
### 查询条件
|
||||
|
||||
**Query 结构**:`Query(query: Queryable, sort: List<Sortable>, page: Pageable)`
|
||||
|
||||
**QueryParser**:抽象类定义解析接口,`JpaQueryParser` 转换为 JPA Predicate
|
||||
|
||||
**支持操作**:
|
||||
|
||||
| 类别 | 操作 |
|
||||
|---|---|
|
||||
| 空值 | nullEqual, notNullEqual, empty, notEmpty |
|
||||
| 相等 | equal, notEqual |
|
||||
| 模糊 | like, notLike, contain, notContain |
|
||||
| 前后缀 | startWith, endWith, notStartWith, notEndWith |
|
||||
| 比较 | great, less, greatEqual, lessEqual |
|
||||
| 区间 | between, notBetween |
|
||||
| 集合 | inside, notInside |
|
||||
|
||||
**实现**:JPA CriteriaBuilder 构建,支持多级字段路径(如 `user.name`),自动类型转换(枚举、LocalDateTime)
|
||||
|
||||
### 响应格式
|
||||
|
||||
```java
|
||||
GlobalResponse<T>(status, message, data)
|
||||
// status: 0 成功, 500 失败
|
||||
// 列表: data = ListItem(items, total)
|
||||
// 详情: data = DetailItem(item)
|
||||
```
|
||||
|
||||
## 技术细节
|
||||
|
||||
### 雪花算法
|
||||
|
||||
`SnowflakeHelper`:64 位 Long,1 位符号 + 41 位时间戳 + 10 位机器 ID + 12 位序列号
|
||||
|
||||
`SnowflakeIdGenerator`:实现 `IdentifierGenerator`,持久化时调用 `SnowflakeHelper.next()`
|
||||
|
||||
### 部分更新
|
||||
|
||||
Fenix `saveOrUpdateByNotNullProperties()`:
|
||||
- 自动判断 INSERT/UPDATE
|
||||
- 仅更新非 null 字段
|
||||
|
||||
### 命名策略
|
||||
|
||||
`PhysicalNamingStrategySnakeCaseImpl`:camelCase → snake_case
|
||||
|
||||
### 注解处理器
|
||||
|
||||
执行顺序:lombok → hibernate-jpamodelgen → querydsl-apt → mapstruct-processor
|
||||
|
||||
生成:getter/setter、JPA 元模型(_Entity)、QueryDSL Q 类(QEntity)、MapStruct Mapper
|
||||
|
||||
## 工具类
|
||||
|
||||
### DatabaseHelper
|
||||
|
||||
```java
|
||||
// 生成 DDL
|
||||
generateDDL(entityPackages, ddlFilePath, dialect, jdbc, username, password, driver)
|
||||
|
||||
// 生成脚手架
|
||||
generateBasicFiles(entityPackages, projectRootPackage, projectRootPath, override)
|
||||
```
|
||||
|
||||
### SnowflakeHelper
|
||||
|
||||
```java
|
||||
Long id = SnowflakeHelper.next();
|
||||
```
|
||||
|
||||
## 扩展指南
|
||||
|
||||
### 软删除
|
||||
|
||||
1. 创建 `SoftDeleteEntity extends SimpleEntity`,添加 `deleted` 字段
|
||||
2. 重写 `commonPredicates()` 返回 `deleted = false` 条件
|
||||
3. 覆盖 `remove()` 改为更新 `deleted` 字段
|
||||
|
||||
### 多租户
|
||||
|
||||
1. 创建 `TenantEntity extends SimpleEntity`,添加 `tenantId` 字段
|
||||
2. 重写 `commonPredicates()` 添加租户过滤
|
||||
3. ThreadLocal 或 Spring Security 存储当前租户
|
||||
|
||||
### 审计字段
|
||||
|
||||
在 `SimpleEntity` 添加 `createdBy`, `modifiedBy`,配合 Spring Security 获取当前用户
|
||||
|
||||
### 自定义查询操作符
|
||||
|
||||
1. `Query.Queryable` 添加字段
|
||||
2. `QueryParser` 添加抽象方法
|
||||
3. `JpaQueryParser` 实现转换为 Predicate
|
||||
|
||||
## 测试
|
||||
|
||||
- H2 内存数据库
|
||||
- `@DataJpaTest` 测试 Repository
|
||||
- `@WebMvcTest` 测试 Controller
|
||||
- 测试用例:`src/test/java/.../integration/`
|
||||
|
||||
## 依赖
|
||||
|
||||
核心:spring-boot-starter-data-jpa, fenix-spring-boot-starter:4.0.0, querydsl-jpa:7.1, mapstruct:1.6.3
|
||||
|
||||
传递:spring-boot-service-template-common
|
||||
|
||||
## 注意事项
|
||||
|
||||
- 事务:写操作 `@Transactional(rollbackFor = Throwable.class)`,读操作 `@Transactional(readOnly = true)`
|
||||
- 异常:不使用全局处理器,直接抛出
|
||||
- 性能:批量操作使用批量方法
|
||||
- 线程安全:GlobalResponse 用 record 保证不可变,SnowflakeHelper 用原子变量
|
||||
409
docs/database-usage.md
Normal file
409
docs/database-usage.md
Normal file
@@ -0,0 +1,409 @@
|
||||
# Database 模块使用指南
|
||||
|
||||
单表 CRUD → REST 接口快速实现框架。
|
||||
|
||||
## 使用模式
|
||||
|
||||
### Web 应用
|
||||
|
||||
引入 database 模块,创建 Entity → Repository → Service → Controller,实现 REST 接口。
|
||||
|
||||
### 非 Web 应用
|
||||
|
||||
**无需引入 web 依赖**(database 模块的 `spring-boot-starter-web` scope 为 `provided`)。
|
||||
|
||||
仅使用 Entity → Repository → Service,直接注入 Service 使用:
|
||||
|
||||
```java
|
||||
@SpringBootApplication
|
||||
public class Application implements CommandLineRunner {
|
||||
@Autowired
|
||||
private EmployeeService service;
|
||||
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
Employee emp = new Employee();
|
||||
emp.setName("张三");
|
||||
Long id = service.save(emp);
|
||||
Employee found = service.detail(id);
|
||||
List<Employee> list = service.list();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
适用:批处理、定时任务、数据迁移、命令行应用、后台服务。
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 1. 添加依赖
|
||||
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>com.lanyuanxiaoyao</groupId>
|
||||
<artifactId>spring-boot-service-template-database</artifactId>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
### 2. 创建实体
|
||||
|
||||
```java
|
||||
@Getter @Setter
|
||||
@ToString(callSuper = true)
|
||||
@FieldNameConstants
|
||||
@Entity
|
||||
@Table(name = "employee")
|
||||
public class Employee extends SimpleEntity {
|
||||
@Column(comment = "员工姓名", nullable = false)
|
||||
private String name;
|
||||
|
||||
@Column(comment = "部门ID")
|
||||
private Long departmentId;
|
||||
|
||||
@Column(comment = "邮箱")
|
||||
private String email;
|
||||
}
|
||||
```
|
||||
|
||||
继承 `SimpleEntity` 自动获得 `id`(雪花算法)、`createdTime`、`modifiedTime`(自动填充)。
|
||||
|
||||
### 3. 创建 Repository
|
||||
|
||||
```java
|
||||
@Repository
|
||||
public interface EmployeeRepository extends SimpleRepository<Employee> {}
|
||||
```
|
||||
|
||||
继承能力:CRUD、分页、Specification、QueryDSL、Example。
|
||||
|
||||
### 4. 创建 Service
|
||||
|
||||
```java
|
||||
@Slf4j
|
||||
@Service
|
||||
public class EmployeeService extends SimpleServiceSupport<Employee> {
|
||||
public EmployeeService(EmployeeRepository repository) {
|
||||
super(repository);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
自动获得完整 CRUD 能力。
|
||||
|
||||
### 5. 创建 Controller(Web 应用)
|
||||
|
||||
```java
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("employee")
|
||||
public class EmployeeController
|
||||
extends SimpleControllerSupport<Employee, EmployeeController.SaveItem, EmployeeController.ListItem, EmployeeController.DetailItem> {
|
||||
|
||||
private final EmployeeService service;
|
||||
|
||||
public EmployeeController(EmployeeService service) {
|
||||
super(service);
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<SaveItem, Employee> saveItemMapper() {
|
||||
return item -> {
|
||||
Employee entity = new Employee();
|
||||
entity.setId(item.id());
|
||||
entity.setName(item.name());
|
||||
entity.setDepartmentId(item.departmentId());
|
||||
entity.setEmail(item.email());
|
||||
return entity;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<Employee, ListItem> listItemMapper() {
|
||||
return entity -> new ListItem(
|
||||
entity.getId(), entity.getName(), entity.getDepartmentId(),
|
||||
entity.getEmail(), entity.getCreatedTime()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<Employee, DetailItem> detailItemMapper() {
|
||||
return entity -> new DetailItem(
|
||||
entity.getId(), entity.getName(), entity.getDepartmentId(),
|
||||
entity.getEmail(), entity.getCreatedTime(), entity.getModifiedTime()
|
||||
);
|
||||
}
|
||||
|
||||
public record SaveItem(Long id, String name, Long departmentId, String email) {}
|
||||
public record ListItem(Long id, String name, Long departmentId, String email, LocalDateTime createdTime) {}
|
||||
public record DetailItem(Long id, String name, Long departmentId, String email, LocalDateTime createdTime, LocalDateTime modifiedTime) {}
|
||||
}
|
||||
```
|
||||
|
||||
实现三个 Mapper:`saveItemMapper()`, `listItemMapper()`, `detailItemMapper()`
|
||||
|
||||
## 代码生成
|
||||
|
||||
```java
|
||||
DatabaseHelper.generateBasicFiles(
|
||||
"com.example.entity", // 实体包
|
||||
"com.example", // 项目根包
|
||||
"./src/main/java/com/example", // 源码路径
|
||||
false // 是否覆盖
|
||||
);
|
||||
```
|
||||
|
||||
生成 Repository、Service、Controller。
|
||||
|
||||
## API 接口(Web 应用)
|
||||
|
||||
### POST /{entity}/save
|
||||
|
||||
保存/更新实体。
|
||||
|
||||
请求(新增):
|
||||
```json
|
||||
{"name": "张三", "departmentId": 1, "email": "zhangsan@example.com"}
|
||||
```
|
||||
|
||||
请求(更新):
|
||||
```json
|
||||
{"id": 123456789, "name": "李四"}
|
||||
```
|
||||
|
||||
响应:
|
||||
```json
|
||||
{"status": 0, "message": "OK", "data": 123456789}
|
||||
```
|
||||
|
||||
特性:不传 id 为新增,传 id 为更新(仅更新非 null 字段)。
|
||||
|
||||
### GET/POST /{entity}/list
|
||||
|
||||
GET:获取全部列表
|
||||
|
||||
POST:条件查询
|
||||
```json
|
||||
{
|
||||
"query": {
|
||||
"equal": {"departmentId": 1},
|
||||
"like": {"name": "%张%"},
|
||||
"greatEqual": {"createdTime": "2026-01-01 00:00:00"}
|
||||
},
|
||||
"sort": [{"column": "createdTime", "direction": "DESC"}],
|
||||
"page": {"index": 1, "size": 20}
|
||||
}
|
||||
```
|
||||
|
||||
响应:
|
||||
```json
|
||||
{"status": 0, "message": "OK", "data": {"items": [...], "total": 100}}
|
||||
```
|
||||
|
||||
### GET /{entity}/detail/{id}
|
||||
|
||||
响应:
|
||||
```json
|
||||
{"status": 0, "message": "OK", "data": {"id": 123, "name": "张三", ...}}
|
||||
```
|
||||
|
||||
ID 不存在返回 500。
|
||||
|
||||
### GET /{entity}/remove/{id}
|
||||
|
||||
响应:
|
||||
```json
|
||||
{"status": 0, "message": "OK", "data": null}
|
||||
```
|
||||
|
||||
## 查询条件
|
||||
|
||||
### Query 结构
|
||||
|
||||
```java
|
||||
Query(
|
||||
query: Queryable, // 查询条件
|
||||
sort: List<Sortable>, // 排序
|
||||
page: Pageable // 分页
|
||||
)
|
||||
```
|
||||
|
||||
### 查询操作
|
||||
|
||||
| 操作 | 类型 | 示例 |
|
||||
|---|---|---|
|
||||
| equal | Map | `{"name": "张三"}` |
|
||||
| notEqual | Map | `{"status": "DELETED"}` |
|
||||
| like | Map | `{"name": "%张%"}` |
|
||||
| contain | Map | `{"name": "张"}` → `%张%` |
|
||||
| startWith | Map | `{"name": "张"}` → `张%` |
|
||||
| endWith | Map | `{"name": "三"}` → `%三` |
|
||||
| great/greatEqual | Map | `{"age": 18}` |
|
||||
| less/lessEqual | Map | `{"age": 60}` |
|
||||
| between | Map | `{"age": {"start": 18, "end": 60}}` |
|
||||
| inside | Map | `{"id": [1, 2, 3]}` |
|
||||
| notInside | Map | `{"status": ["DELETED"]}` |
|
||||
| nullEqual | List | `["deletedAt"]` |
|
||||
| notNullEqual | List | `["email"]` |
|
||||
|
||||
### 排序
|
||||
|
||||
```json
|
||||
{"sort": [{"column": "createdTime", "direction": "DESC"}]}
|
||||
```
|
||||
|
||||
direction: `ASC` 升序,`DESC` 降序
|
||||
|
||||
### 分页
|
||||
|
||||
```json
|
||||
{"page": {"index": 1, "size": 20}}
|
||||
```
|
||||
|
||||
index 从 1 开始,默认 `(1, 10)`,无排序默认 `createdTime DESC`
|
||||
|
||||
## 高级用法
|
||||
|
||||
### 扩展 Service
|
||||
|
||||
```java
|
||||
@Service
|
||||
public class EmployeeService extends SimpleServiceSupport<Employee> {
|
||||
private final EmployeeRepository repository;
|
||||
|
||||
public EmployeeService(EmployeeRepository repository) {
|
||||
super(repository);
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
// 自定义方法
|
||||
public List<Employee> findByDepartmentId(Long departmentId) {
|
||||
return repository.findAll(
|
||||
(root, query, builder) -> builder.equal(root.get("departmentId"), departmentId)
|
||||
);
|
||||
}
|
||||
|
||||
// 全局过滤条件
|
||||
@Override
|
||||
protected Predicate commonPredicates(Root<Employee> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
|
||||
return builder.equal(root.get("deleted"), false); // 软删除过滤
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### QueryDSL 查询
|
||||
|
||||
```java
|
||||
public List<Employee> findActiveEmployees() {
|
||||
QEmployee q = QEmployee.employee;
|
||||
return repository.findAll(q.status.eq("ACTIVE").and(q.deleted.isFalse()));
|
||||
}
|
||||
```
|
||||
|
||||
### MapStruct Mapper
|
||||
|
||||
```java
|
||||
@Mapper
|
||||
public interface EmployeeMapper {
|
||||
Employee toEntity(SaveItem item);
|
||||
ListItem toListItem(Employee entity);
|
||||
DetailItem toDetailItem(Employee entity);
|
||||
}
|
||||
|
||||
// Controller 中使用
|
||||
@Override
|
||||
protected Function<SaveItem, Employee> saveItemMapper() {
|
||||
return mapper::toEntity;
|
||||
}
|
||||
```
|
||||
|
||||
## 实体设计
|
||||
|
||||
### 字段类型
|
||||
|
||||
- ID: `Long`(雪花算法)
|
||||
- 时间: `LocalDateTime`
|
||||
- 金额: `BigDecimal`
|
||||
- 枚举: Java enum(存储为字符串)
|
||||
- 布尔: `Boolean`
|
||||
|
||||
### 关联关系
|
||||
|
||||
```java
|
||||
@Entity
|
||||
public class Order extends SimpleEntity {
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "customer_id")
|
||||
private Customer customer;
|
||||
|
||||
@OneToMany(mappedBy = "order", fetch = FetchType.LAZY)
|
||||
private List<OrderItem> items;
|
||||
}
|
||||
```
|
||||
|
||||
注意:谨慎使用 `@OneToMany`,可能导致 N+1 问题。
|
||||
|
||||
### 索引
|
||||
|
||||
```java
|
||||
@Entity
|
||||
@Table(name = "employee", indexes = {
|
||||
@Index(name = "idx_department", columnList = "department_id")
|
||||
})
|
||||
public class Employee extends SimpleEntity { ... }
|
||||
```
|
||||
|
||||
## 工具类
|
||||
|
||||
### DatabaseHelper
|
||||
|
||||
```java
|
||||
// 生成 DDL
|
||||
DatabaseHelper.generateDDL(
|
||||
"com.example.entity", "./sql", MySQL8Dialect.class,
|
||||
"jdbc:mysql://localhost:3306/test", "root", "password",
|
||||
com.mysql.cj.jdbc.Driver.class
|
||||
);
|
||||
```
|
||||
|
||||
### SnowflakeHelper
|
||||
|
||||
```java
|
||||
Long id = SnowflakeHelper.next();
|
||||
```
|
||||
|
||||
## 常见问题
|
||||
|
||||
**Q: 非 Web 应用如何使用?**
|
||||
|
||||
A: 不引入 web 依赖,创建 Entity → Repository → Service,直接注入 Service 使用。
|
||||
|
||||
**Q: 如何实现软删除?**
|
||||
|
||||
A: 添加 `deleted` 字段,重写 `commonPredicates()` 过滤,覆盖 `remove()` 改为更新。
|
||||
|
||||
**Q: 如何处理复杂查询?**
|
||||
|
||||
A: 使用 QueryDSL 或 Repository `@Query` 方法:
|
||||
```java
|
||||
@Query("SELECT e FROM Employee e WHERE e.departmentId = :deptId")
|
||||
List<Employee> findByDepartment(@Param("deptId") Long deptId);
|
||||
```
|
||||
|
||||
**Q: 如何批量插入?**
|
||||
|
||||
A: `service.save(entities)` 或 `repository.saveAll(entities)`
|
||||
|
||||
**Q: 查询条件支持关联对象吗?**
|
||||
|
||||
A: 支持,使用多级路径如 `"department.name"`
|
||||
|
||||
## 最佳实践
|
||||
|
||||
1. DTO 设计:SaveItem 可修改字段,ListItem 列表字段,DetailItem 完整字段
|
||||
2. 事务:Service 方法已加事务,无需重复
|
||||
3. 性能:列表查询避免关联对象,使用投影或 DTO
|
||||
4. 代码生成:初期脚手架生成,后期手动调整
|
||||
|
||||
## 测试用例
|
||||
|
||||
`src/test/java/.../integration/`
|
||||
14
openspec/config.yaml
Normal file
14
openspec/config.yaml
Normal file
@@ -0,0 +1,14 @@
|
||||
schema: spec-driven
|
||||
|
||||
context: |
|
||||
- **优先阅读README.md**获取项目结构与开发规范,所有代码风格、命名、注解、依赖、API等规范以README为准
|
||||
- 新增代码优先复用已有组件、工具、依赖库,不引入新依赖
|
||||
- 涉及模块结构、API、实体等变更时同步更新README.md
|
||||
- Git提交: 仅中文; 格式"类型: 简短描述", 类型: feat/fix/refactor/docs/style/test/chore; 多行描述空行后写详细说明
|
||||
- 禁止创建git操作task
|
||||
- 积极使用subagents精心设计并行任务,节省上下文空间,加速任务执行
|
||||
- 优先使用提问工具对用户进行提问
|
||||
|
||||
rules:
|
||||
proposal:
|
||||
- 仔细审查每一个过往spec判断是否存在Modified Capabilities
|
||||
59
pom.xml
59
pom.xml
@@ -11,7 +11,7 @@
|
||||
|
||||
<modules>
|
||||
<module>spring-boot-service-template-common</module>
|
||||
<module>spring-boot-service-template-jpa</module>
|
||||
<module>spring-boot-service-template-database</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
@@ -26,6 +26,9 @@
|
||||
<querydsl.version>7.1</querydsl.version>
|
||||
<mapstruct.version>1.6.3</mapstruct.version>
|
||||
<mapstruct-plus.version>1.5.0</mapstruct-plus.version>
|
||||
<datasource-decorator.version>2.0.0</datasource-decorator.version>
|
||||
|
||||
<hutool.version>5.8.43</hutool.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@@ -33,6 +36,10 @@
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jspecify</groupId>
|
||||
<artifactId>jspecify</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
@@ -44,7 +51,7 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.lanyuanxiaoyao</groupId>
|
||||
<artifactId>spring-boot-service-template-jpa</artifactId>
|
||||
<artifactId>spring-boot-service-template-database</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
@@ -64,6 +71,25 @@
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct</artifactId>
|
||||
<version>${mapstruct.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.gavlyukovskiy</groupId>
|
||||
<artifactId>p6spy-spring-boot-starter</artifactId>
|
||||
<version>${datasource-decorator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.gavlyukovskiy</groupId>
|
||||
<artifactId>datasource-proxy-spring-boot-starter</artifactId>
|
||||
<version>${datasource-decorator.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- jpa -->
|
||||
<dependency>
|
||||
<groupId>com.blinkfox</groupId>
|
||||
<artifactId>fenix-spring-boot-starter</artifactId>
|
||||
@@ -74,25 +100,38 @@
|
||||
<artifactId>querydsl-jpa</artifactId>
|
||||
<version>${querydsl.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate.orm</groupId>
|
||||
<artifactId>hibernate-ant</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct</artifactId>
|
||||
<version>${mapstruct.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jspecify</groupId>
|
||||
<artifactId>jspecify</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>${hutool.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-core</artifactId>
|
||||
<version>${hutool.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-http</artifactId>
|
||||
<version>${hutool.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-json</artifactId>
|
||||
<version>${hutool.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
||||
@@ -11,6 +11,14 @@
|
||||
|
||||
<artifactId>spring-boot-service-template-common</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
||||
@@ -0,0 +1,263 @@
|
||||
package com.lanyuanxiaoyao.service.template.common.helper;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DisplayName("ObjectHelper Tests")
|
||||
class ObjectHelperTest {
|
||||
|
||||
@Nested
|
||||
@DisplayName("isNull/isNotNull")
|
||||
class NullTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("isNull with null object returns true")
|
||||
void isNull_withNull_returnsTrue() {
|
||||
assertThat(ObjectHelper.isNull(null)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("isNull with non-null object returns false")
|
||||
void isNull_withNonNull_returnsFalse() {
|
||||
assertThat(ObjectHelper.isNull(new Object())).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("isNotNull with null object returns false")
|
||||
void isNotNull_withNull_returnsFalse() {
|
||||
assertThat(ObjectHelper.isNotNull(null)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("isNotNull with non-null object returns true")
|
||||
void isNotNull_withNonNull_returnsTrue() {
|
||||
assertThat(ObjectHelper.isNotNull(new Object())).isTrue();
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("isEmpty/isNotEmpty")
|
||||
class EmptyTests {
|
||||
|
||||
static Stream<Arguments> emptyObjects() {
|
||||
return Stream.of(
|
||||
Arguments.of("null", null, true),
|
||||
Arguments.of("empty collection", Collections.emptyList(), true),
|
||||
Arguments.of("non-empty collection", List.of("a"), false),
|
||||
Arguments.of("empty map", Collections.emptyMap(), true),
|
||||
Arguments.of("non-empty map", Map.of("key", "value"), false),
|
||||
Arguments.of("empty string", "", true),
|
||||
Arguments.of("non-empty string", "text", false),
|
||||
Arguments.of("empty char sequence", new StringBuilder(), true),
|
||||
Arguments.of("non-empty char sequence", new StringBuilder("text"), false),
|
||||
Arguments.of("empty object array", new Object[]{}, true),
|
||||
Arguments.of("non-empty object array", new Object[]{"a"}, false),
|
||||
Arguments.of("empty byte array", new byte[]{}, true),
|
||||
Arguments.of("non-empty byte array", new byte[]{1}, false),
|
||||
Arguments.of("empty short array", new short[]{}, true),
|
||||
Arguments.of("non-empty short array", new short[]{1}, false),
|
||||
Arguments.of("empty int array", new int[]{}, true),
|
||||
Arguments.of("non-empty int array", new int[]{1}, false),
|
||||
Arguments.of("empty long array", new long[]{}, true),
|
||||
Arguments.of("non-empty long array", new long[]{1L}, false),
|
||||
Arguments.of("empty float array", new float[]{}, true),
|
||||
Arguments.of("non-empty float array", new float[]{1.0f}, false),
|
||||
Arguments.of("empty double array", new double[]{}, true),
|
||||
Arguments.of("non-empty double array", new double[]{1.0}, false),
|
||||
Arguments.of("empty char array", new char[]{}, true),
|
||||
Arguments.of("non-empty char array", new char[]{'a'}, false),
|
||||
Arguments.of("empty boolean array", new boolean[]{}, true),
|
||||
Arguments.of("non-empty boolean array", new boolean[]{true}, false),
|
||||
Arguments.of("empty optional", Optional.empty(), true),
|
||||
Arguments.of("non-empty optional", Optional.of("value"), false),
|
||||
Arguments.of("non-empty object", new Object(), false)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("emptyObjects")
|
||||
@DisplayName("isEmpty returns correct result")
|
||||
void isEmpty_withVariousObjects_returnsCorrectResult(String description, Object obj, boolean expected) {
|
||||
assertThat(ObjectHelper.isEmpty(obj)).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("emptyObjects")
|
||||
@DisplayName("isNotEmpty returns opposite of isEmpty")
|
||||
void isNotEmpty_withVariousObjects_returnsCorrectResult(String description, Object obj, boolean expected) {
|
||||
assertThat(ObjectHelper.isNotEmpty(obj)).isEqualTo(!expected);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("defaultIfNull")
|
||||
class DefaultIfNullTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("defaultIfNull with null returns default value")
|
||||
void defaultIfNull_withNull_returnsDefault() {
|
||||
assertThat(ObjectHelper.defaultIfNull(null, "default")).isEqualTo("default");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("defaultIfNull with non-null returns original value")
|
||||
void defaultIfNull_withNonNull_returnsOriginal() {
|
||||
assertThat(ObjectHelper.defaultIfNull("value", "default")).isEqualTo("value");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("defaultIfNull with null and null default returns null")
|
||||
void defaultIfNull_withBothNull_returnsNull() {
|
||||
String result = ObjectHelper.defaultIfNull(null, null);
|
||||
assertThat(result).isNull();
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("isComparable")
|
||||
class ComparableTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("isComparable with enum class returns true")
|
||||
void isComparable_withEnum_returnsTrue() {
|
||||
assertThat(ObjectHelper.isComparable(TestEnum.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("isComparable with String class returns true")
|
||||
void isComparable_withString_returnsTrue() {
|
||||
assertThat(ObjectHelper.isComparable(String.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("isComparable with Integer class returns true")
|
||||
void isComparable_withInteger_returnsTrue() {
|
||||
assertThat(ObjectHelper.isComparable(Integer.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("isComparable with primitive class returns true")
|
||||
void isComparable_withPrimitive_returnsTrue() {
|
||||
assertThat(ObjectHelper.isComparable(int.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("isComparable with non-comparable class returns false")
|
||||
void isComparable_withNonComparable_returnsFalse() {
|
||||
assertThat(ObjectHelper.isComparable(Object.class)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("isComparable with null class returns false")
|
||||
void isComparable_withNull_returnsFalse() {
|
||||
Class<?> nullClass = null;
|
||||
assertThat(ObjectHelper.isComparable(nullClass)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("isComparable with enum object returns true")
|
||||
void isComparable_withEnumObject_returnsTrue() {
|
||||
assertThat(ObjectHelper.isComparable(TestEnum.VALUE)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("isComparable with null object returns false")
|
||||
void isComparable_withNullObject_returnsFalse() {
|
||||
Object nullObj = null;
|
||||
assertThat(ObjectHelper.isComparable(nullObj)).isFalse();
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("isCollection")
|
||||
class CollectionTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("isCollection with List class returns true")
|
||||
void isCollection_withList_returnsTrue() {
|
||||
assertThat(ObjectHelper.isCollection(List.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("isCollection with Set class returns true")
|
||||
void isCollection_withSet_returnsTrue() {
|
||||
assertThat(ObjectHelper.isCollection(Set.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("isCollection with non-collection class returns false")
|
||||
void isCollection_withNonCollection_returnsFalse() {
|
||||
assertThat(ObjectHelper.isCollection(String.class)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("isCollection with null class returns false")
|
||||
void isCollection_withNull_returnsFalse() {
|
||||
Class<?> nullClass = null;
|
||||
assertThat(ObjectHelper.isCollection(nullClass)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("isCollection with ArrayList object returns true")
|
||||
void isCollection_withArrayListObject_returnsTrue() {
|
||||
assertThat(ObjectHelper.isCollection(new ArrayList<>())).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("isCollection with null object returns false")
|
||||
void isCollection_withNullObject_returnsFalse() {
|
||||
Object nullObj = null;
|
||||
assertThat(ObjectHelper.isCollection(nullObj)).isFalse();
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("isString")
|
||||
class StringTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("isString with String class returns true")
|
||||
void isString_withStringClass_returnsTrue() {
|
||||
assertThat(ObjectHelper.isString(String.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("isString with non-string class returns false")
|
||||
void isString_withNonStringClass_returnsFalse() {
|
||||
assertThat(ObjectHelper.isString(Integer.class)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("isString with null class returns false")
|
||||
void isString_withNullClass_returnsFalse() {
|
||||
Class<?> nullClass = null;
|
||||
assertThat(ObjectHelper.isString(nullClass)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("isString with string object returns true")
|
||||
void isString_withStringObject_returnsTrue() {
|
||||
assertThat(ObjectHelper.isString("text")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("isString with null object returns false")
|
||||
void isString_withNullObject_returnsFalse() {
|
||||
Object nullObj = null;
|
||||
assertThat(ObjectHelper.isString(nullObj)).isFalse();
|
||||
}
|
||||
}
|
||||
|
||||
private enum TestEnum {
|
||||
VALUE
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@
|
||||
<version>1.1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>spring-boot-service-template-jpa</artifactId>
|
||||
<artifactId>spring-boot-service-template-database</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
@@ -22,6 +22,7 @@
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
@@ -34,28 +35,39 @@
|
||||
<groupId>io.github.openfeign.querydsl</groupId>
|
||||
<artifactId>querydsl-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate.orm</groupId>
|
||||
<artifactId>hibernate-ant</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jspecify</groupId>
|
||||
<artifactId>jspecify</artifactId>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-data-jpa-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-webmvc-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.gavlyukovskiy</groupId>
|
||||
<artifactId>p6spy-spring-boot-starter</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -97,6 +109,11 @@
|
||||
<artifactId>jakarta.persistence-api</artifactId>
|
||||
<version>3.2.0</version>
|
||||
</path>
|
||||
<path>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct-processor</artifactId>
|
||||
<version>${mapstruct.version}</version>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
<compilerArgs>
|
||||
<arg>-Aquerydsl.entityAccessors=true</arg>
|
||||
@@ -104,6 +121,10 @@
|
||||
</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.configuration;
|
||||
|
||||
import com.querydsl.jpa.impl.JPAQueryFactory;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class QueryDSLConfiguration {
|
||||
@Bean
|
||||
public JPAQueryFactory jpaQueryFactory(EntityManager manager) {
|
||||
return new JPAQueryFactory(manager);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,7 @@
|
||||
package com.lanyuanxiaoyao.service.template.common.controller;
|
||||
package com.lanyuanxiaoyao.service.template.database.controller;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.database.entity.GlobalResponse;
|
||||
import com.lanyuanxiaoyao.service.template.database.entity.Query;
|
||||
|
||||
/**
|
||||
* 查询控制器接口,用于定义统一的查询实体详情和列表的接口规范
|
||||
@@ -1,4 +1,6 @@
|
||||
package com.lanyuanxiaoyao.service.template.common.controller;
|
||||
package com.lanyuanxiaoyao.service.template.database.controller;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.database.entity.GlobalResponse;
|
||||
|
||||
/**
|
||||
* 删除控制器接口,用于定义统一的删除实体对象的接口规范
|
||||
@@ -1,4 +1,6 @@
|
||||
package com.lanyuanxiaoyao.service.template.common.controller;
|
||||
package com.lanyuanxiaoyao.service.template.database.controller;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.database.entity.GlobalResponse;
|
||||
|
||||
/**
|
||||
* 保存控制器接口,用于定义统一的保存实体对象的接口规范
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.lanyuanxiaoyao.service.template.common.controller;
|
||||
package com.lanyuanxiaoyao.service.template.database.controller;
|
||||
|
||||
public interface SimpleController<SAVE_ITEM, LIST_ITEM, DETAIL_ITEM> extends SaveController<SAVE_ITEM>, QueryController<LIST_ITEM, DETAIL_ITEM>, RemoveController {
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa.controller;
|
||||
package com.lanyuanxiaoyao.service.template.database.controller;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.common.controller.GlobalResponse;
|
||||
import com.lanyuanxiaoyao.service.template.common.controller.Query;
|
||||
import com.lanyuanxiaoyao.service.template.common.controller.SimpleController;
|
||||
import com.lanyuanxiaoyao.service.template.common.helper.ObjectHelper;
|
||||
import com.lanyuanxiaoyao.service.template.jpa.entity.SimpleEntity;
|
||||
import com.lanyuanxiaoyao.service.template.jpa.service.SimpleServiceSupport;
|
||||
import com.lanyuanxiaoyao.service.template.database.entity.GlobalResponse;
|
||||
import com.lanyuanxiaoyao.service.template.database.entity.Query;
|
||||
import com.lanyuanxiaoyao.service.template.database.entity.SimpleEntity;
|
||||
import com.lanyuanxiaoyao.service.template.database.service.SimpleServiceSupport;
|
||||
import java.util.function.Function;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -127,6 +126,7 @@ public abstract class SimpleControllerSupport<ENTITY extends SimpleEntity, SAVE_
|
||||
var result = service.list(query);
|
||||
return GlobalResponse.responseListData(
|
||||
result.items()
|
||||
.stream()
|
||||
.map(entity -> {
|
||||
try {
|
||||
return mapper.apply(entity);
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.lanyuanxiaoyao.service.template.common.controller;
|
||||
package com.lanyuanxiaoyao.service.template.database.entity;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa.entity;
|
||||
package com.lanyuanxiaoyao.service.template.database.entity;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.EntityListeners;
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.lanyuanxiaoyao.service.template.common.service;
|
||||
package com.lanyuanxiaoyao.service.template.database.entity;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 分页数据封装类
|
||||
@@ -27,5 +27,5 @@ import java.util.stream.Stream;
|
||||
* @param items 数据流,包含当前页的所有记录
|
||||
* @param total 总记录数,用于计算总页数和显示分页信息
|
||||
*/
|
||||
public record Page<ENTITY>(Stream<ENTITY> items, long total) {
|
||||
public record Page<ENTITY>(List<ENTITY> items, long total) {
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.lanyuanxiaoyao.service.template.common.controller;
|
||||
package com.lanyuanxiaoyao.service.template.database.entity;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -116,8 +117,8 @@ public record Query(
|
||||
List<String> notNullEqual,
|
||||
List<String> empty,
|
||||
List<String> notEmpty,
|
||||
Map<String, Object> equal,
|
||||
Map<String, Object> notEqual,
|
||||
Map<String, ? extends Serializable> equal,
|
||||
Map<String, ? extends Serializable> notEqual,
|
||||
Map<String, String> like,
|
||||
Map<String, String> notLike,
|
||||
Map<String, String> contain,
|
||||
@@ -126,12 +127,12 @@ public record Query(
|
||||
Map<String, String> notStartWith,
|
||||
Map<String, String> endWith,
|
||||
Map<String, String> notEndWith,
|
||||
Map<String, Object> great,
|
||||
Map<String, Object> less,
|
||||
Map<String, Object> greatEqual,
|
||||
Map<String, Object> lessEqual,
|
||||
Map<String, List<Object>> inside,
|
||||
Map<String, List<Object>> notInside,
|
||||
Map<String, ? extends Serializable> great,
|
||||
Map<String, ? extends Serializable> less,
|
||||
Map<String, ? extends Serializable> greatEqual,
|
||||
Map<String, ? extends Serializable> lessEqual,
|
||||
Map<String, List<? extends Serializable>> inside,
|
||||
Map<String, List<? extends Serializable>> notInside,
|
||||
Map<String, Between> between,
|
||||
Map<String, Between> notBetween
|
||||
) {
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa.entity;
|
||||
package com.lanyuanxiaoyao.service.template.database.entity;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.EntityListeners;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa.entity;
|
||||
package com.lanyuanxiaoyao.service.template.database.entity;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.entity;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.database.helper.SnowflakeHelper;
|
||||
import java.io.Serializable;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.id.IdentifierGenerator;
|
||||
|
||||
@Slf4j
|
||||
public class SnowflakeIdGenerator implements IdentifierGenerator {
|
||||
@Override
|
||||
public Serializable generate(SharedSessionContractImplementor session, Object object) {
|
||||
try {
|
||||
return SnowflakeHelper.next();
|
||||
} catch (Exception e) {
|
||||
log.error("Generate snowflake id failed", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.exception;
|
||||
|
||||
public class IdNotFoundException extends RuntimeException {
|
||||
public IdNotFoundException(Long id) {
|
||||
super("ID为 %d 的资源不存在".formatted(id));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.exception;
|
||||
|
||||
public class NotCollectionException extends RuntimeException {
|
||||
public NotCollectionException(String variable) {
|
||||
super("变量 %s 不是集合".formatted(variable));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.exception;
|
||||
|
||||
public class NotComparableException extends RuntimeException {
|
||||
public NotComparableException(String variable) {
|
||||
super("变量 %s 不能比较".formatted(variable));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.exception;
|
||||
|
||||
public class NotStringException extends RuntimeException {
|
||||
public NotStringException(String variable) {
|
||||
super("变量 %s 不是字符串".formatted(variable));
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa.helper;
|
||||
package com.lanyuanxiaoyao.service.template.database.helper;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import java.io.IOException;
|
||||
@@ -115,7 +115,7 @@ public class DatabaseHelper {
|
||||
package %s.repository;
|
||||
|
||||
import %s;
|
||||
import com.lanyuanxiaoyao.service.template.repository.SimpleRepository;
|
||||
import com.lanyuanxiaoyao.service.template.database.repository.SimpleRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
@@ -134,7 +134,7 @@ public class DatabaseHelper {
|
||||
|
||||
import %s;
|
||||
import %s.repository.%sRepository;
|
||||
import com.lanyuanxiaoyao.service.template.service.SimpleServiceSupport;
|
||||
import com.lanyuanxiaoyao.service.template.database.service.SimpleServiceSupport;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -158,7 +158,7 @@ public class DatabaseHelper {
|
||||
|
||||
import %s;
|
||||
import %s.service.%sService;
|
||||
import com.lanyuanxiaoyao.service.template.controller.SimpleControllerSupport;
|
||||
import com.lanyuanxiaoyao.service.template.database.controller.SimpleControllerSupport;
|
||||
import java.util.function.Function;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@@ -197,7 +197,7 @@ public class DatabaseHelper {
|
||||
}
|
||||
}
|
||||
|
||||
""".formatted(projectRootPackage, className, projectRootPackage, name, camelConvert(name), name, name, name, name, name, name, name, name, name, name));
|
||||
""".formatted(projectRootPackage, className, projectRootPackage, name, camelConvert(name), name, name, name, name, name, name, name, name, name, name, name));
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException("Failed to load entity class: " + className, e);
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.helper;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
public class SnowflakeHelper {
|
||||
/**
|
||||
* 起始的时间戳
|
||||
*/
|
||||
private final static long START_TIMESTAMP = 1;
|
||||
|
||||
/**
|
||||
* 序列号占用的位数
|
||||
*/
|
||||
private final static long SEQUENCE_BIT = 11;
|
||||
|
||||
/**
|
||||
* 序列号最大值
|
||||
*/
|
||||
private final static long MAX_SEQUENCE_BIT = ~(-1 << SEQUENCE_BIT);
|
||||
|
||||
/**
|
||||
* 时间戳值向左位移
|
||||
*/
|
||||
private final static long TIMESTAMP_OFFSET = SEQUENCE_BIT;
|
||||
|
||||
/**
|
||||
* 序列号
|
||||
*/
|
||||
private static long sequence = 0;
|
||||
/**
|
||||
* 上一次时间戳
|
||||
*/
|
||||
private static long lastTimestamp = -1;
|
||||
|
||||
public static synchronized long next() {
|
||||
long currentTimestamp = nowTimestamp();
|
||||
if (currentTimestamp < lastTimestamp) {
|
||||
throw new RuntimeException("Clock have moved backwards.");
|
||||
}
|
||||
|
||||
if (currentTimestamp == lastTimestamp) {
|
||||
// 相同毫秒内, 序列号自增
|
||||
sequence = (sequence + 1) & MAX_SEQUENCE_BIT;
|
||||
// 同一毫秒的序列数已经达到最大
|
||||
if (sequence == 0) {
|
||||
currentTimestamp = nextTimestamp();
|
||||
}
|
||||
} else {
|
||||
// 不同毫秒内, 序列号置为0
|
||||
sequence = 0;
|
||||
}
|
||||
|
||||
lastTimestamp = currentTimestamp;
|
||||
return (currentTimestamp - START_TIMESTAMP) << TIMESTAMP_OFFSET | sequence;
|
||||
}
|
||||
|
||||
private static long nextTimestamp() {
|
||||
long milli = nowTimestamp();
|
||||
while (milli <= lastTimestamp) {
|
||||
milli = nowTimestamp();
|
||||
}
|
||||
return milli;
|
||||
}
|
||||
|
||||
private static long nowTimestamp() {
|
||||
return Instant.now().toEpochMilli();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa.repository;
|
||||
package com.lanyuanxiaoyao.service.template.database.repository;
|
||||
|
||||
import com.blinkfox.fenix.jpa.FenixJpaRepository;
|
||||
import com.blinkfox.fenix.specification.FenixJpaSpecificationExecutor;
|
||||
@@ -0,0 +1,128 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.service;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.common.helper.ObjectHelper;
|
||||
import com.lanyuanxiaoyao.service.template.database.entity.Query;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
|
||||
public abstract class QueryParser<O> {
|
||||
private final Query.Queryable queryable;
|
||||
private final O container;
|
||||
|
||||
protected abstract void nullEqual(Query.Queryable queryable, O container);
|
||||
|
||||
protected abstract void notNullEqual(Query.Queryable queryable, O container);
|
||||
|
||||
protected abstract void empty(Query.Queryable queryable, O container);
|
||||
|
||||
protected abstract void notEmpty(Query.Queryable queryable, O container);
|
||||
|
||||
protected abstract void equal(Query.Queryable queryable, O container);
|
||||
|
||||
protected abstract void notEqual(Query.Queryable queryable, O container);
|
||||
|
||||
protected abstract void like(Query.Queryable queryable, O container);
|
||||
|
||||
protected abstract void notLike(Query.Queryable queryable, O container);
|
||||
|
||||
protected abstract void contain(Query.Queryable queryable, O container);
|
||||
|
||||
protected abstract void notContain(Query.Queryable queryable, O container);
|
||||
|
||||
protected abstract void startWith(Query.Queryable queryable, O container);
|
||||
|
||||
protected abstract void notStartWith(Query.Queryable queryable, O container);
|
||||
|
||||
protected abstract void endWith(Query.Queryable queryable, O container);
|
||||
|
||||
protected abstract void notEndWith(Query.Queryable queryable, O container);
|
||||
|
||||
protected abstract void great(Query.Queryable queryable, O container);
|
||||
|
||||
protected abstract void less(Query.Queryable queryable, O container);
|
||||
|
||||
protected abstract void greatEqual(Query.Queryable queryable, O container);
|
||||
|
||||
protected abstract void lessEqual(Query.Queryable queryable, O container);
|
||||
|
||||
protected abstract void inside(Query.Queryable queryable, O container);
|
||||
|
||||
protected abstract void notInside(Query.Queryable queryable, O container);
|
||||
|
||||
protected abstract void between(Query.Queryable queryable, O container);
|
||||
|
||||
protected abstract void notBetween(Query.Queryable queryable, O container);
|
||||
|
||||
public void build() {
|
||||
if (ObjectHelper.isNull(queryable)) {
|
||||
return;
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.nullEqual())) {
|
||||
nullEqual(queryable, container);
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.notNullEqual())) {
|
||||
notNullEqual(queryable, container);
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.empty())) {
|
||||
empty(queryable, container);
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.notEmpty())) {
|
||||
notEmpty(queryable, container);
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.equal())) {
|
||||
equal(queryable, container);
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.notEqual())) {
|
||||
notEqual(queryable, container);
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.like())) {
|
||||
like(queryable, container);
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.notLike())) {
|
||||
notLike(queryable, container);
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.contain())) {
|
||||
contain(queryable, container);
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.notContain())) {
|
||||
notContain(queryable, container);
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.startWith())) {
|
||||
startWith(queryable, container);
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.notStartWith())) {
|
||||
notStartWith(queryable, container);
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.endWith())) {
|
||||
endWith(queryable, container);
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.notEndWith())) {
|
||||
notEndWith(queryable, container);
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.great())) {
|
||||
great(queryable, container);
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.less())) {
|
||||
less(queryable, container);
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.greatEqual())) {
|
||||
greatEqual(queryable, container);
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.lessEqual())) {
|
||||
lessEqual(queryable, container);
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.inside())) {
|
||||
inside(queryable, container);
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.notInside())) {
|
||||
notInside(queryable, container);
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.between())) {
|
||||
between(queryable, container);
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.notBetween())) {
|
||||
notBetween(queryable, container);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.lanyuanxiaoyao.service.template.common.service;
|
||||
package com.lanyuanxiaoyao.service.template.database.service;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.common.controller.Query;
|
||||
import com.lanyuanxiaoyao.service.template.database.entity.Page;
|
||||
import com.lanyuanxiaoyao.service.template.database.entity.Query;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
package com.lanyuanxiaoyao.service.template.common.service;
|
||||
package com.lanyuanxiaoyao.service.template.database.service;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 删除服务接口,用于定义统一的删除实体对象的服务规范
|
||||
@@ -33,5 +35,5 @@ public interface RemoveService<ENTITY> {
|
||||
* @param ids 需要删除的实体ID集合
|
||||
* @throws Exception 删除过程中可能抛出的异常
|
||||
*/
|
||||
void remove(Iterable<Long> ids) throws Exception;
|
||||
void remove(Set<Long> ids) throws Exception;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.lanyuanxiaoyao.service.template.common.service;
|
||||
package com.lanyuanxiaoyao.service.template.database.service;
|
||||
|
||||
/**
|
||||
* 保存服务接口,用于定义统一的保存实体对象的服务规范
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.lanyuanxiaoyao.service.template.common.service;
|
||||
package com.lanyuanxiaoyao.service.template.database.service;
|
||||
|
||||
public interface SimpleService<ENTITY> extends SaveService<ENTITY>, QueryService<ENTITY>, RemoveService<ENTITY> {
|
||||
}
|
||||
@@ -0,0 +1,540 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.service;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.common.helper.ObjectHelper;
|
||||
import com.lanyuanxiaoyao.service.template.database.entity.Page;
|
||||
import com.lanyuanxiaoyao.service.template.database.entity.Query;
|
||||
import com.lanyuanxiaoyao.service.template.database.exception.IdNotFoundException;
|
||||
import com.lanyuanxiaoyao.service.template.database.exception.NotCollectionException;
|
||||
import com.lanyuanxiaoyao.service.template.database.exception.NotComparableException;
|
||||
import com.lanyuanxiaoyao.service.template.database.exception.NotStringException;
|
||||
import com.lanyuanxiaoyao.service.template.database.entity.IdOnlyEntity;
|
||||
import com.lanyuanxiaoyao.service.template.database.entity.SimpleEntity;
|
||||
import com.lanyuanxiaoyao.service.template.database.repository.SimpleRepository;
|
||||
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||
import jakarta.persistence.criteria.CriteriaQuery;
|
||||
import jakarta.persistence.criteria.Path;
|
||||
import jakarta.persistence.criteria.Predicate;
|
||||
import jakarta.persistence.criteria.Root;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.mapstruct.Named;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* 简单服务支持类,提供基础的业务逻辑实现
|
||||
* <p>
|
||||
* 该类实现了SimpleService接口,提供实体的增删改查等基本操作。
|
||||
* 通过继承此类,可以快速实现常见的业务逻辑功能,包括:
|
||||
* <ul>
|
||||
* <li>实体的保存和更新(支持部分字段更新)</li>
|
||||
* <li>实体的条件查询和分页查询</li>
|
||||
* <li>实体的详情查询(多种方式)</li>
|
||||
* <li>实体的删除操作(支持批量删除)</li>
|
||||
* <li>动态查询条件构建</li>
|
||||
* </ul>
|
||||
* </p>
|
||||
*
|
||||
* <h3>设计特点</h3>
|
||||
* <ul>
|
||||
* <li>泛型设计,支持任意实体类型</li>
|
||||
* <li>事务管理,确保数据一致性</li>
|
||||
* <li>动态查询条件,支持复杂的业务查询</li>
|
||||
* <li>部分更新,只更新非空字段</li>
|
||||
* <li>可扩展的查询条件构建</li>
|
||||
* </ul>
|
||||
*
|
||||
* <h3>使用说明</h3>
|
||||
* <p>子类可以重写以下方法:</p>
|
||||
* <ul>
|
||||
* <li>commonPredicates(): 添加自定义的查询条件</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param <ENTITY> 实体类型,必须继承SimpleEntity
|
||||
*/
|
||||
@Slf4j
|
||||
public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implements SimpleService<ENTITY> {
|
||||
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
private static final int DEFAULT_PAGE_INDEX = 1;
|
||||
private static final int DEFAULT_PAGE_SIZE = 10;
|
||||
protected final SimpleRepository<ENTITY> repository;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param repository 简单仓库实例
|
||||
*/
|
||||
public SimpleServiceSupport(SimpleRepository<ENTITY> repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Throwable.class)
|
||||
@Override
|
||||
public Long save(ENTITY entity) {
|
||||
entity = repository.saveOrUpdateByNotNullProperties(entity);
|
||||
return entity.getId();
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Throwable.class)
|
||||
@Override
|
||||
public void save(Iterable<ENTITY> entities) {
|
||||
repository.saveOrUpdateAllByNotNullProperties(entities);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
@Override
|
||||
public Long count() {
|
||||
return repository.count(this::commonPredicates);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
@Override
|
||||
public List<ENTITY> list() {
|
||||
return repository.findAll(this::commonPredicates);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
@Override
|
||||
public List<ENTITY> list(Set<Long> ids) {
|
||||
if (ObjectHelper.isEmpty(ids)) {
|
||||
return List.of();
|
||||
}
|
||||
return repository.findAll(
|
||||
(root, query, builder) -> {
|
||||
var predicate = commonPredicates(root, query, builder);
|
||||
var idsPredicate = builder.in(root.get(IdOnlyEntity.Fields.id)).value(ids);
|
||||
return ObjectHelper.isNull(predicate)
|
||||
? idsPredicate
|
||||
: builder.and(predicate, idsPredicate);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
protected Predicate commonPredicates(Root<ENTITY> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
@Override
|
||||
public Page<ENTITY> list(Query listQuery) {
|
||||
var pageRequest = PageRequest.of(DEFAULT_PAGE_INDEX - 1, DEFAULT_PAGE_SIZE, Sort.by(SimpleEntity.Fields.createdTime).descending());
|
||||
if (ObjectHelper.isNotNull(listQuery.page())) {
|
||||
var index = Math.max(ObjectHelper.defaultIfNull(listQuery.page().index(), DEFAULT_PAGE_INDEX) - 1, 0);
|
||||
var size = Math.max(ObjectHelper.defaultIfNull(listQuery.page().size(), DEFAULT_PAGE_SIZE), 1);
|
||||
if (ObjectHelper.isNotEmpty(listQuery.sort())) {
|
||||
pageRequest = PageRequest.of(index, size, Sort.by(
|
||||
listQuery.sort()
|
||||
.stream()
|
||||
.map(sort -> new Sort.Order(Sort.Direction.fromString(sort.direction().name()), sort.column()))
|
||||
.toList()
|
||||
));
|
||||
} else {
|
||||
pageRequest = PageRequest.of(index, size, Sort.by(SimpleEntity.Fields.createdTime).descending());
|
||||
}
|
||||
}
|
||||
var result = repository.findAll(
|
||||
(root, query, builder) -> {
|
||||
var predicate = commonPredicates(root, query, builder);
|
||||
var predicates = new ArrayList<Predicate>();
|
||||
new JpaQueryParser<>(listQuery.query(), predicates, root, query, builder).build();
|
||||
var queryPredicate = predicates.size() == 1
|
||||
? predicates.get(0)
|
||||
: builder.and(predicates.toArray(Predicate[]::new));
|
||||
return ObjectHelper.isNull(predicate)
|
||||
? queryPredicate
|
||||
: builder.and(predicate, queryPredicate);
|
||||
},
|
||||
pageRequest
|
||||
);
|
||||
return new Page<>(result.get().toList(), result.getTotalElements());
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取实体详情(Optional包装)
|
||||
* <p>
|
||||
* 如果ID为空则返回空Optional,否则根据ID查询实体。
|
||||
* </p>
|
||||
*
|
||||
* @param id 实体ID
|
||||
* @return 返回实体详情的Optional包装
|
||||
*/
|
||||
private Optional<ENTITY> detailOptional(Long id) {
|
||||
if (ObjectHelper.isNull(id)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return repository.findOne(
|
||||
(root, query, builder) -> {
|
||||
var predicate = commonPredicates(root, query, builder);
|
||||
var idPredicate = builder.equal(root.get(IdOnlyEntity.Fields.id), id);
|
||||
return ObjectHelper.isNull(predicate)
|
||||
? idPredicate
|
||||
: builder.and(predicate, idPredicate);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Named("detail")
|
||||
@Transactional(readOnly = true)
|
||||
@Override
|
||||
public ENTITY detail(Long id) {
|
||||
return detailOptional(id).orElse(null);
|
||||
}
|
||||
|
||||
@Named("detailOrThrow")
|
||||
@Transactional(readOnly = true)
|
||||
@Override
|
||||
public ENTITY detailOrThrow(Long id) {
|
||||
return detailOptional(id).orElseThrow(() -> new IdNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Throwable.class)
|
||||
@Override
|
||||
public void remove(Long id) {
|
||||
if (ObjectHelper.isNotNull(id)) {
|
||||
repository.deleteBatchByIds(List.of(id));
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Throwable.class)
|
||||
@Override
|
||||
public void remove(Set<Long> ids) {
|
||||
if (ObjectHelper.isNotEmpty(ids)) {
|
||||
repository.deleteBatchByIds(ids);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static final class JpaQueryParser<ENTITY> extends QueryParser<List<Predicate>> {
|
||||
private final Root<ENTITY> root;
|
||||
@SuppressWarnings({"unused", "FieldCanBeLocal"})
|
||||
private final CriteriaQuery<?> query;
|
||||
private final CriteriaBuilder builder;
|
||||
|
||||
private JpaQueryParser(Query.Queryable queryable, List<Predicate> predicates, Root<ENTITY> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
|
||||
super(queryable, predicates);
|
||||
this.root = root;
|
||||
this.query = query;
|
||||
this.builder = builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析字段路径
|
||||
* <p>
|
||||
* 支持多级字段路径解析,使用"."分隔多级字段。
|
||||
* 例如: "user.name" 表示实体的user属性的name字段。
|
||||
* </p>
|
||||
*
|
||||
* @param root JPA Criteria查询根节点
|
||||
* @param column 字段路径字符串
|
||||
* @param <Y> 字段类型
|
||||
* @return 返回字段路径对象
|
||||
* @throws IllegalArgumentException 当字段路径为空时抛出
|
||||
*/
|
||||
private <Y> Path<Y> column(Root<ENTITY> root, String column) {
|
||||
if (ObjectHelper.isEmpty(column)) {
|
||||
throw new IllegalArgumentException("Column cannot be blank");
|
||||
}
|
||||
var columns = column.split("\\.");
|
||||
Path<Y> path = root.get(columns[0]);
|
||||
for (int i = 1; i < columns.length; i++) {
|
||||
path = path.get(columns[i]);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理字段值
|
||||
* <p>
|
||||
* 对于枚举类型字段,将字符串值转换为对应的枚举值。
|
||||
* 对于LocalDateTime类型字段,将字符串转换为时间对象。
|
||||
* 其他类型直接返回原值。
|
||||
* </p>
|
||||
*
|
||||
* @param column 字段路径
|
||||
* @param value 字段值
|
||||
* @param <Y> 字段类型
|
||||
* @return 处理后的字段值
|
||||
* @throws IllegalArgumentException 当枚举类型字段的值不是字符串时抛出
|
||||
*/
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
private <Y> Object value(Path<Y> column, Object value) {
|
||||
if (ObjectHelper.isNull(value)) {
|
||||
return null;
|
||||
}
|
||||
var javaType = column.getJavaType();
|
||||
if (javaType.isEnum()) {
|
||||
if (value instanceof String enumName) {
|
||||
var enumType = (Class<Enum>) javaType;
|
||||
return Enum.valueOf(enumType, enumName);
|
||||
} else {
|
||||
throw new IllegalArgumentException("枚举类型字段需要 String 类型的值");
|
||||
}
|
||||
} else if (javaType.isAssignableFrom(LocalDateTime.class)) {
|
||||
return LocalDateTime.parse(String.valueOf(value), DATE_TIME_FORMATTER);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查字段类型是否可比较
|
||||
*
|
||||
* @param path 字段路径
|
||||
* @param value 比较值
|
||||
* @param column 字段名称
|
||||
* @throws NotComparableException 当字段类型不可比较时抛出
|
||||
*/
|
||||
private void checkComparable(Path<?> path, Object value, String column) {
|
||||
if (!ObjectHelper.isComparable(path.getJavaType()) || !ObjectHelper.isComparable(value)) {
|
||||
throw new NotComparableException(column);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查区间值是否可比较
|
||||
*
|
||||
* @param path 字段路径
|
||||
* @param value 区间对象
|
||||
* @param column 字段名称
|
||||
* @throws NotComparableException 当区间值不可比较时抛出
|
||||
*/
|
||||
private void checkComparable(Path<?> path, Query.Queryable.Between value, String column) {
|
||||
checkComparable(path, value.start(), column);
|
||||
checkComparable(path, value.end(), column);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查字段类型是否为集合
|
||||
*
|
||||
* @param path 字段路径
|
||||
* @param column 字段名称
|
||||
* @throws NotCollectionException 当字段类型不是集合时抛出
|
||||
*/
|
||||
private void checkCollection(Path<?> path, String column) {
|
||||
if (!ObjectHelper.isCollection(path.getJavaType())) {
|
||||
throw new NotCollectionException(column);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查值是否为集合
|
||||
*
|
||||
* @param value 值对象
|
||||
* @param column 字段名称
|
||||
* @throws NotCollectionException 当值不是集合时抛出
|
||||
*/
|
||||
private void checkCollection(Object value, String column) {
|
||||
if (!ObjectHelper.isCollection(value)) {
|
||||
throw new NotCollectionException(column);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查字段类型是否为字符串
|
||||
*
|
||||
* @param path 字段路径
|
||||
* @param value 比较值
|
||||
* @param column 字段名称
|
||||
* @throws NotStringException 当字段类型不是字符串时抛出
|
||||
*/
|
||||
private void checkString(Path<?> path, Object value, String column) {
|
||||
if (!ObjectHelper.isString(path.getJavaType()) || !ObjectHelper.isString(value)) {
|
||||
throw new NotStringException(column);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void nullEqual(Query.Queryable queryable, List<Predicate> predicates) {
|
||||
queryable.nullEqual().forEach(column -> predicates.add(builder.isNull(column(root, column))));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void notNullEqual(Query.Queryable queryable, List<Predicate> predicates) {
|
||||
queryable.notNullEqual().forEach(column -> predicates.add(builder.isNotNull(column(root, column))));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void empty(Query.Queryable queryable, List<Predicate> predicates) {
|
||||
queryable.empty().forEach(column -> {
|
||||
var path = this.<Collection<Object>>column(root, column);
|
||||
checkCollection(path, column);
|
||||
predicates.add(builder.isEmpty(path));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void notEmpty(Query.Queryable queryable, List<Predicate> predicates) {
|
||||
queryable.notEmpty().forEach(column -> {
|
||||
var path = this.<Collection<Object>>column(root, column);
|
||||
checkCollection(path, column);
|
||||
predicates.add(builder.isNotEmpty(path));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void equal(Query.Queryable queryable, List<Predicate> predicates) {
|
||||
queryable.equal().forEach((column, value) -> {
|
||||
var path = column(root, column);
|
||||
predicates.add(builder.equal(path, value(path, value)));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void notEqual(Query.Queryable queryable, List<Predicate> predicates) {
|
||||
queryable.notEqual().forEach((column, value) -> {
|
||||
var path = column(root, column);
|
||||
predicates.add(builder.notEqual(path, value(path, value)));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void like(Query.Queryable queryable, List<Predicate> predicates) {
|
||||
queryable.like().forEach((column, value) -> {
|
||||
var path = this.<String>column(root, column);
|
||||
checkString(path, value, column);
|
||||
predicates.add(builder.like(path, value));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void notLike(Query.Queryable queryable, List<Predicate> predicates) {
|
||||
queryable.notLike().forEach((column, value) -> {
|
||||
var path = this.<String>column(root, column);
|
||||
checkString(path, value, column);
|
||||
predicates.add(builder.notLike(path, value));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void contain(Query.Queryable queryable, List<Predicate> predicates) {
|
||||
queryable.contain().forEach((column, value) -> {
|
||||
var path = this.<String>column(root, column);
|
||||
checkString(path, value, column);
|
||||
predicates.add(builder.like(path, "%" + value + "%"));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void notContain(Query.Queryable queryable, List<Predicate> predicates) {
|
||||
queryable.notContain().forEach((column, value) -> {
|
||||
var path = this.<String>column(root, column);
|
||||
checkString(path, value, column);
|
||||
predicates.add(builder.notLike(path, "%" + value + "%"));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void startWith(Query.Queryable queryable, List<Predicate> predicates) {
|
||||
queryable.startWith().forEach((column, value) -> {
|
||||
var path = this.<String>column(root, column);
|
||||
checkString(path, value, column);
|
||||
predicates.add(builder.like(path, value + "%"));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void notStartWith(Query.Queryable queryable, List<Predicate> predicates) {
|
||||
queryable.notStartWith().forEach((column, value) -> {
|
||||
var path = this.<String>column(root, column);
|
||||
checkString(path, value, column);
|
||||
predicates.add(builder.notLike(path, value + "%"));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void endWith(Query.Queryable queryable, List<Predicate> predicates) {
|
||||
queryable.endWith().forEach((column, value) -> {
|
||||
var path = this.<String>column(root, column);
|
||||
checkString(path, value, column);
|
||||
predicates.add(builder.like(path, "%" + value));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void notEndWith(Query.Queryable queryable, List<Predicate> predicates) {
|
||||
queryable.notEndWith().forEach((column, value) -> {
|
||||
var path = this.<String>column(root, column);
|
||||
checkString(path, value, column);
|
||||
predicates.add(builder.notLike(path, "%" + value));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void great(Query.Queryable queryable, List<Predicate> predicates) {
|
||||
queryable.great().forEach((column, value) -> {
|
||||
var path = this.<Comparable<Object>>column(root, column);
|
||||
checkComparable(path, value, column);
|
||||
predicates.add(builder.greaterThan(path, (Comparable<Object>) value(path, value)));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void less(Query.Queryable queryable, List<Predicate> predicates) {
|
||||
queryable.less().forEach((column, value) -> {
|
||||
var path = this.<Comparable<Object>>column(root, column);
|
||||
checkComparable(path, value, column);
|
||||
predicates.add(builder.lessThan(path, (Comparable<Object>) value(path, value)));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void greatEqual(Query.Queryable queryable, List<Predicate> predicates) {
|
||||
queryable.greatEqual().forEach((column, value) -> {
|
||||
var path = this.<Comparable<Object>>column(root, column);
|
||||
checkComparable(path, value, column);
|
||||
predicates.add(builder.greaterThanOrEqualTo(path, (Comparable<Object>) value(path, value)));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void lessEqual(Query.Queryable queryable, List<Predicate> predicates) {
|
||||
queryable.lessEqual().forEach((column, value) -> {
|
||||
var path = this.<Comparable<Object>>column(root, column);
|
||||
checkComparable(path, value, column);
|
||||
predicates.add(builder.lessThanOrEqualTo(path, (Comparable<Object>) value(path, value)));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void inside(Query.Queryable queryable, List<Predicate> predicates) {
|
||||
queryable.inside()
|
||||
.entrySet()
|
||||
.stream()
|
||||
.filter(entry -> ObjectHelper.isNotEmpty(entry.getValue()))
|
||||
.forEach(entry -> predicates.add(builder.in(column(root, entry.getKey())).value(entry.getValue())));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void notInside(Query.Queryable queryable, List<Predicate> predicates) {
|
||||
queryable.notInside()
|
||||
.entrySet()
|
||||
.stream()
|
||||
.filter(entry -> ObjectHelper.isNotEmpty(entry.getValue()))
|
||||
.forEach(entry -> predicates.add(builder.in(column(root, entry.getKey())).value(entry.getValue()).not()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void between(Query.Queryable queryable, List<Predicate> predicates) {
|
||||
queryable.between().forEach((column, value) -> {
|
||||
var path = this.<Comparable<Object>>column(root, column);
|
||||
checkComparable(path, value, column);
|
||||
predicates.add(builder.between(path, (Comparable<Object>) value(path, value.start()), (Comparable<Object>) value(path, value.end())));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void notBetween(Query.Queryable queryable, List<Predicate> predicates) {
|
||||
queryable.notBetween().forEach((column, value) -> {
|
||||
var path = this.<Comparable<Object>>column(root, column);
|
||||
checkComparable(path, value, column);
|
||||
predicates.add(builder.between(path, (Comparable<Object>) value(path, value.start()), (Comparable<Object>) value(path, value.end())).not());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.helper;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.RepeatedTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DisplayName("SnowflakeHelper Tests")
|
||||
class SnowflakeHelperTest {
|
||||
|
||||
@Nested
|
||||
@DisplayName("ID Generation")
|
||||
class IdGenerationTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("next returns positive number")
|
||||
void next_returnsPositiveNumber() {
|
||||
long id = SnowflakeHelper.next();
|
||||
assertThat(id).isPositive();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("next returns monotonically increasing IDs")
|
||||
void next_returnsMonotonicallyIncreasingIds() {
|
||||
long id1 = SnowflakeHelper.next();
|
||||
long id2 = SnowflakeHelper.next();
|
||||
long id3 = SnowflakeHelper.next();
|
||||
|
||||
assertThat(id2).isGreaterThan(id1);
|
||||
assertThat(id3).isGreaterThan(id2);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Uniqueness")
|
||||
class UniquenessTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("batch generation produces unique IDs")
|
||||
void batchGeneration_producesUniqueIds() {
|
||||
int count = 10000;
|
||||
Set<Long> ids = Collections.synchronizedSet(new HashSet<>());
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
ids.add(SnowflakeHelper.next());
|
||||
}
|
||||
|
||||
assertThat(ids).hasSize(count);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("concurrent generation produces unique IDs")
|
||||
void concurrentGeneration_producesUniqueIds() throws InterruptedException {
|
||||
int threadCount = 10;
|
||||
int idsPerThread = 1000;
|
||||
Set<Long> ids = Collections.newSetFromMap(new ConcurrentHashMap<>());
|
||||
CountDownLatch latch = new CountDownLatch(threadCount);
|
||||
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
|
||||
|
||||
for (int i = 0; i < threadCount; i++) {
|
||||
executor.submit(() -> {
|
||||
try {
|
||||
for (int j = 0; j < idsPerThread; j++) {
|
||||
ids.add(SnowflakeHelper.next());
|
||||
}
|
||||
} finally {
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
latch.await();
|
||||
executor.shutdown();
|
||||
|
||||
assertThat(ids).hasSize(threadCount * idsPerThread);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Performance")
|
||||
class PerformanceTests {
|
||||
|
||||
@RepeatedTest(5)
|
||||
@DisplayName("generates 1000 IDs quickly")
|
||||
void generatesIdsQuickly() {
|
||||
long start = System.currentTimeMillis();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
SnowflakeHelper.next();
|
||||
}
|
||||
long elapsed = System.currentTimeMillis() - start;
|
||||
assertThat(elapsed).isLessThan(1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.integration;
|
||||
|
||||
import com.blinkfox.fenix.EnableFenix;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableFenix
|
||||
@EnableJpaAuditing
|
||||
public class IntegrationTestConfiguration {
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.integration;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.database.entity.Query;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class QueryBuilder {
|
||||
private List<String> nullEqual;
|
||||
private List<String> notNullEqual;
|
||||
private List<String> empty;
|
||||
private List<String> notEmpty;
|
||||
private Map<String, ? extends Serializable> equal;
|
||||
private Map<String, ? extends Serializable> notEqual;
|
||||
private Map<String, String> like;
|
||||
private Map<String, String> notLike;
|
||||
private Map<String, String> contain;
|
||||
private Map<String, String> notContain;
|
||||
private Map<String, String> startWith;
|
||||
private Map<String, String> notStartWith;
|
||||
private Map<String, String> endWith;
|
||||
private Map<String, String> notEndWith;
|
||||
private Map<String, ? extends Serializable> great;
|
||||
private Map<String, ? extends Serializable> less;
|
||||
private Map<String, ? extends Serializable> greatEqual;
|
||||
private Map<String, ? extends Serializable> lessEqual;
|
||||
private Map<String, List<? extends Serializable>> inside;
|
||||
private Map<String, List<? extends Serializable>> notInside;
|
||||
private Map<String, Query.Queryable.Between> between;
|
||||
private Map<String, Query.Queryable.Between> notBetween;
|
||||
|
||||
public QueryBuilder nullEqual(List<String> nullEqual) {
|
||||
this.nullEqual = nullEqual;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder notNullEqual(List<String> notNullEqual) {
|
||||
this.notNullEqual = notNullEqual;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder empty(List<String> empty) {
|
||||
this.empty = empty;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder notEmpty(List<String> notEmpty) {
|
||||
this.notEmpty = notEmpty;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder equal(Map<String, ? extends Serializable> equal) {
|
||||
this.equal = equal;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder notEqual(Map<String, ? extends Serializable> notEqual) {
|
||||
this.notEqual = notEqual;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder like(Map<String, String> like) {
|
||||
this.like = like;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder notLike(Map<String, String> notLike) {
|
||||
this.notLike = notLike;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder contain(Map<String, String> contain) {
|
||||
this.contain = contain;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder notContain(Map<String, String> notContain) {
|
||||
this.notContain = notContain;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder startWith(Map<String, String> startWith) {
|
||||
this.startWith = startWith;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder notStartWith(Map<String, String> notStartWith) {
|
||||
this.notStartWith = notStartWith;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder endWith(Map<String, String> endWith) {
|
||||
this.endWith = endWith;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder notEndWith(Map<String, String> notEndWith) {
|
||||
this.notEndWith = notEndWith;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder great(Map<String, ? extends Serializable> great) {
|
||||
this.great = great;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder less(Map<String, ? extends Serializable> less) {
|
||||
this.less = less;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder greatEqual(Map<String, ? extends Serializable> greatEqual) {
|
||||
this.greatEqual = greatEqual;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder lessEqual(Map<String, ? extends Serializable> lessEqual) {
|
||||
this.lessEqual = lessEqual;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder inside(Map<String, List<? extends Serializable>> inside) {
|
||||
this.inside = inside;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder notInside(Map<String, List<? extends Serializable>> notInside) {
|
||||
this.notInside = notInside;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder between(Map<String, Query.Queryable.Between> between) {
|
||||
this.between = between;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder notBetween(Map<String, Query.Queryable.Between> notBetween) {
|
||||
this.notBetween = notBetween;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Query build() {
|
||||
return new Query(new Query.Queryable(
|
||||
nullEqual, notNullEqual, empty, notEmpty, equal, notEqual,
|
||||
like, notLike, contain, notContain, startWith, notStartWith,
|
||||
endWith, notEndWith, great, less, greatEqual, lessEqual,
|
||||
inside, notInside, between, notBetween
|
||||
), null, null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.integration;
|
||||
|
||||
import tools.jackson.databind.ObjectMapper;
|
||||
import com.lanyuanxiaoyao.service.template.database.entity.Query;
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.controller.TestController;
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.entity.TestEntity;
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.entity.TestStatus;
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.repository.TestRepository;
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.service.TestService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.data.jpa.test.autoconfigure.AutoConfigureDataJpa;
|
||||
import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@WebMvcTest(TestController.class)
|
||||
@AutoConfigureDataJpa
|
||||
@ActiveProfiles("test")
|
||||
@Import({IntegrationTestConfiguration.class, TestService.class})
|
||||
@DisplayName("SimpleControllerSupport Integration Tests")
|
||||
class SimpleControllerSupportIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
ObjectMapper objectMapper;
|
||||
|
||||
@Autowired
|
||||
TestRepository repository;
|
||||
|
||||
@BeforeEach
|
||||
void cleanUp() {
|
||||
repository.deleteAll();
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("POST /save")
|
||||
class SaveTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("save with valid data returns id")
|
||||
void save_withValidData_returnsId() throws Exception {
|
||||
var request = new TestController.SaveItem("张三", 25, TestStatus.ACTIVE, 5000.0, "java");
|
||||
|
||||
mockMvc.perform(post("/test/save")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(request)))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(0))
|
||||
.andExpect(jsonPath("$.message").value("OK"))
|
||||
.andExpect(jsonPath("$.data").isNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("saveItemMapper converts correctly")
|
||||
void saveItemMapper_convertsCorrectly() throws Exception {
|
||||
var request = new TestController.SaveItem("张三", 25, TestStatus.ACTIVE, 5000.0, "java,spring");
|
||||
|
||||
String response = mockMvc.perform(post("/test/save")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(request)))
|
||||
.andReturn().getResponse().getContentAsString();
|
||||
|
||||
Long id = objectMapper.readTree(response).get("data").asLong();
|
||||
TestEntity saved = repository.findById(id).orElseThrow();
|
||||
|
||||
assertThat(saved.getName()).isEqualTo("张三");
|
||||
assertThat(saved.getAge()).isEqualTo(25);
|
||||
assertThat(saved.getStatus()).isEqualTo(TestStatus.ACTIVE);
|
||||
assertThat(saved.getSalary()).isEqualTo(5000.0);
|
||||
assertThat(saved.getTags()).isEqualTo("java,spring");
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("GET /list")
|
||||
class ListTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("list returns all entities")
|
||||
void list_returnsAllEntities() throws Exception {
|
||||
repository.save(new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java"));
|
||||
repository.save(new TestEntity("李四", 30, TestStatus.INACTIVE, 6000.0, "python"));
|
||||
|
||||
mockMvc.perform(get("/test/list"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(0))
|
||||
.andExpect(jsonPath("$.message").value("OK"))
|
||||
.andExpect(jsonPath("$.data.items").isArray())
|
||||
.andExpect(jsonPath("$.data.items.length()").value(2))
|
||||
.andExpect(jsonPath("$.data.total").value(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("listItemMapper converts correctly")
|
||||
void listItemMapper_convertsCorrectly() throws Exception {
|
||||
var entity = repository.save(new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java"));
|
||||
|
||||
mockMvc.perform(get("/test/list"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data.items[0].id").value(entity.getId()))
|
||||
.andExpect(jsonPath("$.data.items[0].name").value("张三"))
|
||||
.andExpect(jsonPath("$.data.items[0].age").value(25))
|
||||
.andExpect(jsonPath("$.data.items[0].status").value("ACTIVE"));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("POST /list")
|
||||
class ListWithQueryTests {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
repository.save(new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java"));
|
||||
repository.save(new TestEntity("李四", 30, TestStatus.INACTIVE, 6000.0, "python"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("list with query filters correctly")
|
||||
void listWithQuery_filtersCorrectly() throws Exception {
|
||||
var query = new QueryBuilder().equal(java.util.Map.of("name", "张三")).build();
|
||||
|
||||
mockMvc.perform(post("/test/list")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(query)))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data.items.length()").value(1))
|
||||
.andExpect(jsonPath("$.data.items[0].name").value("张三"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("list with pagination returns correct page")
|
||||
void listWithPagination_returnsCorrectPage() throws Exception {
|
||||
var query = new Query(null, null, new Query.Pageable(1, 1));
|
||||
|
||||
mockMvc.perform(post("/test/list")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(query)))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data.items.length()").value(1))
|
||||
.andExpect(jsonPath("$.data.total").value(2));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("GET /detail/{id}")
|
||||
class DetailTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("detail with existing id returns entity")
|
||||
void detail_withExistingId_returnsEntity() throws Exception {
|
||||
var entity = repository.save(new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java,spring"));
|
||||
|
||||
mockMvc.perform(get("/test/detail/{id}", entity.getId()))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(0))
|
||||
.andExpect(jsonPath("$.data.id").value(entity.getId()))
|
||||
.andExpect(jsonPath("$.data.name").value("张三"))
|
||||
.andExpect(jsonPath("$.data.age").value(25))
|
||||
.andExpect(jsonPath("$.data.status").value("ACTIVE"))
|
||||
.andExpect(jsonPath("$.data.salary").value(5000.0))
|
||||
.andExpect(jsonPath("$.data.tags").value("java,spring"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("detailItemMapper converts correctly")
|
||||
void detailItemMapper_convertsCorrectly() throws Exception {
|
||||
var entity = repository.save(new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java,spring"));
|
||||
|
||||
mockMvc.perform(get("/test/detail/{id}", entity.getId()))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data.id").value(entity.getId()))
|
||||
.andExpect(jsonPath("$.data.name").value("张三"))
|
||||
.andExpect(jsonPath("$.data.tags").value("java,spring"));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("GET /remove/{id}")
|
||||
class RemoveTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("remove deletes entity")
|
||||
void remove_deletesEntity() throws Exception {
|
||||
var entity = repository.save(new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java"));
|
||||
|
||||
mockMvc.perform(get("/test/remove/{id}", entity.getId()))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(0))
|
||||
.andExpect(jsonPath("$.data").isEmpty());
|
||||
|
||||
assertThat(repository.findById(entity.getId())).isEmpty();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,662 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.integration;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.database.entity.Query;
|
||||
import com.lanyuanxiaoyao.service.template.database.exception.IdNotFoundException;
|
||||
import com.lanyuanxiaoyao.service.template.database.exception.NotCollectionException;
|
||||
import com.lanyuanxiaoyao.service.template.database.exception.NotComparableException;
|
||||
import com.lanyuanxiaoyao.service.template.database.exception.NotStringException;
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.entity.TestEntity;
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.entity.TestStatus;
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.repository.TestRepository;
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.service.TestService;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.data.jpa.test.autoconfigure.DataJpaTest;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
@DataJpaTest
|
||||
@ActiveProfiles("test")
|
||||
@Import({IntegrationTestConfiguration.class, TestService.class})
|
||||
@DisplayName("SimpleServiceSupport Integration Tests")
|
||||
class SimpleServiceSupportIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
TestService service;
|
||||
|
||||
@Autowired
|
||||
TestRepository repository;
|
||||
|
||||
@Autowired
|
||||
EntityManager entityManager;
|
||||
|
||||
@BeforeEach
|
||||
void cleanUp() {
|
||||
repository.deleteAllInBatch();
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("CRUD Operations")
|
||||
class CrudTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("save with new entity returns generated id")
|
||||
void save_withNewEntity_returnsId() {
|
||||
var entity = new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java,spring");
|
||||
|
||||
Long id = service.save(entity);
|
||||
|
||||
assertThat(id).isNotNull().isPositive();
|
||||
var saved = repository.findById(id).orElseThrow();
|
||||
assertThat(saved.getName()).isEqualTo("张三");
|
||||
assertThat(saved.getAge()).isEqualTo(25);
|
||||
assertThat(saved.getStatus()).isEqualTo(TestStatus.ACTIVE);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("save with existing entity updates non-null fields")
|
||||
void save_withExistingEntity_updatesFields() {
|
||||
var entity = new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java");
|
||||
Long id = service.save(entity);
|
||||
|
||||
var update = new TestEntity(null, 26, null, 6000.0, null);
|
||||
update.setId(id);
|
||||
service.save(update);
|
||||
|
||||
var updated = repository.findById(id).orElseThrow();
|
||||
assertThat(updated.getName()).isEqualTo("张三");
|
||||
assertThat(updated.getAge()).isEqualTo(26);
|
||||
assertThat(updated.getStatus()).isEqualTo(TestStatus.ACTIVE);
|
||||
assertThat(updated.getSalary()).isEqualTo(6000.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("save multiple entities saves all")
|
||||
void save_multipleEntities_savesAll() {
|
||||
var entities = List.of(
|
||||
new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java"),
|
||||
new TestEntity("李四", 30, TestStatus.INACTIVE, 6000.0, "python")
|
||||
);
|
||||
|
||||
service.save(entities);
|
||||
|
||||
assertThat(repository.count()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("count returns correct total")
|
||||
void count_returnsCorrectTotal() {
|
||||
repository.saveAll(List.of(
|
||||
new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java"),
|
||||
new TestEntity("李四", 30, TestStatus.INACTIVE, 6000.0, "python")
|
||||
));
|
||||
|
||||
Long count = service.count();
|
||||
|
||||
assertThat(count).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("list returns all entities")
|
||||
void list_returnsAllEntities() {
|
||||
repository.saveAll(List.of(
|
||||
new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java"),
|
||||
new TestEntity("李四", 30, TestStatus.INACTIVE, 6000.0, "python")
|
||||
));
|
||||
|
||||
List<TestEntity> result = service.list();
|
||||
|
||||
assertThat(result).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("list with ids returns matching entities")
|
||||
void list_withIds_returnsMatching() {
|
||||
var e1 = repository.save(new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java"));
|
||||
var e2 = repository.save(new TestEntity("李四", 30, TestStatus.INACTIVE, 6000.0, "python"));
|
||||
repository.save(new TestEntity("王五", 35, TestStatus.DELETED, 7000.0, "go"));
|
||||
|
||||
List<TestEntity> result = service.list(Set.of(e1.getId(), e2.getId()));
|
||||
|
||||
assertThat(result).hasSize(2)
|
||||
.extracting(TestEntity::getName)
|
||||
.containsExactlyInAnyOrder("张三", "李四");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("detail with existing id returns entity")
|
||||
void detail_withExistingId_returnsEntity() {
|
||||
var saved = repository.save(new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java"));
|
||||
|
||||
TestEntity result = service.detail(saved.getId());
|
||||
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.getName()).isEqualTo("张三");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("detail with non-existing id returns null")
|
||||
void detail_withNonExistingId_returnsNull() {
|
||||
TestEntity result = service.detail(999L);
|
||||
|
||||
assertThat(result).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("detailOrThrow with non-existing id throws exception")
|
||||
void detailOrThrow_withNonExistingId_throwsException() {
|
||||
assertThatThrownBy(() -> service.detailOrThrow(999L))
|
||||
.isInstanceOf(IdNotFoundException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("remove deletes entity")
|
||||
void remove_deletesEntity() {
|
||||
var saved = repository.save(new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java"));
|
||||
repository.flush();
|
||||
entityManager.clear();
|
||||
|
||||
service.remove(saved.getId());
|
||||
repository.flush();
|
||||
|
||||
var nativeQuery = entityManager.createNativeQuery("SELECT COUNT(*) FROM test_entity WHERE id = ?");
|
||||
nativeQuery.setParameter(1, saved.getId());
|
||||
var count = ((Number) nativeQuery.getSingleResult()).longValue();
|
||||
assertThat(count).isZero();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("remove multiple entities deletes all")
|
||||
void remove_multipleEntities_deletesAll() {
|
||||
var e1 = repository.save(new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java"));
|
||||
var e2 = repository.save(new TestEntity("李四", 30, TestStatus.INACTIVE, 6000.0, "python"));
|
||||
|
||||
service.remove(Set.of(e1.getId(), e2.getId()));
|
||||
repository.flush();
|
||||
|
||||
assertThat(repository.count()).isZero();
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Equality Conditions")
|
||||
class EqualityConditionTests {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
repository.saveAll(List.of(
|
||||
new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java"),
|
||||
new TestEntity("李四", 30, TestStatus.INACTIVE, 6000.0, null),
|
||||
new TestEntity("王五", 35, TestStatus.DELETED, 7000.0, "go")
|
||||
));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("equal condition filters correctly")
|
||||
void equalCondition_filtersCorrectly() {
|
||||
var query = new QueryBuilder().equal(Map.of("name", "张三")).build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(1);
|
||||
assertThat(result.items().get(0).getName()).isEqualTo("张三");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("notEqual condition filters correctly")
|
||||
void notEqualCondition_filtersCorrectly() {
|
||||
var query = new QueryBuilder().notEqual(Map.of("name", "张三")).build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(2);
|
||||
assertThat(result.items()).noneMatch(e -> "张三".equals(e.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("nullEqual condition filters null values")
|
||||
void nullEqualCondition_filtersNullValues() {
|
||||
var query = new QueryBuilder().nullEqual(List.of("tags")).build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(1);
|
||||
assertThat(result.items().get(0).getTags()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("notNullEqual condition filters non-null values")
|
||||
void notNullEqualCondition_filtersNonNullValues() {
|
||||
var query = new QueryBuilder().notNullEqual(List.of("tags")).build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(2);
|
||||
assertThat(result.items()).allMatch(e -> e.getTags() != null);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("String Conditions")
|
||||
class StringConditionTests {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
repository.saveAll(List.of(
|
||||
new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java,spring"),
|
||||
new TestEntity("李四三", 30, TestStatus.INACTIVE, 6000.0, "python"),
|
||||
new TestEntity("王五", 35, TestStatus.DELETED, 7000.0, "go")
|
||||
));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("like condition filters correctly")
|
||||
void likeCondition_filtersCorrectly() {
|
||||
var query = new QueryBuilder().like(Map.of("name", "%三%")).build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("notLike condition filters correctly")
|
||||
void notLikeCondition_filtersCorrectly() {
|
||||
var query = new QueryBuilder().notLike(Map.of("name", "%三%")).build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(1);
|
||||
assertThat(result.items().get(0).getName()).isEqualTo("王五");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("contain condition filters correctly")
|
||||
void containCondition_filtersCorrectly() {
|
||||
var query = new QueryBuilder().contain(Map.of("name", "三")).build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("notContain condition filters correctly")
|
||||
void notContainCondition_filtersCorrectly() {
|
||||
var query = new QueryBuilder().notContain(Map.of("name", "三")).build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("startWith condition filters correctly")
|
||||
void startWithCondition_filtersCorrectly() {
|
||||
var query = new QueryBuilder().startWith(Map.of("name", "张")).build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(1);
|
||||
assertThat(result.items().get(0).getName()).isEqualTo("张三");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("notStartWith condition filters correctly")
|
||||
void notStartWithCondition_filtersCorrectly() {
|
||||
var query = new QueryBuilder().notStartWith(Map.of("name", "张")).build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(2);
|
||||
assertThat(result.items()).noneMatch(e -> e.getName().startsWith("张"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("endWith condition filters correctly")
|
||||
void endWithCondition_filtersCorrectly() {
|
||||
var query = new QueryBuilder().endWith(Map.of("name", "三")).build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("notEndWith condition filters correctly")
|
||||
void notEndWithCondition_filtersCorrectly() {
|
||||
var query = new QueryBuilder().notEndWith(Map.of("name", "三")).build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(1);
|
||||
assertThat(result.items().get(0).getName()).isEqualTo("王五");
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Comparison Conditions")
|
||||
class ComparisonConditionTests {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
repository.saveAll(List.of(
|
||||
new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java"),
|
||||
new TestEntity("李四", 35, TestStatus.INACTIVE, 6000.0, "python"),
|
||||
new TestEntity("王五", 45, TestStatus.DELETED, 7000.0, "go")
|
||||
));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("great condition filters correctly")
|
||||
void greatCondition_filtersCorrectly() {
|
||||
var query = new QueryBuilder().great(Map.of("age", 30)).build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(2);
|
||||
assertThat(result.items()).allMatch(e -> e.getAge() > 30);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("less condition filters correctly")
|
||||
void lessCondition_filtersCorrectly() {
|
||||
var query = new QueryBuilder().less(Map.of("age", 40)).build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(2);
|
||||
assertThat(result.items()).allMatch(e -> e.getAge() < 40);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("greatEqual condition filters correctly")
|
||||
void greatEqualCondition_filtersCorrectly() {
|
||||
var query = new QueryBuilder().greatEqual(Map.of("age", 35)).build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(2);
|
||||
assertThat(result.items()).allMatch(e -> e.getAge() >= 35);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("lessEqual condition filters correctly")
|
||||
void lessEqualCondition_filtersCorrectly() {
|
||||
var query = new QueryBuilder().lessEqual(Map.of("age", 35)).build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(2);
|
||||
assertThat(result.items()).allMatch(e -> e.getAge() <= 35);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("between condition filters correctly")
|
||||
void betweenCondition_filtersCorrectly() {
|
||||
var query = new QueryBuilder().between(Map.of("age", new Query.Queryable.Between(25, 35))).build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(2);
|
||||
assertThat(result.items()).allMatch(e -> e.getAge() >= 25 && e.getAge() <= 35);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("notBetween condition filters correctly")
|
||||
void notBetweenCondition_filtersCorrectly() {
|
||||
var query = new QueryBuilder().notBetween(Map.of("age", new Query.Queryable.Between(26, 44))).build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(2);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Collection Conditions")
|
||||
class CollectionConditionTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("inside condition filters correctly")
|
||||
void insideCondition_filtersCorrectly() {
|
||||
repository.saveAll(List.of(
|
||||
new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java"),
|
||||
new TestEntity("李四", 30, TestStatus.INACTIVE, 6000.0, "python"),
|
||||
new TestEntity("王五", 35, TestStatus.DELETED, 7000.0, "go")
|
||||
));
|
||||
|
||||
var query = new QueryBuilder().inside(Map.of("age", List.of(25, 35))).build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("notInside condition filters correctly")
|
||||
void notInsideCondition_filtersCorrectly() {
|
||||
repository.saveAll(List.of(
|
||||
new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java"),
|
||||
new TestEntity("李四", 30, TestStatus.INACTIVE, 6000.0, "python"),
|
||||
new TestEntity("王五", 35, TestStatus.DELETED, 7000.0, "go")
|
||||
));
|
||||
|
||||
var query = new QueryBuilder().notInside(Map.of("age", List.of(25, 35))).build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(1);
|
||||
assertThat(result.items().get(0).getAge()).isEqualTo(30);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("empty condition filters correctly")
|
||||
void emptyCondition_filtersCorrectly() {
|
||||
repository.saveAll(List.of(
|
||||
new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java"),
|
||||
new TestEntity("李四", 30, TestStatus.INACTIVE, 6000.0, "python"),
|
||||
new TestEntity("王五", 35, TestStatus.DELETED, 7000.0, "go")
|
||||
));
|
||||
|
||||
var query = new QueryBuilder().empty(List.of("tags")).build();
|
||||
|
||||
assertThatThrownBy(() -> service.list(query))
|
||||
.isInstanceOf(NotCollectionException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("notEmpty condition filters correctly")
|
||||
void notEmptyCondition_filtersCorrectly() {
|
||||
repository.saveAll(List.of(
|
||||
new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java"),
|
||||
new TestEntity("李四", 30, TestStatus.INACTIVE, 6000.0, "python"),
|
||||
new TestEntity("王五", 35, TestStatus.DELETED, 7000.0, "go")
|
||||
));
|
||||
|
||||
var query = new QueryBuilder().notEmpty(List.of("tags")).build();
|
||||
|
||||
assertThatThrownBy(() -> service.list(query))
|
||||
.isInstanceOf(NotCollectionException.class);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Enum Condition")
|
||||
class EnumConditionTests {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
repository.saveAll(List.of(
|
||||
new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java"),
|
||||
new TestEntity("李四", 30, TestStatus.INACTIVE, 6000.0, "python"),
|
||||
new TestEntity("王五", 35, TestStatus.DELETED, 7000.0, "go")
|
||||
));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("enum condition filters correctly")
|
||||
void enumCondition_filtersCorrectly() {
|
||||
var query = new QueryBuilder().equal(Map.of("status", "ACTIVE")).build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(1);
|
||||
assertThat(result.items().get(0).getStatus()).isEqualTo(TestStatus.ACTIVE);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Combined Conditions")
|
||||
class CombinedConditionTests {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
repository.saveAll(List.of(
|
||||
new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java"),
|
||||
new TestEntity("李四", 30, TestStatus.INACTIVE, 6000.0, "python"),
|
||||
new TestEntity("王五", 35, TestStatus.ACTIVE, 7000.0, "go"),
|
||||
new TestEntity("赵六", 40, TestStatus.ACTIVE, 8000.0, "rust")
|
||||
));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("combined conditions filter correctly")
|
||||
void combinedConditions_filterCorrectly() {
|
||||
var query = new QueryBuilder()
|
||||
.equal(Map.of("status", "ACTIVE"))
|
||||
.great(Map.of("age", 30))
|
||||
.build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(2);
|
||||
assertThat(result.items()).allMatch(e -> e.getStatus() == TestStatus.ACTIVE && e.getAge() > 30);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Pagination and Sorting")
|
||||
class PaginationSortingTests {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
repository.deleteAllInBatch();
|
||||
entityManager.flush();
|
||||
entityManager.clear();
|
||||
for (int i = 1; i <= 25; i++) {
|
||||
repository.save(new TestEntity("用户" + i, 20 + i, TestStatus.ACTIVE, 5000.0 + i * 100, "tag" + i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("default pagination returns first page with 10 items")
|
||||
void defaultPagination_returnsFirstPage() {
|
||||
var query = new Query(null, null, null);
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(10);
|
||||
assertThat(result.total()).isEqualTo(25);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("custom pagination returns correct page")
|
||||
void customPagination_returnsCorrectPage() {
|
||||
var query = new Query(null, null, new Query.Pageable(1, 5));
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(5);
|
||||
assertThat(result.total()).isEqualTo(25);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("custom sorting sorts correctly")
|
||||
void customSorting_sortsCorrectly() {
|
||||
var query = new Query(null, List.of(new Query.Sortable("age", Query.Sortable.Direction.ASC)), new Query.Pageable(1, 10));
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(10);
|
||||
assertThat(result.items().get(0).getAge()).isEqualTo(21);
|
||||
assertThat(result.items()).isSortedAccordingTo((a, b) -> a.getAge().compareTo(b.getAge()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("multiple sort fields sort correctly")
|
||||
void multipleSortFields_sortCorrectly() {
|
||||
repository.deleteAllInBatch();
|
||||
entityManager.flush();
|
||||
entityManager.clear();
|
||||
|
||||
repository.saveAll(List.of(
|
||||
new TestEntity("A", 20, TestStatus.ACTIVE, 5000.0, null),
|
||||
new TestEntity("B", 30, TestStatus.ACTIVE, 3000.0, null),
|
||||
new TestEntity("C", 30, TestStatus.ACTIVE, 4000.0, null)
|
||||
));
|
||||
|
||||
var query = new Query(null, List.of(
|
||||
new Query.Sortable("age", Query.Sortable.Direction.ASC),
|
||||
new Query.Sortable("salary", Query.Sortable.Direction.DESC)
|
||||
), new Query.Pageable(1, 10));
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).hasSize(3);
|
||||
assertThat(result.items().get(0).getName()).isEqualTo("A");
|
||||
assertThat(result.items().get(1).getName()).isEqualTo("C");
|
||||
assertThat(result.items().get(2).getName()).isEqualTo("B");
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Exception Handling")
|
||||
class ExceptionTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("like on non-string field throws NotStringException")
|
||||
void likeOnNonStringField_throwsNotStringException() {
|
||||
repository.save(new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java"));
|
||||
|
||||
var query = new QueryBuilder().like(Map.of("age", "%5%")).build();
|
||||
|
||||
assertThatThrownBy(() -> service.list(query))
|
||||
.isInstanceOf(NotStringException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("great on enum field with enum value throws exception")
|
||||
void greatOnEnumField_withEnumValue_throwsException() {
|
||||
repository.save(new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java"));
|
||||
|
||||
var query = new QueryBuilder().great(Map.of("status", TestStatus.ACTIVE)).build();
|
||||
|
||||
assertThatThrownBy(() -> service.list(query))
|
||||
.isInstanceOf(org.springframework.dao.InvalidDataAccessApiUsageException.class)
|
||||
.hasMessageContaining("枚举类型字段需要 String 类型的值");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("empty on non-collection field throws NotCollectionException")
|
||||
void emptyOnNonCollectionField_throwsNotCollectionException() {
|
||||
repository.save(new TestEntity("张三", 25, TestStatus.ACTIVE, 5000.0, "java"));
|
||||
|
||||
var query = new QueryBuilder().empty(List.of("name")).build();
|
||||
|
||||
assertThatThrownBy(() -> service.list(query))
|
||||
.isInstanceOf(NotCollectionException.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.integration;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.entity.TestSoftDeleteEntity;
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.repository.TestSoftDeleteRepository;
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.service.TestSoftDeleteService;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.data.jpa.test.autoconfigure.DataJpaTest;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DataJpaTest
|
||||
@ActiveProfiles("test")
|
||||
@Import({IntegrationTestConfiguration.class, TestSoftDeleteService.class})
|
||||
@DisplayName("@SoftDelete Integration Tests")
|
||||
class SoftDeleteIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
TestSoftDeleteService service;
|
||||
|
||||
@Autowired
|
||||
TestSoftDeleteRepository repository;
|
||||
|
||||
@Autowired
|
||||
EntityManager entityManager;
|
||||
|
||||
@BeforeEach
|
||||
void cleanUp() {
|
||||
repository.deleteAllInBatch();
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Save and Delete Behavior")
|
||||
class SaveDeleteTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("save entity sets deleted to false")
|
||||
void saveEntity_setsDeletedFalse() {
|
||||
var entity = new TestSoftDeleteEntity("张三", 25);
|
||||
|
||||
Long id = service.save(entity);
|
||||
|
||||
var saved = repository.findById(id).orElseThrow();
|
||||
assertThat(saved.getName()).isEqualTo("张三");
|
||||
|
||||
var nativeQuery = entityManager.createNativeQuery("SELECT deleted FROM test_soft_delete_entity WHERE id = ?");
|
||||
nativeQuery.setParameter(1, id);
|
||||
var deleted = (Boolean) nativeQuery.getSingleResult();
|
||||
assertThat(deleted).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("remove sets deleted to true")
|
||||
void remove_setsDeletedTrue() {
|
||||
var entity = new TestSoftDeleteEntity("张三", 25);
|
||||
Long id = service.save(entity);
|
||||
repository.flush();
|
||||
entityManager.clear();
|
||||
|
||||
service.remove(id);
|
||||
repository.flush();
|
||||
|
||||
var nativeQuery = entityManager.createNativeQuery("SELECT deleted FROM test_soft_delete_entity WHERE id = ?");
|
||||
nativeQuery.setParameter(1, id);
|
||||
var deleted = (Boolean) nativeQuery.getSingleResult();
|
||||
assertThat(deleted).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("batch remove sets deleted to true")
|
||||
void batchRemove_setsDeletedTrue() {
|
||||
var e1 = new TestSoftDeleteEntity("张三", 25);
|
||||
var e2 = new TestSoftDeleteEntity("李四", 30);
|
||||
Long id1 = service.save(e1);
|
||||
Long id2 = service.save(e2);
|
||||
|
||||
service.remove(Set.of(id1, id2));
|
||||
|
||||
var nativeQuery = entityManager.createNativeQuery("SELECT COUNT(*) FROM test_soft_delete_entity WHERE deleted = true");
|
||||
var count = ((Number) nativeQuery.getSingleResult()).longValue();
|
||||
assertThat(count).isEqualTo(2);
|
||||
|
||||
assertThat(repository.findAll()).isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Query Behavior")
|
||||
class QueryTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("list filters out deleted entities")
|
||||
void list_filtersDeletedEntities() {
|
||||
var e1 = repository.save(new TestSoftDeleteEntity("张三", 25));
|
||||
repository.save(new TestSoftDeleteEntity("李四", 30));
|
||||
|
||||
entityManager.createNativeQuery("UPDATE test_soft_delete_entity SET deleted = true WHERE id = ?")
|
||||
.setParameter(1, e1.getId())
|
||||
.executeUpdate();
|
||||
entityManager.clear();
|
||||
|
||||
List<TestSoftDeleteEntity> result = service.list();
|
||||
|
||||
assertThat(result).hasSize(1);
|
||||
assertThat(result.get(0).getName()).isEqualTo("李四");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("count excludes deleted entities")
|
||||
void count_excludesDeletedEntities() {
|
||||
var e1 = repository.save(new TestSoftDeleteEntity("张三", 25));
|
||||
repository.save(new TestSoftDeleteEntity("李四", 30));
|
||||
|
||||
entityManager.createNativeQuery("UPDATE test_soft_delete_entity SET deleted = true WHERE id = ?")
|
||||
.setParameter(1, e1.getId())
|
||||
.executeUpdate();
|
||||
entityManager.clear();
|
||||
|
||||
Long count = service.count();
|
||||
|
||||
assertThat(count).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("detail returns null for deleted entity")
|
||||
void detail_returnsNullForDeletedEntity() {
|
||||
var entity = repository.save(new TestSoftDeleteEntity("张三", 25));
|
||||
|
||||
entityManager.createNativeQuery("UPDATE test_soft_delete_entity SET deleted = true WHERE id = ?")
|
||||
.setParameter(1, entity.getId())
|
||||
.executeUpdate();
|
||||
entityManager.clear();
|
||||
|
||||
TestSoftDeleteEntity result = service.detail(entity.getId());
|
||||
|
||||
assertThat(result).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("query with conditions filters deleted entities")
|
||||
void queryWithConditions_filtersDeletedEntities() {
|
||||
var e1 = repository.save(new TestSoftDeleteEntity("张三", 25));
|
||||
repository.save(new TestSoftDeleteEntity("李四", 30));
|
||||
|
||||
entityManager.createNativeQuery("UPDATE test_soft_delete_entity SET deleted = true WHERE id = ?")
|
||||
.setParameter(1, e1.getId())
|
||||
.executeUpdate();
|
||||
entityManager.clear();
|
||||
|
||||
var query = new QueryBuilder().equal(Map.of("name", "张三")).build();
|
||||
|
||||
var result = service.list(query);
|
||||
|
||||
assertThat(result.items()).isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Restore Behavior")
|
||||
class RestoreTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("restore deleted entity makes it visible again")
|
||||
void restore_makesEntityVisible() {
|
||||
var entity = repository.save(new TestSoftDeleteEntity("张三", 25));
|
||||
|
||||
entityManager.createNativeQuery("UPDATE test_soft_delete_entity SET deleted = true WHERE id = ?")
|
||||
.setParameter(1, entity.getId())
|
||||
.executeUpdate();
|
||||
entityManager.clear();
|
||||
|
||||
assertThat(service.list()).isEmpty();
|
||||
|
||||
entityManager.createNativeQuery("UPDATE test_soft_delete_entity SET deleted = false WHERE id = ?")
|
||||
.setParameter(1, entity.getId())
|
||||
.executeUpdate();
|
||||
entityManager.clear();
|
||||
|
||||
List<TestSoftDeleteEntity> result = service.list();
|
||||
assertThat(result).hasSize(1);
|
||||
assertThat(result.get(0).getName()).isEqualTo("张三");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.integration.controller;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.entity.TestEntity;
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.entity.TestStatus;
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.service.TestService;
|
||||
import com.lanyuanxiaoyao.service.template.database.controller.SimpleControllerSupport;
|
||||
import java.util.function.Function;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/test")
|
||||
public class TestController extends SimpleControllerSupport<TestEntity, TestController.SaveItem, TestController.ListItem, TestController.DetailItem> {
|
||||
public TestController(TestService service) {
|
||||
super(service);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<SaveItem, TestEntity> saveItemMapper() {
|
||||
return item -> new TestEntity(item.name, item.age, item.status, item.salary, item.tags);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<TestEntity, ListItem> listItemMapper() {
|
||||
return entity -> new ListItem(entity.getId(), entity.getName(), entity.getAge(), entity.getStatus());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<TestEntity, DetailItem> detailItemMapper() {
|
||||
return entity -> new DetailItem(entity.getId(), entity.getName(), entity.getAge(), entity.getStatus(), entity.getSalary(), entity.getTags());
|
||||
}
|
||||
|
||||
public record SaveItem(String name, Integer age, TestStatus status, Double salary, String tags) {
|
||||
}
|
||||
|
||||
public record ListItem(Long id, String name, Integer age, TestStatus status) {
|
||||
}
|
||||
|
||||
public record DetailItem(Long id, String name, Integer age, TestStatus status, Double salary, String tags) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.integration.controller;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.entity.TestSoftDeleteEntity;
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.service.TestSoftDeleteService;
|
||||
import com.lanyuanxiaoyao.service.template.database.controller.SimpleControllerSupport;
|
||||
import java.util.function.Function;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/test-soft-delete")
|
||||
public class TestSoftDeleteController extends SimpleControllerSupport<TestSoftDeleteEntity, TestSoftDeleteController.SaveItem, TestSoftDeleteController.ListItem, TestSoftDeleteController.DetailItem> {
|
||||
public TestSoftDeleteController(TestSoftDeleteService service) {
|
||||
super(service);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<SaveItem, TestSoftDeleteEntity> saveItemMapper() {
|
||||
return item -> new TestSoftDeleteEntity(item.name, item.age);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<TestSoftDeleteEntity, ListItem> listItemMapper() {
|
||||
return entity -> new ListItem(entity.getId(), entity.getName(), entity.getAge());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<TestSoftDeleteEntity, DetailItem> detailItemMapper() {
|
||||
return entity -> new DetailItem(entity.getId(), entity.getName(), entity.getAge());
|
||||
}
|
||||
|
||||
public record SaveItem(String name, Integer age) {
|
||||
}
|
||||
|
||||
public record ListItem(Long id, String name, Integer age) {
|
||||
}
|
||||
|
||||
public record DetailItem(Long id, String name, Integer age) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.integration.entity;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.database.entity.SimpleEntity;
|
||||
import com.lanyuanxiaoyao.service.template.database.entity.SnowflakeId;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.EnumType;
|
||||
import jakarta.persistence.Enumerated;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.FieldNameConstants;
|
||||
|
||||
@Entity
|
||||
@Table(name = "test_entity")
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@FieldNameConstants
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class TestEntity extends SimpleEntity {
|
||||
@SnowflakeId
|
||||
@Column(comment = "名称")
|
||||
private String name;
|
||||
|
||||
@Column(comment = "年龄")
|
||||
private Integer age;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(comment = "状态")
|
||||
private TestStatus status;
|
||||
|
||||
@Column(comment = "薪资")
|
||||
private Double salary;
|
||||
|
||||
@Column(comment = "标签")
|
||||
private String tags;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.integration.entity;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.database.entity.SimpleEntity;
|
||||
import com.lanyuanxiaoyao.service.template.database.entity.SnowflakeId;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.FieldNameConstants;
|
||||
import org.hibernate.annotations.SoftDelete;
|
||||
import org.hibernate.annotations.SoftDeleteType;
|
||||
|
||||
@Entity
|
||||
@Table(name = "test_soft_delete_entity")
|
||||
@SoftDelete(strategy = SoftDeleteType.DELETED)
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@FieldNameConstants
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class TestSoftDeleteEntity extends SimpleEntity {
|
||||
@SnowflakeId
|
||||
@Column(comment = "名称")
|
||||
private String name;
|
||||
|
||||
@Column(comment = "年龄")
|
||||
private Integer age;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.integration.entity;
|
||||
|
||||
public enum TestStatus {
|
||||
ACTIVE,
|
||||
INACTIVE,
|
||||
DELETED
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.integration.repository;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.entity.TestEntity;
|
||||
import com.lanyuanxiaoyao.service.template.database.repository.SimpleRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface TestRepository extends SimpleRepository<TestEntity> {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.integration.repository;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.entity.TestSoftDeleteEntity;
|
||||
import com.lanyuanxiaoyao.service.template.database.repository.SimpleRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface TestSoftDeleteRepository extends SimpleRepository<TestSoftDeleteEntity> {
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.integration.service;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.entity.TestEntity;
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.repository.TestRepository;
|
||||
import com.lanyuanxiaoyao.service.template.database.service.SimpleServiceSupport;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class TestService extends SimpleServiceSupport<TestEntity> {
|
||||
public TestService(TestRepository repository) {
|
||||
super(repository);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.lanyuanxiaoyao.service.template.database.integration.service;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.entity.TestSoftDeleteEntity;
|
||||
import com.lanyuanxiaoyao.service.template.database.integration.repository.TestSoftDeleteRepository;
|
||||
import com.lanyuanxiaoyao.service.template.database.service.SimpleServiceSupport;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class TestSoftDeleteService extends SimpleServiceSupport<TestSoftDeleteEntity> {
|
||||
public TestSoftDeleteService(TestSoftDeleteRepository repository) {
|
||||
super(repository);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
|
||||
driver-class-name: org.h2.Driver
|
||||
username: sa
|
||||
password:
|
||||
|
||||
jpa:
|
||||
hibernate:
|
||||
ddl-auto: create-drop
|
||||
|
||||
decorator:
|
||||
datasource:
|
||||
p6spy:
|
||||
logging: slf4j
|
||||
log-format: "%(executionTime) ms | %(sqlSingleLine)"
|
||||
|
||||
logging:
|
||||
level:
|
||||
p6spy: INFO
|
||||
@@ -1,85 +0,0 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa.entity;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.Instant;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.id.IdentifierGenerator;
|
||||
|
||||
@Slf4j
|
||||
public class SnowflakeIdGenerator implements IdentifierGenerator {
|
||||
@Override
|
||||
public Serializable generate(SharedSessionContractImplementor session, Object object) {
|
||||
try {
|
||||
return Snowflake.next();
|
||||
} catch (Exception e) {
|
||||
log.error("Generate snowflake id failed", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Snowflake {
|
||||
/**
|
||||
* 起始的时间戳
|
||||
*/
|
||||
private final static long START_TIMESTAMP = 1;
|
||||
|
||||
/**
|
||||
* 序列号占用的位数
|
||||
*/
|
||||
private final static long SEQUENCE_BIT = 11;
|
||||
|
||||
/**
|
||||
* 序列号最大值
|
||||
*/
|
||||
private final static long MAX_SEQUENCE_BIT = ~(-1 << SEQUENCE_BIT);
|
||||
|
||||
/**
|
||||
* 时间戳值向左位移
|
||||
*/
|
||||
private final static long TIMESTAMP_OFFSET = SEQUENCE_BIT;
|
||||
|
||||
/**
|
||||
* 序列号
|
||||
*/
|
||||
private static long sequence = 0;
|
||||
/**
|
||||
* 上一次时间戳
|
||||
*/
|
||||
private static long lastTimestamp = -1;
|
||||
|
||||
public static synchronized long next() {
|
||||
long currentTimestamp = nowTimestamp();
|
||||
if (currentTimestamp < lastTimestamp) {
|
||||
throw new RuntimeException("Clock have moved backwards.");
|
||||
}
|
||||
|
||||
if (currentTimestamp == lastTimestamp) {
|
||||
// 相同毫秒内, 序列号自增
|
||||
sequence = (sequence + 1) & MAX_SEQUENCE_BIT;
|
||||
// 同一毫秒的序列数已经达到最大
|
||||
if (sequence == 0) {
|
||||
currentTimestamp = nextTimestamp();
|
||||
}
|
||||
} else {
|
||||
// 不同毫秒内, 序列号置为0
|
||||
sequence = 0;
|
||||
}
|
||||
|
||||
lastTimestamp = currentTimestamp;
|
||||
return (currentTimestamp - START_TIMESTAMP) << TIMESTAMP_OFFSET | sequence;
|
||||
}
|
||||
|
||||
private static long nextTimestamp() {
|
||||
long milli = nowTimestamp();
|
||||
while (milli <= lastTimestamp) {
|
||||
milli = nowTimestamp();
|
||||
}
|
||||
return milli;
|
||||
}
|
||||
|
||||
private static long nowTimestamp() {
|
||||
return Instant.now().toEpochMilli();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,665 +0,0 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa.service;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.common.controller.Query;
|
||||
import com.lanyuanxiaoyao.service.template.common.helper.ObjectHelper;
|
||||
import com.lanyuanxiaoyao.service.template.common.service.Page;
|
||||
import com.lanyuanxiaoyao.service.template.common.service.SimpleService;
|
||||
import com.lanyuanxiaoyao.service.template.jpa.entity.IdOnlyEntity;
|
||||
import com.lanyuanxiaoyao.service.template.jpa.entity.SimpleEntity;
|
||||
import com.lanyuanxiaoyao.service.template.jpa.repository.SimpleRepository;
|
||||
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||
import jakarta.persistence.criteria.CriteriaQuery;
|
||||
import jakarta.persistence.criteria.Path;
|
||||
import jakarta.persistence.criteria.Predicate;
|
||||
import jakarta.persistence.criteria.Root;
|
||||
import jakarta.transaction.Transactional;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.mapstruct.Named;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
|
||||
/**
|
||||
* 简单服务支持类,提供基础的业务逻辑实现
|
||||
* <p>
|
||||
* 该类实现了SimpleService接口,提供实体的增删改查等基本操作。
|
||||
* 通过继承此类,可以快速实现常见的业务逻辑功能,包括:
|
||||
* <ul>
|
||||
* <li>实体的保存和更新(支持部分字段更新)</li>
|
||||
* <li>实体的条件查询和分页查询</li>
|
||||
* <li>实体的详情查询(多种方式)</li>
|
||||
* <li>实体的删除操作(支持批量删除)</li>
|
||||
* <li>动态查询条件构建</li>
|
||||
* </ul>
|
||||
* </p>
|
||||
*
|
||||
* <h3>设计特点</h3>
|
||||
* <ul>
|
||||
* <li>泛型设计,支持任意实体类型</li>
|
||||
* <li>事务管理,确保数据一致性</li>
|
||||
* <li>动态查询条件,支持复杂的业务查询</li>
|
||||
* <li>部分更新,只更新非空字段</li>
|
||||
* <li>可扩展的查询条件构建</li>
|
||||
* </ul>
|
||||
*
|
||||
* <h3>使用说明</h3>
|
||||
* <p>子类可以重写以下方法:</p>
|
||||
* <ul>
|
||||
* <li>listPredicate(): 添加自定义的查询条件</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param <ENTITY> 实体类型,必须继承SimpleEntity
|
||||
*/
|
||||
@Slf4j
|
||||
public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implements SimpleService<ENTITY> {
|
||||
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
private static final int DEFAULT_PAGE_INDEX = 1;
|
||||
private static final int DEFAULT_PAGE_SIZE = 10;
|
||||
protected final SimpleRepository<ENTITY> repository;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param repository 简单仓库实例
|
||||
*/
|
||||
public SimpleServiceSupport(SimpleRepository<ENTITY> repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存实体对象
|
||||
* <p>
|
||||
* 使用saveOrUpdateByNotNullProperties方法保存实体,只更新非空字段。
|
||||
* 该方法具有事务性,遇到任何异常都会回滚。
|
||||
* </p>
|
||||
*
|
||||
* @param entity 需要保存的实体对象
|
||||
* @return 返回保存后的实体ID
|
||||
*/
|
||||
@Transactional(rollbackOn = Throwable.class)
|
||||
@Override
|
||||
public Long save(ENTITY entity) {
|
||||
entity = repository.saveOrUpdateByNotNullProperties(entity);
|
||||
return entity.getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量保存实体对象集合
|
||||
* <p>
|
||||
* 使用saveOrUpdateAllByNotNullProperties方法,只更新非空字段。
|
||||
* 该方法具有事务性,遇到任何异常都会回滚。
|
||||
* </p>
|
||||
*
|
||||
* @param entities 需要保存的实体对象集合
|
||||
*/
|
||||
@Transactional(rollbackOn = Throwable.class)
|
||||
@Override
|
||||
public void save(Iterable<ENTITY> entities) {
|
||||
repository.saveOrUpdateAllByNotNullProperties(entities);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计符合条件的实体数量
|
||||
* <p>
|
||||
* 根据listPredicate方法构建的条件统计实体数量。
|
||||
* </p>
|
||||
*
|
||||
* @return 返回符合条件的实体数量
|
||||
*/
|
||||
@Override
|
||||
public Long count() {
|
||||
return repository.count(this::listPredicate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有符合条件的实体列表
|
||||
* <p>
|
||||
* 根据listPredicate方法构建的条件查询所有实体。
|
||||
* </p>
|
||||
*
|
||||
* @return 返回符合条件的实体列表
|
||||
*/
|
||||
@Override
|
||||
public List<ENTITY> list() {
|
||||
return repository.findAll(this::listPredicate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID集合获取实体列表
|
||||
* <p>
|
||||
* 根据提供的ID集合查询对应的实体列表,并结合listPredicate方法构建的条件。
|
||||
* </p>
|
||||
*
|
||||
* @param ids ID集合
|
||||
* @return 返回ID集合对应的实体列表
|
||||
*/
|
||||
@Override
|
||||
public List<ENTITY> list(Set<Long> ids) {
|
||||
if (ObjectHelper.isEmpty(ids)) {
|
||||
return List.of();
|
||||
}
|
||||
return repository.findAll(
|
||||
(root, query, builder) -> {
|
||||
var predicate = listPredicate(root, query, builder);
|
||||
var idsPredicate = builder.in(root.get(IdOnlyEntity.Fields.id)).value(ids);
|
||||
return ObjectHelper.isNull(predicate)
|
||||
? idsPredicate
|
||||
: builder.and(predicate, idsPredicate);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析字段路径
|
||||
* <p>
|
||||
* 支持多级字段路径解析,使用"."分隔多级字段。
|
||||
* 例如: "user.name" 表示实体的user属性的name字段。
|
||||
* </p>
|
||||
*
|
||||
* @param root JPA Criteria查询根节点
|
||||
* @param column 字段路径字符串
|
||||
* @param <Y> 字段类型
|
||||
* @return 返回字段路径对象
|
||||
* @throws IllegalArgumentException 当字段路径为空时抛出
|
||||
*/
|
||||
private <Y> Path<Y> column(Root<ENTITY> root, String column) {
|
||||
if (ObjectHelper.isEmpty(column)) {
|
||||
throw new IllegalArgumentException("Column cannot be blank");
|
||||
}
|
||||
var columns = column.split("\\.");
|
||||
Path<Y> path = root.get(columns[0]);
|
||||
for (int i = 1; i < columns.length; i++) {
|
||||
path = path.get(columns[i]);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理字段值
|
||||
* <p>
|
||||
* 对于枚举类型字段,将字符串值转换为对应的枚举值。
|
||||
* 对于LocalDateTime类型字段,将字符串转换为时间对象。
|
||||
* 其他类型直接返回原值。
|
||||
* </p>
|
||||
*
|
||||
* @param column 字段路径
|
||||
* @param value 字段值
|
||||
* @param <Y> 字段类型
|
||||
* @return 处理后的字段值
|
||||
* @throws IllegalArgumentException 当枚举类型字段的值不是字符串时抛出
|
||||
*/
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
private <Y> Object value(Path<Y> column, Object value) {
|
||||
if (ObjectHelper.isNull(value)) {
|
||||
return null;
|
||||
}
|
||||
var javaType = column.getJavaType();
|
||||
if (javaType.isEnum()) {
|
||||
if (value instanceof String enumName) {
|
||||
var enumType = (Class<Enum>) javaType;
|
||||
return Enum.valueOf(enumType, enumName);
|
||||
} else {
|
||||
throw new IllegalArgumentException("枚举类型字段需要 String 类型的值");
|
||||
}
|
||||
} else if (javaType.isAssignableFrom(LocalDateTime.class)) {
|
||||
return LocalDateTime.parse(String.valueOf(value), DATE_TIME_FORMATTER);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建查询条件谓词列表
|
||||
* <p>
|
||||
* 根据Query.Queryable对象构建JPA Criteria查询的谓词列表。
|
||||
* 支持多种查询条件类型,包括相等、不等、模糊匹配、范围查询等。
|
||||
* </p>
|
||||
*
|
||||
* @param queryable 查询条件对象
|
||||
* @param root JPA Criteria查询根节点
|
||||
* @param query JPA Criteria查询对象
|
||||
* @param builder JPA Criteria构建器
|
||||
* @return 返回构建的谓词列表
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Predicate queryPredicates(Query.Queryable queryable, Root<ENTITY> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
|
||||
var predicates = new ArrayList<Predicate>();
|
||||
if (ObjectHelper.isNull(queryable)) {
|
||||
return null;
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.nullEqual())) {
|
||||
queryable.nullEqual().forEach(column -> predicates.add(builder.isNull(column(root, column))));
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.notNullEqual())) {
|
||||
queryable.notNullEqual().forEach(column -> predicates.add(builder.isNotNull(column(root, column))));
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.empty())) {
|
||||
queryable.empty().forEach(column -> {
|
||||
var path = this.<Collection<Object>>column(root, column);
|
||||
checkCollection(path, column);
|
||||
predicates.add(builder.isEmpty(path));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.notEmpty())) {
|
||||
queryable.notEmpty().forEach(column -> {
|
||||
var path = this.<Collection<Object>>column(root, column);
|
||||
checkCollection(path, column);
|
||||
predicates.add(builder.isNotEmpty(path));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.equal())) {
|
||||
queryable.equal().forEach((column, value) -> {
|
||||
var path = column(root, column);
|
||||
predicates.add(builder.equal(path, value(path, value)));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.notEqual())) {
|
||||
queryable.notEqual().forEach((column, value) -> {
|
||||
var path = column(root, column);
|
||||
predicates.add(builder.notEqual(path, value(path, value)));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.like())) {
|
||||
queryable.like().forEach((column, value) -> {
|
||||
var path = this.<String>column(root, column);
|
||||
checkString(path, value, column);
|
||||
predicates.add(builder.like(path, value));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.notLike())) {
|
||||
queryable.notLike().forEach((column, value) -> {
|
||||
var path = this.<String>column(root, column);
|
||||
checkString(path, value, column);
|
||||
predicates.add(builder.notLike(path, value));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.contain())) {
|
||||
queryable.contain().forEach((column, value) -> {
|
||||
var path = this.<String>column(root, column);
|
||||
checkString(path, value, column);
|
||||
predicates.add(builder.like(path, "%" + value + "%"));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.notContain())) {
|
||||
queryable.notContain().forEach((column, value) -> {
|
||||
var path = this.<String>column(root, column);
|
||||
checkString(path, value, column);
|
||||
predicates.add(builder.notLike(path, "%" + value + "%"));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.startWith())) {
|
||||
queryable.startWith().forEach((column, value) -> {
|
||||
var path = this.<String>column(root, column);
|
||||
checkString(path, value, column);
|
||||
predicates.add(builder.like(path, value + "%"));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.notStartWith())) {
|
||||
queryable.notStartWith().forEach((column, value) -> {
|
||||
var path = this.<String>column(root, column);
|
||||
checkString(path, value, column);
|
||||
predicates.add(builder.notLike(path, value + "%"));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.endWith())) {
|
||||
queryable.endWith().forEach((column, value) -> {
|
||||
var path = this.<String>column(root, column);
|
||||
checkString(path, value, column);
|
||||
predicates.add(builder.like(path, "%" + value));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.notEndWith())) {
|
||||
queryable.notEndWith().forEach((column, value) -> {
|
||||
var path = this.<String>column(root, column);
|
||||
checkString(path, value, column);
|
||||
predicates.add(builder.notLike(path, "%" + value));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.great())) {
|
||||
queryable.great().forEach((column, value) -> {
|
||||
var path = this.<Comparable<Object>>column(root, column);
|
||||
checkComparable(path, value, column);
|
||||
predicates.add(builder.greaterThan(path, (Comparable<Object>) value(path, value)));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.less())) {
|
||||
queryable.less().forEach((column, value) -> {
|
||||
var path = this.<Comparable<Object>>column(root, column);
|
||||
checkComparable(path, value, column);
|
||||
predicates.add(builder.lessThan(path, (Comparable<Object>) value(path, value)));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.greatEqual())) {
|
||||
queryable.greatEqual().forEach((column, value) -> {
|
||||
var path = this.<Comparable<Object>>column(root, column);
|
||||
checkComparable(path, value, column);
|
||||
predicates.add(builder.greaterThanOrEqualTo(path, (Comparable<Object>) value(path, value)));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.lessEqual())) {
|
||||
queryable.lessEqual().forEach((column, value) -> {
|
||||
var path = this.<Comparable<Object>>column(root, column);
|
||||
checkComparable(path, value, column);
|
||||
predicates.add(builder.lessThanOrEqualTo(path, (Comparable<Object>) value(path, value)));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.inside())) {
|
||||
queryable.inside()
|
||||
.entrySet()
|
||||
.stream()
|
||||
.filter(entry -> ObjectHelper.isNotEmpty(entry.getValue()))
|
||||
.forEach(entry -> predicates.add(builder.in(column(root, entry.getKey())).value(entry.getValue())));
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.notInside())) {
|
||||
queryable.notInside()
|
||||
.entrySet()
|
||||
.stream()
|
||||
.filter(entry -> ObjectHelper.isNotEmpty(entry.getValue()))
|
||||
.forEach(entry -> predicates.add(builder.in(column(root, entry.getKey())).value(entry.getValue()).not()));
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.between())) {
|
||||
queryable.between().forEach((column, value) -> {
|
||||
var path = this.<Comparable<Object>>column(root, column);
|
||||
checkComparable(path, value, column);
|
||||
predicates.add(builder.between(path, (Comparable<Object>) value(path, value.start()), (Comparable<Object>) value(path, value.end())));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.notBetween())) {
|
||||
queryable.notBetween().forEach((column, value) -> {
|
||||
var path = this.<Comparable<Object>>column(root, column);
|
||||
checkComparable(path, value, column);
|
||||
predicates.add(builder.between(path, (Comparable<Object>) value(path, value.start()), (Comparable<Object>) value(path, value.end())).not());
|
||||
});
|
||||
}
|
||||
|
||||
return predicates.size() == 1
|
||||
? predicates.get(0)
|
||||
: builder.and(predicates.toArray(Predicate[]::new));
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查字段类型是否可比较
|
||||
*
|
||||
* @param path 字段路径
|
||||
* @param value 比较值
|
||||
* @param column 字段名称
|
||||
* @throws NotComparableException 当字段类型不可比较时抛出
|
||||
*/
|
||||
private void checkComparable(Path<?> path, Object value, String column) {
|
||||
if (!ObjectHelper.isComparable(path.getJavaType()) || !ObjectHelper.isComparable(value)) {
|
||||
throw new NotComparableException(column);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查区间值是否可比较
|
||||
*
|
||||
* @param path 字段路径
|
||||
* @param value 区间对象
|
||||
* @param column 字段名称
|
||||
* @throws NotComparableException 当区间值不可比较时抛出
|
||||
*/
|
||||
private void checkComparable(Path<?> path, Query.Queryable.Between value, String column) {
|
||||
checkComparable(path, value.start(), column);
|
||||
checkComparable(path, value.end(), column);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查字段类型是否为集合
|
||||
*
|
||||
* @param path 字段路径
|
||||
* @param column 字段名称
|
||||
* @throws NotCollectionException 当字段类型不是集合时抛出
|
||||
*/
|
||||
private void checkCollection(Path<?> path, String column) {
|
||||
if (!ObjectHelper.isCollection(path.getJavaType())) {
|
||||
throw new NotCollectionException(column);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查值是否为集合
|
||||
*
|
||||
* @param value 值对象
|
||||
* @param column 字段名称
|
||||
* @throws NotCollectionException 当值不是集合时抛出
|
||||
*/
|
||||
private void checkCollection(Object value, String column) {
|
||||
if (!ObjectHelper.isCollection(value)) {
|
||||
throw new NotCollectionException(column);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查字段类型是否为字符串
|
||||
*
|
||||
* @param path 字段路径
|
||||
* @param value 比较值
|
||||
* @param column 字段名称
|
||||
* @throws NotStringException 当字段类型不是字符串时抛出
|
||||
*/
|
||||
private void checkString(Path<?> path, Object value, String column) {
|
||||
if (!ObjectHelper.isString(path.getJavaType()) || !ObjectHelper.isString(value)) {
|
||||
throw new NotStringException(column);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建列表查询条件
|
||||
* <p>
|
||||
* 子类可以重写此方法以添加特定的查询条件。
|
||||
* 默认返回null,表示不添加额外条件。
|
||||
* </p>
|
||||
*
|
||||
* @param root JPA Criteria查询根节点
|
||||
* @param query JPA Criteria查询对象
|
||||
* @param builder JPA Criteria构建器
|
||||
* @return 返回查询条件谓词
|
||||
*/
|
||||
protected Predicate listPredicate(Root<ENTITY> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据查询条件分页获取实体列表
|
||||
* <p>
|
||||
* 支持复杂的查询条件和分页功能。
|
||||
* 默认分页参数:第1页,每页10条记录,按创建时间降序排列。
|
||||
* </p>
|
||||
*
|
||||
* @param listQuery 查询条件对象
|
||||
* @return 返回分页查询结果
|
||||
*/
|
||||
@Override
|
||||
public Page<ENTITY> list(Query listQuery) {
|
||||
var pageRequest = PageRequest.of(DEFAULT_PAGE_INDEX - 1, DEFAULT_PAGE_SIZE, Sort.by(SimpleEntity.Fields.createdTime).descending());
|
||||
if (ObjectHelper.isNotNull(listQuery.page())) {
|
||||
var index = Math.max(ObjectHelper.defaultIfNull(listQuery.page().index(), DEFAULT_PAGE_INDEX) - 1, 0);
|
||||
var size = Math.max(ObjectHelper.defaultIfNull(listQuery.page().size(), DEFAULT_PAGE_SIZE), 1);
|
||||
if (ObjectHelper.isNotEmpty(listQuery.sort())) {
|
||||
pageRequest = PageRequest.of(index, size, Sort.by(
|
||||
listQuery.sort()
|
||||
.stream()
|
||||
.map(sort -> new Sort.Order(Sort.Direction.fromString(sort.direction().name()), sort.column()))
|
||||
.toList()
|
||||
));
|
||||
} else {
|
||||
pageRequest = PageRequest.of(index, size, Sort.by(SimpleEntity.Fields.createdTime).descending());
|
||||
}
|
||||
}
|
||||
var result = repository.findAll(
|
||||
(root, query, builder) -> {
|
||||
var predicate = listPredicate(root, query, builder);
|
||||
var queryPredicate = queryPredicates(listQuery.query(), root, query, builder);
|
||||
return ObjectHelper.isNull(predicate)
|
||||
? queryPredicate
|
||||
: builder.and(predicate, queryPredicate);
|
||||
},
|
||||
pageRequest
|
||||
);
|
||||
return new Page<>(result.get(), result.getTotalElements());
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取实体详情(Optional包装)
|
||||
* <p>
|
||||
* 如果ID为空则返回空Optional,否则根据ID查询实体。
|
||||
* </p>
|
||||
*
|
||||
* @param id 实体ID
|
||||
* @return 返回实体详情的Optional包装
|
||||
*/
|
||||
private Optional<ENTITY> detailOptional(Long id) {
|
||||
if (ObjectHelper.isNull(id)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return repository.findOne(
|
||||
(root, query, builder) -> {
|
||||
var predicate = listPredicate(root, query, builder);
|
||||
var idPredicate = builder.equal(root.get(IdOnlyEntity.Fields.id), id);
|
||||
return ObjectHelper.isNull(predicate)
|
||||
? idPredicate
|
||||
: builder.and(predicate, idPredicate);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取实体详情
|
||||
* <p>
|
||||
* 如果实体不存在则返回null。
|
||||
* </p>
|
||||
*
|
||||
* @param id 实体ID
|
||||
* @return 返回实体详情,不存在时返回null
|
||||
*/
|
||||
@Named("detail")
|
||||
@Override
|
||||
public ENTITY detail(Long id) {
|
||||
return detailOptional(id).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取实体详情,不存在时抛出异常
|
||||
* <p>
|
||||
* 如果实体不存在则抛出IdNotFoundException异常。
|
||||
* </p>
|
||||
*
|
||||
* @param id 实体ID
|
||||
* @return 返回实体详情
|
||||
* @throws IdNotFoundException 当实体不存在时抛出
|
||||
*/
|
||||
@Named("detailOrThrow")
|
||||
@Override
|
||||
public ENTITY detailOrThrow(Long id) {
|
||||
return detailOptional(id).orElseThrow(() -> new IdNotFoundException(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID删除实体
|
||||
* <p>
|
||||
* 具有事务性,遇到任何异常都会回滚。
|
||||
* 如果ID为空则不执行任何操作。
|
||||
* </p>
|
||||
*
|
||||
* @param id 实体主键ID
|
||||
*/
|
||||
@Transactional(rollbackOn = Throwable.class)
|
||||
@Override
|
||||
public void remove(Long id) {
|
||||
if (ObjectHelper.isNotNull(id)) {
|
||||
repository.deleteById(id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID集合批量删除实体
|
||||
* <p>
|
||||
* 使用deleteAllById方法根据ID集合批量删除实体。
|
||||
* 该方法具有事务性,遇到任何异常都会回滚。
|
||||
* 如果ID集合为空则不执行任何操作。
|
||||
* </p>
|
||||
*
|
||||
* @param ids 实体主键ID集合
|
||||
*/
|
||||
@Transactional(rollbackOn = Throwable.class)
|
||||
@Override
|
||||
public void remove(Iterable<Long> ids) {
|
||||
if (ObjectHelper.isNotEmpty(ids)) {
|
||||
repository.deleteBatchByIds(ids);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ID未找到异常
|
||||
* <p>
|
||||
* 当根据ID查询实体但实体不存在时抛出此异常。
|
||||
* </p>
|
||||
*/
|
||||
public static final class IdNotFoundException extends RuntimeException {
|
||||
/**
|
||||
* 构造函数(带ID参数)
|
||||
*
|
||||
* @param id 实体ID
|
||||
*/
|
||||
public IdNotFoundException(Long id) {
|
||||
super("ID为 %d 的资源不存在".formatted(id));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 不可比较异常
|
||||
* <p>
|
||||
* 当尝试对不可比较的字段或值执行比较操作时抛出此异常。
|
||||
* </p>
|
||||
*/
|
||||
public static final class NotComparableException extends RuntimeException {
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param variable 变量名称
|
||||
*/
|
||||
public NotComparableException(String variable) {
|
||||
super("变量 %s 不能比较".formatted(variable));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 非集合异常
|
||||
* <p>
|
||||
* 当尝试对非集合类型的字段或值执行集合操作时抛出此异常。
|
||||
* </p>
|
||||
*/
|
||||
public static final class NotCollectionException extends RuntimeException {
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param variable 变量名称
|
||||
*/
|
||||
public NotCollectionException(String variable) {
|
||||
super("变量 %s 不是集合".formatted(variable));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 非字符串异常
|
||||
* <p>
|
||||
* 当尝试对非字符串类型的字段或值执行字符串操作时抛出此异常。
|
||||
* </p>
|
||||
*/
|
||||
public static final class NotStringException extends RuntimeException {
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param variable 变量名称
|
||||
*/
|
||||
public NotStringException(String variable) {
|
||||
super("变量 %s 不是字符串".formatted(variable));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.jpa.helper.DatabaseHelper;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Helper测试类
|
||||
* 用于测试驼峰命名法转下划线命名法的功能
|
||||
*/
|
||||
@Slf4j
|
||||
public class HelperTest {
|
||||
|
||||
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
||||
// 通过反射调用Helper类中的private静态方法camelConvert
|
||||
var camelConvert = DatabaseHelper.class.getDeclaredMethod("camelConvert", String.class);
|
||||
camelConvert.setAccessible(true);
|
||||
|
||||
// 测试用例集合
|
||||
Map<String, String> testCases = new HashMap<>();
|
||||
|
||||
// 基本转换测试
|
||||
testCases.put("helloWorld", "hello_world");
|
||||
testCases.put("firstName", "first_name");
|
||||
testCases.put("lastName", "last_name");
|
||||
testCases.put("URL", "url");
|
||||
testCases.put("HTTPResponse", "http_response");
|
||||
testCases.put("XMLParser", "xml_parser");
|
||||
|
||||
// 边界情况测试
|
||||
testCases.put(null, null); // null输入
|
||||
testCases.put("", ""); // 空字符串
|
||||
testCases.put("a", "a"); // 单个小写字母
|
||||
testCases.put("A", "a"); // 单个大写字母
|
||||
testCases.put("aB", "a_b"); // 两个字符
|
||||
testCases.put("Ab", "ab"); // 首字母大写
|
||||
|
||||
// 数字相关测试
|
||||
testCases.put("field1Name", "field1_name");
|
||||
testCases.put("field12Name", "field12_name");
|
||||
testCases.put("2FARequired", "2_fa_required");
|
||||
testCases.put("ID", "id");
|
||||
testCases.put("userID", "user_id");
|
||||
testCases.put("HTML5Parser", "html5_parser");
|
||||
|
||||
// 连续大写字母测试
|
||||
testCases.put("HTTPSConnection", "https_connection");
|
||||
testCases.put("XMLHttpRequest", "xml_http_request");
|
||||
testCases.put("URLPath", "url_path");
|
||||
testCases.put("APIKey", "api_key");
|
||||
testCases.put("JWTToken", "jwt_token");
|
||||
|
||||
// 特殊场景测试
|
||||
testCases.put("iPhone", "i_phone"); // 以小写字母开头,后面有大写
|
||||
testCases.put("iOSVersion", "i_os_version"); // 连续小写字母后跟大写
|
||||
testCases.put("CAPTCHA", "captcha"); // 全大写字母缩写
|
||||
|
||||
log.info("开始执行驼峰命名转下划线命名测试...");
|
||||
|
||||
int passedTests = 0;
|
||||
int totalTests = testCases.size();
|
||||
|
||||
for (Map.Entry<String, String> testCase : testCases.entrySet()) {
|
||||
String input = testCase.getKey();
|
||||
String expected = testCase.getValue();
|
||||
String actual = (String) camelConvert.invoke(null, input);
|
||||
|
||||
try {
|
||||
Assert.isTrue(Objects.equals(expected, actual), "测试失败: 输入='%s', 期望='%s', 实际='%s'".formatted(input, expected, actual));
|
||||
passedTests++;
|
||||
log.info("✓ 测试通过: '{}' -> '{}'", input, actual);
|
||||
} catch (Exception e) {
|
||||
log.error("✗ {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
log.info("测试结果: {}/{} 通过", passedTests, totalTests);
|
||||
if (passedTests == totalTests) {
|
||||
log.info("所有测试通过!✓");
|
||||
} else {
|
||||
log.error("有测试失败!✗");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,302 +0,0 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa;
|
||||
|
||||
import com.blinkfox.fenix.EnableFenix;
|
||||
import com.lanyuanxiaoyao.service.template.jpa.entity.Company;
|
||||
import com.lanyuanxiaoyao.service.template.jpa.entity.Company_;
|
||||
import com.lanyuanxiaoyao.service.template.jpa.entity.Employee;
|
||||
import com.lanyuanxiaoyao.service.template.jpa.entity.Employee_;
|
||||
import com.lanyuanxiaoyao.service.template.jpa.entity.QEmployee;
|
||||
import com.lanyuanxiaoyao.service.template.jpa.entity.Report;
|
||||
import com.lanyuanxiaoyao.service.template.jpa.entity.Report_;
|
||||
import com.lanyuanxiaoyao.service.template.jpa.repository.EmployeeRepository;
|
||||
import jakarta.annotation.Resource;
|
||||
import java.util.List;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import tools.jackson.databind.JsonNode;
|
||||
import tools.jackson.databind.ObjectMapper;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableFenix
|
||||
@EnableJpaAuditing
|
||||
public class TestApplication {
|
||||
private static final Logger log = LoggerFactory.getLogger(TestApplication.class);
|
||||
private static final String BASE_URL = "http://localhost:2490";
|
||||
private static final RestTemplate REST_CLIENT = new RestTemplate();
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
@Resource
|
||||
private EmployeeRepository employeeRepository;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestApplication.class, args);
|
||||
}
|
||||
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void runTests() {
|
||||
// 增
|
||||
var cid1 = saveItem("company", "{\"name\": \"Apple\",\"members\": 10}").get("data").asLong();
|
||||
var cid2 = saveItem("company", "{\"name\": \"Banana\",\"members\": 20}").get("data").asLong();
|
||||
var cid3 = saveItem("company", "{\"name\": \"Cheery\",\"members\": 20}").get("data").asLong();
|
||||
|
||||
// 查
|
||||
var companies = listItems("company");
|
||||
Assert.isTrue(companies.at("/data/items").size() == 3, "数量错误");
|
||||
Assert.isTrue(companies.at("/data/total").asLong() == 3, "返回数量错误");
|
||||
|
||||
// language=JSON
|
||||
var companies2 = listItems("company", "{\n" +
|
||||
" \"page\": {\n" +
|
||||
" \"index\": 1,\n" +
|
||||
" \"size\": 2\n" +
|
||||
" }\n" +
|
||||
"}");
|
||||
Assert.isTrue(companies2.at("/data/items").size() == 2, "数量错误");
|
||||
Assert.isTrue(companies2.at("/data/total").asLong() == 3, "返回数量错误");
|
||||
// language=JSON
|
||||
var companies3 = listItems("company", "{\n" +
|
||||
" \"query\": {\n" +
|
||||
" \"notNullEqual\": [\n" +
|
||||
" \"name\"\n" +
|
||||
" ],\n" +
|
||||
" \"equal\": {\n" +
|
||||
" \"name\": \"Apple\"\n" +
|
||||
" },\n" +
|
||||
" \"like\": {\n" +
|
||||
" \"name\": \"Appl%\"\n" +
|
||||
" },\n" +
|
||||
" \"contain\": {\n" +
|
||||
" \"name\": \"ple\"\n" +
|
||||
" },\n" +
|
||||
" \"startWith\": {\n" +
|
||||
" \"name\": \"Appl\"\n" +
|
||||
" },\n" +
|
||||
" \"endWith\": {\n" +
|
||||
" \"name\": \"le\"\n" +
|
||||
" },\n" +
|
||||
" \"less\": {\n" +
|
||||
" \"members\": 50\n" +
|
||||
" },\n" +
|
||||
" \"greatEqual\": {\n" +
|
||||
" \"members\": 0,\n" +
|
||||
" \"createdTime\": \"2025-01-01 00:00:00\"\n" +
|
||||
" },\n" +
|
||||
" \"in\": {\n" +
|
||||
" \"name\": [\n" +
|
||||
" \"Apple\",\n" +
|
||||
" \"Banana\"\n" +
|
||||
" ]\n" +
|
||||
" },\n" +
|
||||
" \"between\": {\n" +
|
||||
" \"members\": {\n" +
|
||||
" \"start\": 0,\n" +
|
||||
" \"end\": 50\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" },\n" +
|
||||
" \"page\": {\n" +
|
||||
" \"index\": 1,\n" +
|
||||
" \"size\": 2\n" +
|
||||
" }\n" +
|
||||
"}");
|
||||
Assert.isTrue(companies3.at("/data/items").size() == 1, "数量错误");
|
||||
Assert.isTrue(companies3.at("/data/total").asLong() == 1, "返回数量错误");
|
||||
|
||||
var company1 = detailItem("company", cid1);
|
||||
Assert.isTrue(cid1 == company1.at("/data/id").asLong(), "id错误");
|
||||
Assert.isTrue("Apple".equals(company1.at("/data/name").asText()), "name错误");
|
||||
|
||||
// 改
|
||||
var cid4 = saveItem("company", "{\"id\": %d, \"name\": \"Dog\"}".formatted(cid2)).get("data").asLong();
|
||||
Assert.isTrue(cid2 == cid4, "id错误");
|
||||
var company2 = detailItem("company", cid2);
|
||||
Assert.isTrue("Dog".equals(company2.at("/data/name").asText()), "name错误");
|
||||
|
||||
// 删
|
||||
removeItem("company", cid3);
|
||||
Assert.isTrue(listItems("company").at("/data/items").size() == 2, "数量错误");
|
||||
Assert.isTrue(listItems("company").at("/data/total").asLong() == 2, "返回数量错误");
|
||||
|
||||
log.info(listItems("company").toPrettyString());
|
||||
|
||||
var eid1 = saveItem("employee", "{\"name\": \"Tom\",\"age\": 18, \"companyId\": %d}".formatted(cid1)).get("data").asLong();
|
||||
var eid2 = saveItem("employee", "{\"name\": \"Jerry\",\"age\": 18, \"companyId\": %d}".formatted(cid1)).get("data").asLong();
|
||||
var eid3 = saveItem("employee", "{\"name\": \"Mike\",\"age\": 18, \"companyId\": %d}".formatted(cid2)).get("data").asLong();
|
||||
|
||||
var employees = listItems("employee");
|
||||
Assert.isTrue(employees.at("/data/items").size() == 3, "数量错误");
|
||||
Assert.isTrue(employees.at("/data/total").asLong() == 3, "返回数量错误");
|
||||
|
||||
var employee1 = detailItem("employee", eid1);
|
||||
Assert.isTrue(eid1 == employee1.at("/data/id").asLong(), "id错误");
|
||||
Assert.isTrue("Tom".equals(employee1.at("/data/name").asText()), "name错误");
|
||||
Assert.isTrue(18 == employee1.at("/data/age").asInt(), "age错误");
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void runSpecificationTests() {
|
||||
// 增
|
||||
var cid1 = saveItem("company", "{\"name\": \"Apple\",\"members\": 10}").get("data").asLong();
|
||||
var cid2 = saveItem("company", "{\"name\": \"Banana\",\"members\": 20}").get("data").asLong();
|
||||
var cid3 = saveItem("company", "{\"name\": \"Cheery\",\"members\": 20}").get("data").asLong();
|
||||
var eid1 = saveItem("employee", "{\"name\": \"Tom\",\"age\": 18, \"companyId\": %d}".formatted(cid1)).get("data").asLong();
|
||||
var eid2 = saveItem("employee", "{\"name\": \"Jerry\",\"age\": 18, \"companyId\": %d}".formatted(cid1)).get("data").asLong();
|
||||
var eid3 = saveItem("employee", "{\"name\": \"Mike\",\"age\": 18, \"companyId\": %d}".formatted(cid2)).get("data").asLong();
|
||||
var rid = saveItem("report", "{\"employeeId\": %d, \"score\": 56.38, \"level\": \"A\"}".formatted(eid1)).get("data").asLong();
|
||||
var rid2 = saveItem("report", "{\"employeeId\": %d, \"score\": 78.98, \"level\": \"B\"}".formatted(eid2)).get("data").asLong();
|
||||
|
||||
log.debug(
|
||||
"Results: {}",
|
||||
employeeRepository.findAll(
|
||||
builder -> builder
|
||||
.andIsNotNull(Employee.Fields.name)
|
||||
.andEquals(Employee.Fields.name, "Tom")
|
||||
.andLike(Employee.Fields.name, "To%")
|
||||
.andStartsWith(Employee.Fields.name, "To")
|
||||
.andEndsWith(Employee.Fields.name, "om")
|
||||
.andLessThan(Employee.Fields.age, 50)
|
||||
.andGreaterThanEqual(Employee.Fields.age, 0)
|
||||
.andIn(Employee.Fields.name, List.of("Tom", "Mike"))
|
||||
.andBetween(Employee.Fields.age, 0, 50)
|
||||
.build()
|
||||
)
|
||||
);
|
||||
|
||||
log.debug(
|
||||
"Results: {}",
|
||||
employeeRepository.findAll(
|
||||
(root, query, builder) ->
|
||||
builder.and(
|
||||
builder.isNotNull(root.get(Employee_.name)),
|
||||
builder.equal(root.get(Employee_.name), "Tom"),
|
||||
builder.like(root.get(Employee_.name), "To%"),
|
||||
builder.lessThan(root.get(Employee_.age), 50),
|
||||
builder.greaterThanOrEqualTo(root.get(Employee_.age), 0),
|
||||
builder.in(root.get(Employee_.NAME)).value(List.of("Tom", "Mike")),
|
||||
builder.between(root.get(Employee_.age), 0, 50),
|
||||
builder.isNotEmpty(root.get(Employee_.company).get(Company_.employees)),
|
||||
builder.isMember(Company.Industry.MEDIA, root.get(Employee_.company).get(Company_.industries))
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
log.debug(
|
||||
"Results: {}",
|
||||
employeeRepository.findAll(
|
||||
QEmployee.employee.name.isNotNull()
|
||||
.and(QEmployee.employee.name.eq("Tom"))
|
||||
.and(QEmployee.employee.name.like("To%"))
|
||||
.and(QEmployee.employee.name.startsWith("To"))
|
||||
.and(QEmployee.employee.name.endsWith("om"))
|
||||
.and(QEmployee.employee.age.lt(50))
|
||||
.and(QEmployee.employee.age.goe(0))
|
||||
.and(QEmployee.employee.name.in("Tom", "Mike"))
|
||||
.and(QEmployee.employee.age.between(0, 50))
|
||||
.and(QEmployee.employee.company().employees.isNotEmpty())
|
||||
.and(QEmployee.employee.company().employees.any().name.ne("Tom"))
|
||||
.and(QEmployee.employee.company().industries.contains(Company.Industry.MEDIA))
|
||||
.and(QEmployee.employee.connections.containsKey(Employee.ConnectionType.EMAIL))
|
||||
)
|
||||
);
|
||||
|
||||
log.debug(
|
||||
"Results: {}",
|
||||
employeeRepository.findAll(
|
||||
(root, query, builder) -> {
|
||||
var reportRoot = query.from(Report.class);
|
||||
return builder.and(
|
||||
builder.equal(root.get(Employee_.id), reportRoot.get(Report_.employeeId)),
|
||||
builder.equal(reportRoot.get(Report_.level), Report.Level.A)
|
||||
);
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void runNativeQueryTests() {
|
||||
// 增
|
||||
var cid1 = saveItem("company", "{\"name\": \"Apple\",\"members\": 10}").get("data").asLong();
|
||||
var cid2 = saveItem("company", "{\"name\": \"Banana\",\"members\": 20}").get("data").asLong();
|
||||
var cid3 = saveItem("company", "{\"name\": \"Cheery\",\"members\": 20}").get("data").asLong();
|
||||
var eid1 = saveItem("employee", "{\"name\": \"Tom\",\"age\": 18, \"companyId\": %d}".formatted(cid1)).get("data").asLong();
|
||||
var eid2 = saveItem("employee", "{\"name\": \"Jerry\",\"age\": 18, \"companyId\": %d}".formatted(cid1)).get("data").asLong();
|
||||
var eid3 = saveItem("employee", "{\"name\": \"Mike\",\"age\": 18, \"companyId\": %d}".formatted(cid2)).get("data").asLong();
|
||||
|
||||
var list = employeeRepository.findAllEmployeeWithCompanyName();
|
||||
Assert.isTrue(list.size() == 3, "数量错误");
|
||||
log.debug("Results: {}", list);
|
||||
var list_native = employeeRepository.findAllEmployeeWithCompanyNameNative();
|
||||
Assert.isTrue(list_native.size() == 3, "数量错误");
|
||||
log.debug("Results: {}", list_native);
|
||||
}
|
||||
|
||||
private HttpHeaders headers() {
|
||||
var headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
return headers;
|
||||
}
|
||||
|
||||
private JsonNode saveItem(String path, String body) {
|
||||
var response = REST_CLIENT.postForEntity(
|
||||
"%s/%s/save".formatted(BASE_URL, path),
|
||||
new HttpEntity<>(body, headers()),
|
||||
String.class
|
||||
);
|
||||
Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "请求失败");
|
||||
Assert.notNull(response.getBody(), "请求失败");
|
||||
return MAPPER.readTree(response.getBody());
|
||||
}
|
||||
|
||||
private JsonNode listItems(String path) {
|
||||
var response = REST_CLIENT.getForEntity(
|
||||
"%s/%s/list".formatted(BASE_URL, path),
|
||||
String.class
|
||||
);
|
||||
Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "请求失败");
|
||||
Assert.notNull(response.getBody(), "请求失败");
|
||||
return MAPPER.readTree(response.getBody());
|
||||
}
|
||||
|
||||
private JsonNode listItems(String path, String query) {
|
||||
var response = REST_CLIENT.postForEntity(
|
||||
"%s/%s/list".formatted(BASE_URL, path),
|
||||
new HttpEntity<>(query, headers()),
|
||||
String.class
|
||||
);
|
||||
Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "请求失败");
|
||||
Assert.notNull(response.getBody(), "请求失败");
|
||||
return MAPPER.readTree(response.getBody());
|
||||
}
|
||||
|
||||
private JsonNode detailItem(String path, Long id) {
|
||||
var response = REST_CLIENT.getForEntity(
|
||||
"%s/%s/detail/%d".formatted(BASE_URL, path, id),
|
||||
String.class
|
||||
);
|
||||
Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "请求失败");
|
||||
Assert.notNull(response.getBody(), "请求失败");
|
||||
return MAPPER.readTree(response.getBody());
|
||||
}
|
||||
|
||||
private void removeItem(String path, Long id) {
|
||||
var response = REST_CLIENT.getForEntity(
|
||||
"%s/%s/remove/%d".formatted(BASE_URL, path, id),
|
||||
Void.class
|
||||
);
|
||||
Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "请求失败");
|
||||
}
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa.controller;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.jpa.entity.Company;
|
||||
import com.lanyuanxiaoyao.service.template.jpa.service.CompanyService;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.function.Function;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("company")
|
||||
public class CompanyController extends SimpleControllerSupport<Company, CompanyController.SaveItem, CompanyController.ListItem, CompanyController.DetailItem> {
|
||||
public CompanyController(CompanyService service) {
|
||||
super(service);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<SaveItem, Company> saveItemMapper() {
|
||||
return item -> {
|
||||
var company = new Company();
|
||||
company.setId(item.id());
|
||||
company.setName(item.name());
|
||||
company.setMembers(item.members());
|
||||
return company;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<Company, ListItem> listItemMapper() {
|
||||
return company -> new ListItem(
|
||||
company.getId(),
|
||||
company.getName(),
|
||||
company.getMembers()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<Company, DetailItem> detailItemMapper() {
|
||||
return company -> new DetailItem(
|
||||
company.getId(),
|
||||
company.getName(),
|
||||
company.getMembers(),
|
||||
company.getCreatedTime(),
|
||||
company.getModifiedTime()
|
||||
);
|
||||
}
|
||||
|
||||
public record SaveItem(
|
||||
Long id,
|
||||
String name,
|
||||
Integer members
|
||||
) {
|
||||
}
|
||||
|
||||
public record ListItem(
|
||||
Long id,
|
||||
String name,
|
||||
Integer members
|
||||
) {
|
||||
}
|
||||
|
||||
public record DetailItem(
|
||||
Long id,
|
||||
String name,
|
||||
Integer members,
|
||||
LocalDateTime createdTime,
|
||||
LocalDateTime modifiedTime
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa.controller;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.jpa.entity.Employee;
|
||||
import com.lanyuanxiaoyao.service.template.jpa.service.CompanyService;
|
||||
import com.lanyuanxiaoyao.service.template.jpa.service.EmployeeService;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.function.Function;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("employee")
|
||||
public class EmployeeController extends SimpleControllerSupport<Employee, EmployeeController.SaveItem, EmployeeController.ListItem, EmployeeController.DetailItem> {
|
||||
private final CompanyService companyService;
|
||||
|
||||
public EmployeeController(EmployeeService service, CompanyService companyService) {
|
||||
super(service);
|
||||
this.companyService = companyService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<SaveItem, Employee> saveItemMapper() {
|
||||
return item -> {
|
||||
var employee = new Employee();
|
||||
employee.setId(item.id());
|
||||
employee.setName(item.name());
|
||||
employee.setAge(item.age());
|
||||
employee.setRole(Employee.Role.USER);
|
||||
employee.setCompany(companyService.detailOrThrow(item.companyId()));
|
||||
return employee;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<Employee, ListItem> listItemMapper() {
|
||||
return employee -> new ListItem(
|
||||
employee.getId(),
|
||||
employee.getName(),
|
||||
employee.getAge(),
|
||||
employee.getRole()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<Employee, DetailItem> detailItemMapper() {
|
||||
return employee -> new DetailItem(
|
||||
employee.getId(),
|
||||
employee.getCompany().getId(),
|
||||
employee.getName(),
|
||||
employee.getAge(),
|
||||
employee.getRole(),
|
||||
employee.getCreatedTime(),
|
||||
employee.getModifiedTime()
|
||||
);
|
||||
}
|
||||
|
||||
public record SaveItem(
|
||||
Long id,
|
||||
Long companyId,
|
||||
String name,
|
||||
Integer age,
|
||||
Employee.Role role
|
||||
) {
|
||||
}
|
||||
|
||||
public record ListItem(
|
||||
Long id,
|
||||
String name,
|
||||
Integer age,
|
||||
Employee.Role role
|
||||
) {
|
||||
}
|
||||
|
||||
public record DetailItem(
|
||||
Long id,
|
||||
Long companyId,
|
||||
String name,
|
||||
Integer age,
|
||||
Employee.Role role,
|
||||
LocalDateTime createdTime,
|
||||
LocalDateTime modifiedTime
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa.controller;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.jpa.entity.Report;
|
||||
import com.lanyuanxiaoyao.service.template.jpa.service.EmployeeService;
|
||||
import com.lanyuanxiaoyao.service.template.jpa.service.ReportService;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.function.Function;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("report")
|
||||
public class ReportController extends SimpleControllerSupport<Report, ReportController.SaveItem, ReportController.ListItem, ReportController.DetailItem> {
|
||||
private final EmployeeService employeeService;
|
||||
|
||||
public ReportController(ReportService service, EmployeeService employeeService) {
|
||||
super(service);
|
||||
this.employeeService = employeeService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<SaveItem, Report> saveItemMapper() {
|
||||
return item -> {
|
||||
var report = new Report();
|
||||
report.setId(item.id());
|
||||
report.setScore(item.score());
|
||||
report.setLevel(item.level());
|
||||
report.setEmployeeId(item.employeeId());
|
||||
return report;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<Report, ListItem> listItemMapper() {
|
||||
return report -> {
|
||||
var employee = employeeService.detailOrThrow(report.getEmployeeId());
|
||||
return new ListItem(
|
||||
report.getId(),
|
||||
employee.getId(),
|
||||
employee.getName(),
|
||||
report.getScore(),
|
||||
report.getLevel()
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<Report, DetailItem> detailItemMapper() {
|
||||
return report -> {
|
||||
var employee = employeeService.detailOrThrow(report.getEmployeeId());
|
||||
return new DetailItem(
|
||||
report.getId(),
|
||||
employee.getId(),
|
||||
employee.getName(),
|
||||
report.getScore(),
|
||||
report.getLevel(),
|
||||
report.getCreatedTime(),
|
||||
report.getModifiedTime()
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
public record SaveItem(
|
||||
Long id,
|
||||
Double score,
|
||||
Report.Level level,
|
||||
Long employeeId
|
||||
) {
|
||||
}
|
||||
|
||||
public record ListItem(
|
||||
Long id,
|
||||
Long employeeId,
|
||||
String employeeName,
|
||||
Double score,
|
||||
Report.Level level
|
||||
) {
|
||||
}
|
||||
|
||||
public record DetailItem(
|
||||
Long id,
|
||||
Long employeeId,
|
||||
String employeeName,
|
||||
Double score,
|
||||
Report.Level level,
|
||||
LocalDateTime createdTime,
|
||||
LocalDateTime modifiedTime
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa.entity;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.ConstraintMode;
|
||||
import jakarta.persistence.ElementCollection;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.EntityListeners;
|
||||
import jakarta.persistence.EnumType;
|
||||
import jakarta.persistence.Enumerated;
|
||||
import jakarta.persistence.ForeignKey;
|
||||
import jakarta.persistence.JoinTable;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.Table;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.FieldNameConstants;
|
||||
import org.hibernate.annotations.DynamicInsert;
|
||||
import org.hibernate.annotations.DynamicUpdate;
|
||||
import org.hibernate.annotations.SoftDelete;
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString(callSuper = true)
|
||||
@FieldNameConstants
|
||||
@Entity
|
||||
@SoftDelete
|
||||
@DynamicUpdate
|
||||
@DynamicInsert
|
||||
@EntityListeners(AuditingEntityListener.class)
|
||||
@Table(comment = "企业")
|
||||
public class Company extends SimpleEntity {
|
||||
@Column(nullable = false, comment = "名称")
|
||||
private String name;
|
||||
@Column(nullable = false, comment = "成员数")
|
||||
private Integer members;
|
||||
|
||||
@OneToMany(mappedBy = "company")
|
||||
@ToString.Exclude
|
||||
private Set<Employee> employees;
|
||||
|
||||
@ElementCollection
|
||||
@JoinTable(foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT), inverseForeignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(nullable = false)
|
||||
private Set<Industry> industries = new HashSet<>();
|
||||
|
||||
public enum Industry {
|
||||
TECHNOLOGY,
|
||||
FINANCE,
|
||||
MEDIA,
|
||||
SERVICE,
|
||||
GOVERNMENT,
|
||||
EDUCATION,
|
||||
HEALTHCARE,
|
||||
CONSTRUCTION,
|
||||
RETAIL,
|
||||
OTHER,
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa.entity;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.ConstraintMode;
|
||||
import jakarta.persistence.ElementCollection;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.EntityListeners;
|
||||
import jakarta.persistence.EnumType;
|
||||
import jakarta.persistence.Enumerated;
|
||||
import jakarta.persistence.ForeignKey;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.JoinTable;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.MapKeyEnumerated;
|
||||
import jakarta.persistence.Table;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.FieldNameConstants;
|
||||
import org.hibernate.annotations.DynamicInsert;
|
||||
import org.hibernate.annotations.DynamicUpdate;
|
||||
import org.hibernate.annotations.SoftDelete;
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString(callSuper = true)
|
||||
@FieldNameConstants
|
||||
@Entity
|
||||
@SoftDelete
|
||||
@DynamicUpdate
|
||||
@DynamicInsert
|
||||
@EntityListeners(AuditingEntityListener.class)
|
||||
@Table(comment = "员工")
|
||||
public class Employee extends SimpleEntity {
|
||||
@Column(nullable = false, comment = "名称")
|
||||
private String name;
|
||||
@Column(nullable = false, comment = "年龄")
|
||||
private Integer age;
|
||||
@Column(nullable = false, comment = "角色")
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Role role;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(nullable = false, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
|
||||
@ToString.Exclude
|
||||
private Company company;
|
||||
|
||||
@ElementCollection
|
||||
@JoinTable(foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT), inverseForeignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
|
||||
@MapKeyEnumerated(EnumType.STRING)
|
||||
@Column(nullable = false)
|
||||
private Map<ConnectionType, String> connections = new HashMap<>();
|
||||
|
||||
public enum Role {
|
||||
USER,
|
||||
ADMIN,
|
||||
}
|
||||
|
||||
public enum ConnectionType {
|
||||
EMAIL,
|
||||
PHONE,
|
||||
ADDRESS,
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa.entity;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.EntityListeners;
|
||||
import jakarta.persistence.EnumType;
|
||||
import jakarta.persistence.Enumerated;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.FieldNameConstants;
|
||||
import org.hibernate.annotations.DynamicInsert;
|
||||
import org.hibernate.annotations.DynamicUpdate;
|
||||
import org.hibernate.annotations.SoftDelete;
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString(callSuper = true)
|
||||
@FieldNameConstants
|
||||
@Entity
|
||||
@SoftDelete
|
||||
@DynamicUpdate
|
||||
@DynamicInsert
|
||||
@EntityListeners(AuditingEntityListener.class)
|
||||
@Table(comment = "报告")
|
||||
public class Report extends SimpleEntity {
|
||||
@Column(nullable = false, comment = "分数")
|
||||
private Double score = 0.0;
|
||||
@Column(nullable = false, comment = "等级")
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Level level;
|
||||
|
||||
@Column(nullable = false, comment = "员工 ID")
|
||||
private Long employeeId;
|
||||
|
||||
public enum Level {
|
||||
A, B, C, D, E
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa.entity.vo;
|
||||
|
||||
public record EmployeeWithCompanyName(
|
||||
String name,
|
||||
String companyName,
|
||||
Integer age,
|
||||
String role
|
||||
) {
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa.repository;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.jpa.entity.Company;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface CompanyRepository extends SimpleRepository<Company> {
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa.repository;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.jpa.entity.Employee;
|
||||
import com.lanyuanxiaoyao.service.template.jpa.entity.vo.EmployeeWithCompanyName;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.data.jpa.repository.EntityGraph;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@SuppressWarnings("NullableProblems")
|
||||
@Repository
|
||||
public interface EmployeeRepository extends SimpleRepository<Employee> {
|
||||
@EntityGraph(attributePaths = {"company"})
|
||||
@Override
|
||||
Optional<Employee> findOne(Specification<Employee> specification);
|
||||
|
||||
@Query(value = "select e.name, c.name, e.age, e.role from employee e, company c where e.company_id = c.id", nativeQuery = true)
|
||||
List<EmployeeWithCompanyName> findAllEmployeeWithCompanyNameNative();
|
||||
|
||||
@Query("select new com.lanyuanxiaoyao.service.template.jpa.entity.vo.EmployeeWithCompanyName(employee.name, employee.company.name, employee.age, cast(employee.role as string)) from Employee employee")
|
||||
List<EmployeeWithCompanyName> findAllEmployeeWithCompanyName();
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa.repository;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.jpa.entity.Report;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface ReportRepository extends SimpleRepository<Report> {
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa.service;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.jpa.entity.Company;
|
||||
import com.lanyuanxiaoyao.service.template.jpa.repository.CompanyRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class CompanyService extends SimpleServiceSupport<Company> {
|
||||
public CompanyService(CompanyRepository repository) {
|
||||
super(repository);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa.service;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.jpa.entity.Employee;
|
||||
import com.lanyuanxiaoyao.service.template.jpa.repository.EmployeeRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class EmployeeService extends SimpleServiceSupport<Employee> {
|
||||
public EmployeeService(EmployeeRepository repository) {
|
||||
super(repository);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.lanyuanxiaoyao.service.template.jpa.service;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.jpa.entity.Report;
|
||||
import com.lanyuanxiaoyao.service.template.jpa.repository.ReportRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class ReportService extends SimpleServiceSupport<Report> {
|
||||
public ReportService(ReportRepository repository) {
|
||||
super(repository);
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
server:
|
||||
port: 2490
|
||||
spring:
|
||||
application:
|
||||
name: Test
|
||||
datasource:
|
||||
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
|
||||
username: test
|
||||
password: test
|
||||
driver-class-name: org.h2.Driver
|
||||
jpa:
|
||||
show-sql: true
|
||||
generate-ddl: true
|
||||
fenix:
|
||||
print-banner: false
|
||||
Reference in New Issue
Block a user