Files
db2java/db2java-core/README.md
2025-09-28 22:59:16 +08:00

81 lines
2.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
db2java-core
---
>db2java-core是核心功能模块主要功能是把数据库表生成对应的java对象。
<br/>支持的数据库MySQLPostgreSQLOracleSQL ServerDB2
<br/>支持的生成对象EntityMapperMybatisJPA
## 支持的模板引擎
- **FreeMarker** (.ftl) - 功能强大的Java模板引擎
- **Velocity** (.vm) - Apache Velocity模板引擎
- **Beetl** (.btl) - 国产高性能模板引擎
- **Enjoy** (.ej) - JFinal框架内置模板引擎
- **可扩展** - 通过实现TemplateEngineProcessor接口支持更多引擎
## 支持的生成模式
- **JPA** - 生成JPA实体类
- **MyBatis** - 生成Entity + Mapper接口 + Mapper XML
- **MyBatis-Plus** - 生成Entity + Mapper + Service + ServiceImpl + Controller
- **Hibernate** - 生成Hibernate实体类
- **可扩展** - 通过实现CodeGenerator接口支持更多模式
## 核心架构
### 1. 枚举定义
- `TemplateEngine` - 模板引擎枚举支持4种引擎
- `GenerationMode` - 生成模式枚举支持4种模式
### 2. 模板引擎架构
- `TemplateEngineProcessor` - 模板引擎处理器接口
- `TemplateEngineFactory` - 模板引擎工厂,负责创建和管理处理器
- 具体实现FreemarkerTemplateProcessor、VelocityTemplateProcessor等
### 3. 代码生成架构
- `CodeGenerator` - 代码生成器接口
- `CodeGeneratorFactory` - 代码生成器工厂,负责创建和管理生成器
- 具体实现MybatisCodeGenerator、MybatisPlusCodeGenerator等
### 4. 数据模型
- `TableInfo` - 表信息类
- `FieldInfo` - 字段信息类
- `GenerationConfig` - 生成配置类
- `GenerationResult` - 生成结果类
- `GeneratedFile` - 生成文件信息类
### 5. 核心控制器
- `Db2JavaCore` - 主要入口类,整合所有功能
## 使用示例
```java
// 创建生成配置
GenerationConfig config = new GenerationConfig();
config.setEngine("freemarker"); // 支持: freemarker, velocity, beetl, enjoy
config.setMode("mybatis-plus"); // 支持: jpa, mybatis, mybatis-plus, hibernate
config.setOutputPath("/path/to/output");
config.setPackageName("com.example");
config.setAuthor("yexuejc");
config.setLombok(true);
// 生成代码
GenerationResult result = Db2JavaCore.generateCode(tableInfos, config);
if (result.isSuccess()) {
System.out.println("生成成功!生成了 " + result.getGeneratedFiles().size() + " 个文件");
} else {
System.out.println("生成失败: " + result.getErrorMessage());
}
```
## 扩展支持
### 添加新的模板引擎
1. 实现 `TemplateEngineProcessor` 接口
2.`TemplateEngineFactory` 中注册
3. 添加对应的模板文件
### 添加新的生成模式
1. 实现 `CodeGenerator` 接口
2.`CodeGeneratorFactory` 中注册
3. 定义支持的文件类型和模板映射