项目简介
easy-query-demo 是一个基于 Spring Boot 的双库双表多数据源查询项目,演示如何使用 Easy Query ORM 框架实现以下功能:
- 同库联合查询 - 在单个数据库内进行表关联查询
- 跨库联合查询 - 跨越两个数据库进行数据关联
- 动态数据源切换 - 使用注解方式动态切换数据源
- 数据库函数适配 - 自动适配不同数据库的 SQL 方言差异
数据库架构
本项目采用双库双表架构,每个数据库都包含完整的用户和产品数据:
| 数据库 | 用户表 | 产品表 | 用途 |
|---|---|---|---|
| PostgreSQL | t_user |
t_product |
主业务库(小写表名) |
| 达梦数据库 | T_USER |
T_PRODUCT |
国产数据库(大写表名) |
核心特性
1. 三种查询模式
| 模式 | 说明 | API 前缀 |
|---|---|---|
| PostgreSQL 同库查询 | 在 PostgreSQL 内关联 t_user 和 t_product |
/api/query/pg/* |
| 达梦同库查询 | 在达梦内关联 T_USER 和 T_PRODUCT |
/api/query/dm/* |
| 跨库联合查询 | PostgreSQL 与达梦之间的数据关联 | /api/query/cross/* |
2. 动态数据源切换
java
// 使用注解方式切换数据源
@DataSource(DataSourceType.POSTGRESQL)
public List<User> queryPostgreSQL() { ... }
@DataSource(DataSourceType.DAMENG)
public List<User> queryDameng() { ... }
3. 数据库函数适配
自动适配不同数据库的 SQL 方言差异:
| 函数 | PostgreSQL | 达梦数据库 |
|---|---|---|
| 字符串连接 | `a | |
| 当前时间 | CURRENT_TIMESTAMP |
CURRENT_TIMESTAMP |
| 日期格式化 | TO_CHAR() |
TO_CHAR() |
项目架构
分层架构
scss
┌─────────────────────────────────────────────────────────────────┐
│ Controller Layer │
│ UnifiedQueryController / FederatedQueryController │
│ (REST API Endpoints) │
└───────────────────────────────┬─────────────────────────────────┘
│
┌───────────────────────────────▼─────────────────────────────────┐
│ Service Layer │
│ SameDatabaseJoinService / FederatedQueryService │
│ (Business Logic, Parallel Query, Data Combination) │
└───────────────────────────────┬─────────────────────────────────┘
│
┌───────────────────────────────▼─────────────────────────────────┐
│ Repository Layer │
│ UserRepository / ProductRepository / PgProductRepository │
│ (Data Access with Easy Query) │
└───────────────────────────────┬─────────────────────────────────┘
│
┌───────────────────────────────▼─────────────────────────────────┐
│ Entity Layer │
│ User / Product / PgUser / PgProduct / DmUser / DmProduct │
│ (Domain Models & Proxy Classes) │
└───────────────────────────────┬─────────────────────────────────┘
│
┌───────────────────────────────▼─────────────────────────────────┐
│ Data Source & Adapter Layer │
│ DynamicDataSource / DataSourceAspect / DatabaseFunctionAdapter │
└───────────────────────────────┬─────────────────────────────────┘
│
┌───────────────────────────┴──────────────────────────┐
│ │
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ PostgreSQL │ │ Dameng DB │
│ :5432 │ │ :5236 │
│ │ │ │
│ t_user + │ │ T_USER + │
│ t_product │ │ T_PRODUCT │
└─────────────────┘ └─────────────────┘
项目目录结构
bash
easy-query-demo/
├── pom.xml # Maven 配置
├── src/
│ ├── main/
│ │ ├── java/com/demo/easyquery/
│ │ │ ├── EasyQueryApplication.java # 启动类
│ │ │ │
│ │ │ ├── config/ # 配置层
│ │ │ │ ├── datasource/
│ │ │ │ │ ├── DataSourceType.java # 数据源类型枚举
│ │ │ │ │ ├── DataSourceContextHolder.java # 数据源上下文
│ │ │ │ │ ├── DynamicDataSource.java # 动态数据源
│ │ │ │ │ ├── @DataSource.java # 数据源切换注解
│ │ │ │ │ └── DataSourceAspect.java # AOP 切面
│ │ │ │ ├── adapter/
│ │ │ │ │ ├── DatabaseType.java # 数据库类型
│ │ │ │ │ ├── DatabaseFunctionAdapter.java # 函数适配器接口
│ │ │ │ │ ├── PostgreSQLFunctionAdapter.java
│ │ │ │ │ ├── DamengFunctionAdapter.java
│ │ │ │ │ └── DatabaseFunctionAdapterFactory.java
│ │ │ │ ├── DataSourceConfig.java # 双数据源配置
│ │ │ │ └── EasyQueryConfig.java # EasyQuery 实例配置
│ │ │ │
│ │ │ ├── entity/ # 实体层
│ │ │ │ ├── User.java # 通用用户实体
│ │ │ │ ├── Product.java # 通用产品实体
│ │ │ │ ├── pg/
│ │ │ │ │ ├── PgUser.java # PostgreSQL 用户实体
│ │ │ │ │ └── PgProduct.java # PostgreSQL 产品实体
│ │ │ │ └── dm/
│ │ │ │ ├── DmUser.java # 达梦用户实体
│ │ │ │ └── DmProduct.java # 达梦产品实体
│ │ │ │
│ │ │ ├── repository/ # 数据访问层
│ │ │ │ ├── pg/
│ │ │ │ │ ├── UserRepository.java # PG 用户仓储
│ │ │ │ │ └── PgProductRepository.java # PG 产品仓储
│ │ │ │ └── dm/
│ │ │ │ ├── DmUserRepository.java # DM 用户仓储
│ │ │ │ └── ProductRepository.java # DM 产品仓储
│ │ │ │
│ │ │ ├── service/ # 业务层
│ │ │ │ ├── SameDatabaseJoinService.java # 同库关联查询
│ │ │ │ ├── FederatedQueryService.java # 跨库联合查询
│ │ │ │ ├── DynamicDataSourceService.java # 动态数据源演示
│ │ │ │ └── UserService.java
│ │ │ │
│ │ │ ├── controller/ # 控制层
│ │ │ │ ├── UnifiedQueryController.java # 统一查询控制器
│ │ │ │ ├── FederatedQueryController.java # 联邦查询控制器
│ │ │ │ ├── SameDatabaseJoinController.java # 同库关联查询控制器
│ │ │ │ └── MultiDataSourceController.java # 多数据源控制器
│ │ │ │
│ │ │ ├── dto/ # 数据传输对象
│ │ │ │ ├── UserProductDTO.java # PG 用户产品关联 DTO
│ │ │ │ ├── UserProductStatsDTO.java # PG 用户产品统计 DTO
│ │ │ │ ├── ProductBuyerStatsDTO.java # PG 产品买家统计 DTO
│ │ │ │ ├── DmUserProductDTO.java # DM 用户产品关联 DTO
│ │ │ │ └── DmProductBuyerStatsDTO.java # DM 产品买家统计 DTO
│ │ │ │
│ │ │ └── dto/proxy/ # DTO 代理类(APT 生成)
│ │ │ ├── UserProductDTOProxy.java
│ │ │ ├── UserProductStatsDTOProxy.java
│ │ │ ├── ProductBuyerStatsDTOProxy.java
│ │ │ ├── DmUserProductDTOProxy.java
│ │ │ └── DmProductBuyerStatsDTOProxy.java
│ │ │
│ │ └── resources/
│ │ ├── application.properties
│ │ └── application-datasource.properties
│ │
│ └── test/ # 测试代码
│ └── java/com/demo/easyquery/
│ ├── FederatedQueryServiceTest.java
│ ├── MultiDataSourceTest.java
│ └── DatabaseComparisonTest.java
架构 4+1 视图
1. 逻辑视图 (Logical View)
描述系统的功能组件和它们之间的关系。
graph TB
subgraph "API Layer"
UC[UnifiedQueryController
统一查询接口] FQC[FederatedQueryController
联邦查询接口] end subgraph "Service Layer" SDS[SameDatabaseJoinService
同库关联查询] FQS[FederatedQueryService
跨库联合查询] end subgraph "Repository Layer" UR[UserRepository] PR[ProductRepository] PgPR[PgProductRepository] DmUR[DmUserRepository] end subgraph "Entity Layer" U[User / PgUser / DmUser] P[Product / PgProduct / DmProduct] end subgraph "Data Source Layer" PG[(PostgreSQL
t_user + t_product)] DM[(Dameng DB
T_USER + T_PRODUCT)] end UC --> SDS UC --> FQS FQC --> FQS SDS --> UR SDS --> PgPR SDS --> DmUR SDS --> PR FQS --> UR FQS --> PR UR --> PG PgPR --> PG DmUR --> DM PR --> DM style PG fill:#6495ED style DM fill:#DC143C
统一查询接口] FQC[FederatedQueryController
联邦查询接口] end subgraph "Service Layer" SDS[SameDatabaseJoinService
同库关联查询] FQS[FederatedQueryService
跨库联合查询] end subgraph "Repository Layer" UR[UserRepository] PR[ProductRepository] PgPR[PgProductRepository] DmUR[DmUserRepository] end subgraph "Entity Layer" U[User / PgUser / DmUser] P[Product / PgProduct / DmProduct] end subgraph "Data Source Layer" PG[(PostgreSQL
t_user + t_product)] DM[(Dameng DB
T_USER + T_PRODUCT)] end UC --> SDS UC --> FQS FQC --> FQS SDS --> UR SDS --> PgPR SDS --> DmUR SDS --> PR FQS --> UR FQS --> PR UR --> PG PgPR --> PG DmUR --> DM PR --> DM style PG fill:#6495ED style DM fill:#DC143C
2. 实现视图 (Implementation View)
描述系统的代码结构和模块组织。
graph LR
subgraph Controller [Controller Layer]
UC[UnifiedQueryController]
FQC[FederatedQueryController]
end
subgraph Service [Service Layer]
SDS[SameDatabaseJoinService]
FQS[FederatedQueryService]
end
subgraph Repository [Repository Layer]
UR[UserRepository]
PR[ProductRepository]
PgPR[PgProductRepository]
DmUR[DmUserRepository]
end
subgraph Entity [Entity Layer]
U[User]
P[Product]
PgU[PgUser]
PgP[PgProduct]
DmU[DmUser]
DmP[DmProduct]
end
UC --> SDS
FQC --> FQS
SDS --> UR
SDS --> PgPR
SDS --> DmUR
SDS --> PR
FQS --> UR
FQS --> PR
UR --> U
UR --> PgU
PgPR --> PgP
DmUR --> DmU
PR --> P
PR --> DmP
3. 进程视图 (Process View)
描述系统的并发和同步机制。
sequenceDiagram
participant Client
participant Controller
participant Service
participant PGRepo as PG Repository
participant DMRepo as DM Repository
participant PG as PostgreSQL
participant DM as Dameng DB
Client->>Controller: GET /api/query/pg/affordable-products
Controller->>Service: queryPgUserAffordableProducts()
Service->>PGRepo: queryable(PgUser.class)
PGRepo->>PG: SELECT * FROM t_user
PG-->>PGRepo: List
Service->>PGRepo: queryable(PgProduct.class)
PGRepo->>PG: SELECT * FROM t_product
PG-->>PGRepo: List
Service->>Service: 组合结果
Service-->>Controller: List
4. 部署视图 (Deployment View)
描述系统的物理部署架构。
graph TB
subgraph "Client Layer"
Browser[Web Browser]
RestClient[REST Client]
end
subgraph "Application Server"
App[Spring Boot App
Port: 8080
JDK 21] end subgraph "Database Layer" subgraph "PostgreSQL Server" PGDB[(PostgreSQL :5432
Database: ins_bus
Tables:
- t_user
- t_product)] end subgraph "Dameng Database Server" DMDB[(Dameng DB :5236
Database: ins_bus
Tables:
- T_USER
- T_PRODUCT)] end end Browser -->|HTTP| App RestClient -->|HTTP| App App -->|JDBC| PGDB App -->|JDBC| DMDB style PGDB fill:#6495ED,color:#fff style DMDB fill:#DC143C,color:#fff
Port: 8080
JDK 21] end subgraph "Database Layer" subgraph "PostgreSQL Server" PGDB[(PostgreSQL :5432
Database: ins_bus
Tables:
- t_user
- t_product)] end subgraph "Dameng Database Server" DMDB[(Dameng DB :5236
Database: ins_bus
Tables:
- T_USER
- T_PRODUCT)] end end Browser -->|HTTP| App RestClient -->|HTTP| App App -->|JDBC| PGDB App -->|JDBC| DMDB style PGDB fill:#6495ED,color:#fff style DMDB fill:#DC143C,color:#fff
5. 用例视图 (Use Case View)
描述系统的功能需求和使用场景。
graph TB
Actor(User)
Admin(Admin)
subgraph "PostgreSQL 同库查询"
UC1[查询用户可购买产品]
UC2[查询所有用户产品组合]
UC3[统计用户购买力]
end
subgraph "达梦同库查询"
UC4[查询产品潜在购买者]
UC5[分析购买力]
UC6[统计产品购买者]
end
subgraph "跨库联合查询"
UC7[对比数据库数据]
UC8[汇总所有数据]
end
subgraph "数据管理"
UC9[创建用户]
UC10[创建产品]
UC11[更新数据]
UC12[删除数据]
end
Actor --> UC1
Actor --> UC2
Actor --> UC3
Actor --> UC4
Actor --> UC5
Actor --> UC6
Actor --> UC7
Actor --> UC8
Admin --> UC9
Admin --> UC10
Admin --> UC11
Admin --> UC12
UML 9 图
1. 类图 (Class Diagram)
描述系统的静态结构和类之间的关系。
classDiagram
class UnifiedQueryController {
-SameDatabaseJoinService sameDatabaseJoinService
+getPgAffordableProducts() Map
+getDmPurchasingPower() Map
+compareDatabases() Map
}
class SameDatabaseJoinService {
-UserRepository pgUserRepository
-PgProductRepository pgProductRepository
-DmUserRepository dmUserRepository
-ProductRepository dmProductRepository
+queryPgUserAffordableProducts() List
+analyzeDmPurchasingPower() List
+compareDataBetweenDatabases() Map
}
class UserRepository {
-EasyEntityQuery easyEntityQuery
+findAll() List
+findById(String) User
+queryUsersWithAffordableProducts() List
}
class PgProductRepository {
-EasyEntityQuery easyEntityQuery
+findAll() List
+findById(String) PgProduct
}
class DmUserRepository {
-EasyEntityQuery easyEntityQuery
+findAll() List
+findById(String) DmUser
}
class ProductRepository {
-EasyEntityQuery easyEntityQuery
+findAll() List
+queryUsersForProduct(String) List
}
class User {
<>
-String id
-String username
-String email
-Integer age
-BigDecimal balance
}
class PgUser {
<>
@Table("t_user")
-String id
-String username
}
class DmUser {
<>
@Table("T_USER")
-String id
-String username
}
class Product {
<>
@Table("T_PRODUCT")
-String id
-String name
-BigDecimal price
}
class PgProduct {
<>
@Table("t_product")
-String id
-String name
}
class DmProduct {
<>
@Table("T_PRODUCT")
-String id
-String name
}
UnifiedQueryController --> SameDatabaseJoinService
SameDatabaseJoinService --> UserRepository
SameDatabaseJoinService --> PgProductRepository
SameDatabaseJoinService --> DmUserRepository
SameDatabaseJoinService --> ProductRepository
UserRepository --> User : manages
PgProductRepository --> PgProduct : manages
DmUserRepository --> DmUser : manages
ProductRepository --> Product : manages
2. 对象图 (Object Diagram)
描述系统在特定时刻的对象状态。
graph TB
subgraph Controllers [Controllers]
ctrl["UnifiedQueryController
(instance)"] end subgraph Service [Service] svc["SameDatabaseJoinService
(instance)"] end subgraph PGEs [PostgreSQL Entities] pgUser1["PgUser
id: u001
username: 张三
balance: 5000.00"] pgUser2["PgUser
id: u004
username: 赵六
balance: 10000.00"] pgProd1["PgProduct
id: p001
name: 智能手机
price: 2999.00"] pgProd2["PgProduct
id: p008
name: 机械键盘
price: 799.00"] end subgraph DMEs [Dameng Entities] dmUser1["DmUser
id: u001
username: 张三
balance: 5000.00"] dmProd1["DmProduct
id: p001
name: 智能手机
price: 2999.00"] end ctrl --> svc svc --> pgUser1 svc --> pgUser2 svc --> pgProd1 svc --> pgProd2 svc --> dmUser1 svc --> dmProd1
(instance)"] end subgraph Service [Service] svc["SameDatabaseJoinService
(instance)"] end subgraph PGEs [PostgreSQL Entities] pgUser1["PgUser
id: u001
username: 张三
balance: 5000.00"] pgUser2["PgUser
id: u004
username: 赵六
balance: 10000.00"] pgProd1["PgProduct
id: p001
name: 智能手机
price: 2999.00"] pgProd2["PgProduct
id: p008
name: 机械键盘
price: 799.00"] end subgraph DMEs [Dameng Entities] dmUser1["DmUser
id: u001
username: 张三
balance: 5000.00"] dmProd1["DmProduct
id: p001
name: 智能手机
price: 2999.00"] end ctrl --> svc svc --> pgUser1 svc --> pgUser2 svc --> pgProd1 svc --> pgProd2 svc --> dmUser1 svc --> dmProd1
3. 用例图 (Use Case Diagram)
graph LR
User((用户))
Admin((管理员))
System(System)
subgraph "PostgreSQL 同库"
PG1[查询用户可购买产品]
PG2[查询用户产品组合]
PG3[用户购买力统计]
end
subgraph "达梦同库"
DM1[查询产品购买者]
DM2[购买力分析]
DM3[产品购买者统计]
end
subgraph "跨库"
Cross1[数据库数据对比]
Cross2[所有数据汇总]
end
subgraph "管理"
Mgmt1[数据初始化]
Mgmt2[健康检查]
end
User --> PG1
User --> PG2
User --> PG3
User --> DM1
User --> DM2
User --> DM3
User --> Cross1
User --> Cross2
Admin --> Mgmt1
Admin --> Mgmt2
4. 序列图 (Sequence Diagram)
描述 PostgreSQL 同库查询的时序交互。
sequenceDiagram
participant Client
participant Controller
participant Service
participant PgUserRepo
participant PgProductRepo
participant PG
Client->>Controller: GET /api/query/pg/affordable-products
Controller->>Service: queryPgUserAffordableProducts()
Service->>PgUserRepo: queryable(PgUser.class).toList()
PgUserRepo->>PG: SELECT * FROM t_user
PG-->>PgUserRepo: List
PgUserRepo-->>Service: List
Service->>PgProductRepo: queryable(PgProduct.class).toList()
PgProductRepo->>PG: SELECT * FROM t_product
PG-->>PgProductRepo: List
PgProductRepo-->>Service: List
Service->>Service: Stream组合查询
Note over Service: where balance >= price
Service-->>Controller: List
Controller-->>Client: JSON Response
5. 活动图 (Activity Diagram)
描述跨库数据对比的业务流程。
flowchart TD
Start([开始]) --> Receive[接收对比请求]
Receive --> QueryPG[查询 PostgreSQL]
Receive --> QueryDM[查询达梦数据库]
QueryPG --> GetPGUsers[获取用户统计]
QueryPG --> GetPGProducts[获取产品统计]
QueryDM --> GetDMUsers[获取用户统计]
QueryDM --> GetDMProducts[获取产品统计]
GetPGUsers --> WaitPG[等待 PG 完成]
GetPGProducts --> WaitPG
GetDMUsers --> WaitDM[等待 DM 完成]
GetDMProducts --> WaitDM
WaitPG --> Combine[组合结果]
WaitDM --> Combine
Combine --> Calculate[计算差异]
Calculate --> Build[构建响应]
Build --> End([返回响应])
6. 协作图 (Communication Diagram)
描述跨库查询的对象间协作。
graph LR
Client[Client] -->|1: compare| Controller[Controller]
Controller -->|2: compare| Service[Service]
Service -->|3: count| PGRepo[PG UserRepo]
Service -->|4: count| DMRepo[DM UserRepo]
Service -->|5: count| PGProdRepo[PG ProductRepo]
Service -->|6: count| DMProdRepo[DM ProductRepo]
PGRepo -->|7: result| Service
DMRepo -->|8: result| Service
PGProdRepo -->|9: result| Service
DMProdRepo -->|10: result| Service
Service -->|11: Map| Controller
Controller -->|12: JSON| Client
7. 状态图 (State Diagram)
描述数据源切换的状态变化。
stateDiagram-v2
[*] --> Primary: 启动默认 PG
Primary --> Switching: @DataSource(DM)
Switching --> Dameng: 切换到 DM
Switching --> PostgreSQL: 切换到 PG
Dameng --> Querying: 执行查询
PostgreSQL --> Querying: 执行查询
Querying --> Primary: 重置为默认
Querying --> Switching: 继续切换
8. 组件图 (Component Diagram)
描述系统的物理组件和依赖关系。
graph TB
subgraph "Web Layer"
REST[REST API]
end
subgraph "Business Layer"
SameDb[Same-DB Join Service]
CrossDb[Cross-DB Join Service]
end
subgraph "Data Access Layer"
PG_UserRepo[PG User Repository]
PG_ProdRepo[PG Product Repository]
DM_UserRepo[DM User Repository]
DM_ProdRepo[DM Product Repository]
end
subgraph "ORM Layer"
EQ_PG[EasyQuery PG]
EQ_DM[EasyQuery DM]
end
subgraph "Database Layer"
PG[(PostgreSQL)]
DM[(Dameng)]
end
REST --> SameDb
REST --> CrossDb
SameDb --> PG_UserRepo
SameDb --> PG_ProdRepo
SameDb --> DM_UserRepo
SameDb --> DM_ProdRepo
CrossDb --> PG_UserRepo
CrossDb --> DM_ProdRepo
PG_UserRepo --> EQ_PG
PG_ProdRepo --> EQ_PG
DM_UserRepo --> EQ_DM
DM_ProdRepo --> EQ_DM
EQ_PG --> PG
EQ_DM --> DM
9. 部署图 (Deployment Diagram)
graph TB
subgraph "客户端"
Client[Web Browser/REST Client]
end
subgraph "应用服务器 :8080"
App[Spring Boot Application]
JVM[JDK 21]
App -.运行.-> JVM
end
subgraph "PostgreSQL 服务器 :5432"
PGIns[数据库实例]
PGDB[(ins_bus
t_user
t_product)] PGIns -.存储.-> PGDB end subgraph "达梦数据库服务器 :5236" DMIns[数据库实例] DMDB[(ins_bus
T_USER
T_PRODUCT)] DMIns -.存储.-> DMDB end Client -->|HTTP| App App -->|JDBC| PGIns App -->|JDBC| DMIns style PGDB fill:#6495ED,color:#fff style DMDB fill:#DC143C,color:#fff
t_user
t_product)] PGIns -.存储.-> PGDB end subgraph "达梦数据库服务器 :5236" DMIns[数据库实例] DMDB[(ins_bus
T_USER
T_PRODUCT)] DMIns -.存储.-> DMDB end Client -->|HTTP| App App -->|JDBC| PGIns App -->|JDBC| DMIns style PGDB fill:#6495ED,color:#fff style DMDB fill:#DC143C,color:#fff
数据库设计
双库双表架构
本项目采用双库双表架构,每个数据库都包含完整的用户和产品表:
| 数据库 | 用户表 | 产品表 | 表名规范 |
|---|---|---|---|
| PostgreSQL | t_user |
t_product |
小写+下划线 |
| 达梦数据库 | T_USER |
T_PRODUCT |
大写+下划线 |
表结构设计
PostgreSQL 用户表 (t_user)
| 字段名 | 类型 | 约束 | 说明 |
|---|---|---|---|
| id | VARCHAR(50) | PRIMARY KEY | 用户ID |
| username | VARCHAR(100) | NOT NULL | 用户名 |
| VARCHAR(100) | 邮箱 | ||
| age | INTEGER | 年龄 | |
| balance | DECIMAL(10,2) | 余额 | |
| create_time | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | 创建时间 |
| update_time | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | 更新时间 |
PostgreSQL 产品表 (t_product)
| 字段名 | 类型 | 约束 | 说明 |
|---|---|---|---|
| id | VARCHAR(50) | PRIMARY KEY | 产品ID |
| name | VARCHAR(100) | NOT NULL | 产品名称 |
| description | VARCHAR(500) | 产品描述 | |
| price | DECIMAL(10,2) | NOT NULL | 价格 |
| stock | INTEGER | NOT NULL | 库存 |
| create_time | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | 创建时间 |
| update_time | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | 更新时间 |
达梦用户表 (T_USER)
| 字段名 | 类型 | 约束 | 说明 |
|---|---|---|---|
| ID | VARCHAR(50) | PRIMARY KEY | 用户ID |
| USERNAME | VARCHAR(100) | NOT NULL | 用户名 |
| VARCHAR(100) | 邮箱 | ||
| AGE | INTEGER | 年龄 | |
| BALANCE | DECIMAL(10,2) | 余额 | |
| CREATE_TIME | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | 创建时间 |
| UPDATE_TIME | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | 更新时间 |
达梦产品表 (T_PRODUCT)
| 字段名 | 类型 | 约束 | 说明 |
|---|---|---|---|
| ID | VARCHAR(50) | PRIMARY KEY | 产品ID |
| NAME | VARCHAR(100) | NOT NULL | 产品名称 |
| DESCRIPTION | VARCHAR(500) | 产品描述 | |
| PRICE | DECIMAL(10,2) | NOT NULL | 价格 |
| STOCK | INTEGER | NOT NULL | 库存 |
| CREATE_TIME | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | 创建时间 |
| UPDATE_TIME | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | 更新时间 |
索引设计
PostgreSQL 索引
sql
-- 用户表索引
CREATE INDEX idx_pg_username ON t_user(username);
CREATE INDEX idx_pg_email ON t_user(email);
CREATE INDEX idx_pg_age ON t_user(age);
CREATE INDEX idx_pg_balance ON t_user(balance);
-- 产品表索引
CREATE INDEX idx_pg_product_name ON t_product(name);
CREATE INDEX idx_pg_product_price ON t_product(price);
CREATE INDEX idx_pg_product_stock ON t_product(stock);
达梦索引
sql
-- 用户表索引
CREATE INDEX idx_dm_username ON T_USER(USERNAME);
CREATE INDEX idx_dm_email ON T_USER(EMAIL);
CREATE INDEX idx_dm_age ON T_USER(AGE);
CREATE INDEX idx_dm_balance ON T_USER(BALANCE);
-- 产品表索引
CREATE INDEX idx_dm_product_name ON T_PRODUCT(NAME);
CREATE INDEX idx_dm_product_price ON T_PRODUCT(PRICE);
CREATE INDEX idx_dm_product_stock ON T_PRODUCT(STOCK);