) dataModel.get("table");
- if (tableMap != null) {
- result = result.replace("$!{table.comment}", safeString(tableMap.get("comment")));
- result = result.replace("${table.mapperName}", safeString(tableMap.get("mapperName")));
- result = result.replace("${table.serviceName}", safeString(tableMap.get("serviceName")));
- result = result.replace("${table.serviceImplName}", safeString(tableMap.get("serviceImplName")));
- result = result.replace("${table.controllerName}", safeString(tableMap.get("controllerName")));
- }
-
- // 处理条件判断和循环
- result = processConditions(result);
-
- return result;
- }
-
- /**
- * 安全的字符串转换,避免空指针
- */
- private String safeString(Object obj) {
- return obj == null ? "" : String.valueOf(obj);
- }
-
- /**
- * 处理条件判断
- */
- private String processConditions(String template) {
- // 简化处理,移除Velocity条件标签
- String result = template;
- result = result.replaceAll("#if\\([^)]*\\)", "");
- result = result.replaceAll("#end", "");
- result = result.replaceAll("#else", "");
- result = result.replaceAll("#elseif\\([^)]*\\)", "");
- result = result.replaceAll("#foreach\\([^)]*\\)", "");
-
- return result;
- }
}
\ No newline at end of file
diff --git a/db2java-core/src/main/resources/templates/controller.java.btl b/db2java-core/src/main/resources/templates/controller.java.btl
index 6bd3b69..a44d163 100644
--- a/db2java-core/src/main/resources/templates/controller.java.btl
+++ b/db2java-core/src/main/resources/templates/controller.java.btl
@@ -9,6 +9,13 @@ import org.springframework.stereotype.Controller;
<% if(isNotEmpty(superControllerClassPackage)){ %>
import ${superControllerClassPackage};
<% } %>
+<% if(isNotEmpty(importControllerFrameworkPackages)){ %>
+<% for(pkg in importControllerFrameworkPackages){ %>
+<% if(pkg != "org.springframework.web.bind.annotation.RestController"){ %>
+import ${pkg};
+<% } %>
+<% } %>
+<% } %>
/**
*
@@ -33,5 +40,11 @@ public class ${table.controllerName} extends ${superControllerClass} {
public class ${table.controllerName} {
<% } %>
-}
+<% if(isNotEmpty(controllerAutowiredFields)){ %>
+<% for(field in controllerAutowiredFields){ %>
+ ${field.annotation}
+ private ${field.type} ${field.name};
<% } %>
+<% } %>
+}
+<% } %>
\ No newline at end of file
diff --git a/db2java-core/src/main/resources/templates/controller.java.vm b/db2java-core/src/main/resources/templates/controller.java.vm
index 0a27fe4..14641e0 100644
--- a/db2java-core/src/main/resources/templates/controller.java.vm
+++ b/db2java-core/src/main/resources/templates/controller.java.vm
@@ -9,6 +9,13 @@ import org.springframework.stereotype.Controller;
#if(${superControllerClassPackage})
import ${superControllerClassPackage};
#end
+#if(${importControllerFrameworkPackages})
+#foreach($pkg in ${importControllerFrameworkPackages})
+#if($pkg != "org.springframework.web.bind.annotation.RestController")
+import ${pkg};
+#end
+#end
+#end
/**
*
@@ -33,5 +40,11 @@ public class ${table.controllerName} extends ${superControllerClass} {
public class ${table.controllerName} {
#end
-}
+#if(${controllerAutowiredFields})
+#foreach($field in ${controllerAutowiredFields})
+ ${field.annotation}
+ private ${field.type} ${field.name};
#end
+#end
+}
+#end
\ No newline at end of file
diff --git a/db2java-core/src/main/resources/templates/entity.java.btl b/db2java-core/src/main/resources/templates/entity.java.btl
index 2702e54..dc79114 100644
--- a/db2java-core/src/main/resources/templates/entity.java.btl
+++ b/db2java-core/src/main/resources/templates/entity.java.btl
@@ -57,6 +57,33 @@ public class ${entity} {
private ${field.propertyType} ${field.propertyName};
<% } %>
<% /** -----------END 字段循环遍历----------- **/ %>
+
+<% if(isNotEmpty(table.oneToOneFields)){ %>
+<% for(field in table.oneToOneFields){ %>
+
+ /**
+ * ${field.comment}
+ */
+ <% for(an in field.annotationAttributesList){ %>
+ ${an.displayName}
+ <% } %>
+ private ${field.propertyType} ${field.propertyName};
+<% } %>
+<% } %>
+
+<% if(isNotEmpty(table.oneToManyFields)){ %>
+<% for(field in table.oneToManyFields){ %>
+
+ /**
+ * ${field.comment}
+ */
+ <% for(an in field.annotationAttributesList){ %>
+ ${an.displayName}
+ <% } %>
+ private java.util.List<${field.propertyType}> ${field.propertyName};
+<% } %>
+<% } %>
+
<% if(!entityLombokModel){ %>
<% for(field in table.fields){ %>
<%
@@ -116,4 +143,4 @@ public class ${entity} {
"}";
}
<% } %>
-}
+}
\ No newline at end of file
diff --git a/db2java-core/src/main/resources/templates/entity.java.vm b/db2java-core/src/main/resources/templates/entity.java.vm
index 902cf7d..2b1d69e 100644
--- a/db2java-core/src/main/resources/templates/entity.java.vm
+++ b/db2java-core/src/main/resources/templates/entity.java.vm
@@ -54,6 +54,37 @@ public class ${entity} {
private ${field.propertyType} ${field.propertyName};
#end
## ---------- END 字段循环遍历 ----------
+
+## ---------- BEGIN 一对一关联字段 ----------
+#if(${table.oneToOneFields})
+#foreach($field in ${table.oneToOneFields})
+
+ /**
+ * ${field.comment}
+ */
+#foreach($an in ${field.annotationAttributesList})
+ ${an.displayName}
+#end
+ private ${field.propertyType} ${field.propertyName};
+#end
+#end
+## ---------- END 一对一关联字段 ----------
+
+## ---------- BEGIN 一对多关联字段 ----------
+#if(${table.oneToManyFields})
+#foreach($field in ${table.oneToManyFields})
+
+ /**
+ * ${field.comment}
+ */
+#foreach($an in ${field.annotationAttributesList})
+ ${an.displayName}
+#end
+ private java.util.List<${field.propertyType}> ${field.propertyName};
+#end
+#end
+## ---------- END 一对多关联字段 ----------
+
#if(!${entityLombokModel})
#foreach($field in ${table.fields})
#if(${field.propertyType.equals("boolean")})
@@ -112,4 +143,4 @@ public class ${entity} {
"}";
}
#end
-}
+}
\ No newline at end of file
diff --git a/db2java-core/src/test/java/example/Db2JavaExample.java b/db2java-core/src/test/java/example/Db2JavaExample.java
index f437692..4d2adb2 100644
--- a/db2java-core/src/test/java/example/Db2JavaExample.java
+++ b/db2java-core/src/test/java/example/Db2JavaExample.java
@@ -7,12 +7,10 @@ import com.yexuejc.db2java.core.generator.GenerationResult;
import com.yexuejc.db2java.core.generator.TableInfo;
import java.util.Arrays;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
/**
- * DB2Java使用示例
+ * 改进的DB2Java使用示例
*
* @author yexuejc
* @since 2025-01
@@ -21,62 +19,117 @@ public class Db2JavaExample {
public static void main(String[] args) {
try {
- // 打印支持的模板引擎和生成模式
- System.out.println("支持的模板引擎: " + String.join(", ", Db2JavaCore.getSupportedTemplateEngines()));
- System.out.println("支持的生成模式: " + String.join(", ", Db2JavaCore.getSupportedGenerationModes()));
+ System.out.println("=== 改进的DB2Java代码生成测试 ===");
// 模拟一个表信息
TableInfo userTable = createUserTable();
List tableInfos = Arrays.asList(userTable);
- // 测试不同的模板引擎和生成模式组合
- testGeneration(tableInfos, "freemarker", "mybatis");
- testGeneration(tableInfos, "velocity", "mybatis-plus");
- testGeneration(tableInfos, "beetl", "jpa");
- testGeneration(tableInfos, "enjoy", "hibernate");
-
+ // 测试生成标准的Java代码
+ // 设置为coding模式,生成标准的src/main/java结构
+ testImprovedGeneration(tableInfos, "freemarker", "mybatis", true,"coding");// 基本的OK
+ testImprovedGeneration(tableInfos, "freemarker", "mybatis", false,"coding");// 基本的OK
+ testImprovedGeneration(tableInfos, "freemarker", "mybatis", false,"file");// 基本的OK
+ testImprovedGeneration(tableInfos, "freemarker", "mybatis-plus", true,"coding");// 基本的OK
+ testImprovedGeneration(tableInfos, "freemarker", "jpa", false,"coding");// 测试不使用Lombok的情况
+ testImprovedGeneration(tableInfos, "freemarker", "jpa", true,"coding");// 基本的OK
+
+ // velocity,beetl,enjoy
+
+ testImprovedGeneration(tableInfos, "velocity", "mybatis", true,"coding");
+ testImprovedGeneration(tableInfos, "velocity", "mybatis", false,"coding");
+ testImprovedGeneration(tableInfos, "velocity", "mybatis", false,"file");
+ testImprovedGeneration(tableInfos, "velocity", "mybatis-plus", true,"coding");
+ testImprovedGeneration(tableInfos, "velocity", "hibernate", false,"coding");
+ testImprovedGeneration(tableInfos, "velocity", "jpa", true,"coding");
+
+ testImprovedGeneration(tableInfos, "beetl", "mybatis", true,"coding");
+ testImprovedGeneration(tableInfos, "beetl", "mybatis", false,"coding");
+ testImprovedGeneration(tableInfos, "beetl", "mybatis", false,"file");
+ testImprovedGeneration(tableInfos, "beetl", "mybatis-plus", true,"coding");
+ testImprovedGeneration(tableInfos, "beetl", "hibernate", false,"coding");
+ testImprovedGeneration(tableInfos, "beetl", "jpa", true,"coding");
+
} catch (Exception e) {
System.err.println("生成代码时发生错误: " + e.getMessage());
e.printStackTrace();
}
}
- private static void testGeneration(List tableInfos, String engine, String mode) {
+ private static void testImprovedGeneration(List tableInfos, String engine, String mode, boolean useLombok,String type) {
try {
- System.out.println("\n=== 测试 " + engine + " + " + mode + " ===");
+ String lombokSuffix = useLombok ? "-lombok" : "-standard";
+ System.out.println("\n=== 测试 " + engine + " + " + mode + lombokSuffix + " ===");
// 创建生成配置
GenerationConfig config = new GenerationConfig();
config.setEngine(engine);
config.setMode(mode);
- config.setType("file"); // 设置为file模式,直接在指定目录下生成
- config.setOutputPath("C:/0110_Workspace/yexuejc/db2java/export/" + engine + "-" + mode);
- config.setPackageName("com.example.generated." + mode);
- config.setAuthor("db2java-generator");
- config.setLombok("freemarker".equals(engine)); // 只有FreeMarker使用Lombok
+ config.setType(type); // 设置为coding模式,生成标准的src/main/java结构
+ config.setOutputPath("C:/0110_Workspace/yexuejc/db2java/export/improved-" + engine + "-" + mode + lombokSuffix);
+ config.setPackageName("com.yexuejc.db2java.export." + mode.replace("-", ""));
+ config.setAuthor("yexuejc");
+ config.setLombok(useLombok);
config.setOverride(true);
- // 通过extraConfig传递额外配置
- Map extraConfig = new HashMap<>();
- extraConfig.put("serialVersionUID", true);
- extraConfig.put("fieldUseJavaDoc", true);
- extraConfig.put("toString", true);
- extraConfig.put("serializable", true);
- config.setExtraConfig(extraConfig);
-
// 生成代码
GenerationResult result = Db2JavaCore.generateCode(tableInfos, config);
if (result.isSuccess()) {
System.out.println("生成成功!生成了 " + result.getGeneratedFiles().size() + " 个文件:");
result.getGeneratedFiles().forEach(file ->
- System.out.println(" - " + file.getFileType() + ": " + file.getFileName()));
+ System.out.println(" - " + file.getFileType() + ": " + file.getFileName() + " (" + file.getFileSize() + " bytes)"));
+
+ // 显示生成的Entity代码预览
+ result.getGeneratedFiles().stream()
+ .filter(file -> "Entity".equals(file.getFileType()))
+ .findFirst()
+ .ifPresent(file -> {
+ System.out.println("\n生成的Entity代码预览:");
+ System.out.println("================================");
+ String content = file.getContent();
+ if (content != null && !content.isEmpty()) {
+ String[] lines = content.split("\n");
+ for (int i = 0; i < Math.min(lines.length, 30); i++) {
+ System.out.println(lines[i]);
+ }
+ if (lines.length > 30) {
+ System.out.println("... (省略 " + (lines.length - 30) + " 行)");
+ }
+ } else {
+ System.out.println("警告:生成的文件内容为空!");
+ }
+ System.out.println("================================");
+ });
+
+ // 显示生成的Mapper XML代码预览
+ result.getGeneratedFiles().stream()
+ .filter(file -> "MapperXml".equals(file.getFileType()))
+ .findFirst()
+ .ifPresent(file -> {
+ System.out.println("\n生成的Mapper XML代码预览:");
+ System.out.println("================================");
+ String content = file.getContent();
+ if (content != null && !content.isEmpty()) {
+ String[] lines = content.split("\n");
+ for (int i = 0; i < Math.min(lines.length, 30); i++) {
+ System.out.println(lines[i]);
+ }
+ if (lines.length > 30) {
+ System.out.println("... (省略 " + (lines.length - 30) + " 行)");
+ }
+ } else {
+ System.out.println("警告:生成的文件内容为空!");
+ }
+ System.out.println("================================");
+ });
} else {
System.out.println("生成失败: " + result.getErrorMessage());
}
} catch (Exception e) {
System.out.println("测试失败: " + e.getMessage());
+ e.printStackTrace();
}
}
diff --git a/db2java-core/src/test/java/example/ImprovedDb2JavaExample.java b/db2java-core/src/test/java/example/ImprovedDb2JavaExample.java
deleted file mode 100644
index fe2cdf0..0000000
--- a/db2java-core/src/test/java/example/ImprovedDb2JavaExample.java
+++ /dev/null
@@ -1,115 +0,0 @@
-package example;
-
-import com.yexuejc.db2java.core.Db2JavaCore;
-import com.yexuejc.db2java.core.generator.FieldInfo;
-import com.yexuejc.db2java.core.generator.GenerationConfig;
-import com.yexuejc.db2java.core.generator.GenerationResult;
-import com.yexuejc.db2java.core.generator.TableInfo;
-
-import java.util.Arrays;
-import java.util.List;
-
-/**
- * 改进的DB2Java使用示例
- *
- * @author yexuejc
- * @since 2025-01
- */
-public class ImprovedDb2JavaExample {
-
- public static void main(String[] args) {
- try {
- System.out.println("=== 改进的DB2Java代码生成测试 ===");
-
- // 模拟一个表信息
- TableInfo userTable = createUserTable();
- List tableInfos = Arrays.asList(userTable);
-
- // 测试生成标准的Java代码
-// testImprovedGeneration(tableInfos, "freemarker", "mybatis-plus", false,"coding");
-// testImprovedGeneration(tableInfos, "freemarker", "mybatis-plus", true,"file");
- testImprovedGeneration(tableInfos, "freemarker", "mybatis", true,"coding");
-
- } catch (Exception e) {
- System.err.println("生成代码时发生错误: " + e.getMessage());
- e.printStackTrace();
- }
- }
-
- private static void testImprovedGeneration(List tableInfos, String engine, String mode, boolean useLombok,String type) {
- try {
- String lombokSuffix = useLombok ? "-lombok" : "-standard";
- System.out.println("\n=== 测试 " + engine + " + " + mode + lombokSuffix + " ===");
-
- // 创建生成配置
- GenerationConfig config = new GenerationConfig();
- config.setEngine(engine);
- config.setMode(mode);
- config.setType(type); // 设置为coding模式,生成标准的src/main/java结构
- config.setOutputPath("C:/0110_Workspace/yexuejc/db2java/export/improved-" + engine + "-" + mode + lombokSuffix);
- config.setPackageName("com.yexuejc.db2java.export." + mode.replace("-", ""));
- config.setAuthor("yexuejc");
- config.setLombok(useLombok);
- config.setOverride(true);
-
- // 生成代码
- GenerationResult result = Db2JavaCore.generateCode(tableInfos, config);
-
- if (result.isSuccess()) {
- System.out.println("生成成功!生成了 " + result.getGeneratedFiles().size() + " 个文件:");
- result.getGeneratedFiles().forEach(file ->
- System.out.println(" - " + file.getFileType() + ": " + file.getFileName()));
-
- // 显示生成的Entity代码预览
- result.getGeneratedFiles().stream()
- .filter(file -> "Entity".equals(file.getFileType()))
- .findFirst()
- .ifPresent(file -> {
- System.out.println("\n生成的Entity代码预览:");
- System.out.println("================================");
- String[] lines = file.getContent().split("\n");
- for (int i = 0; i < Math.min(lines.length, 20); i++) {
- System.out.println(lines[i]);
- }
- if (lines.length > 20) {
- System.out.println("... (省略 " + (lines.length - 20) + " 行)");
- }
- System.out.println("================================");
- });
- } else {
- System.out.println("生成失败: " + result.getErrorMessage());
- }
-
- } catch (Exception e) {
- System.out.println("测试失败: " + e.getMessage());
- }
- }
-
- private static TableInfo createUserTable() {
- TableInfo table = new TableInfo();
- table.setTableName("sys_user");
- table.setTableComment("系统用户表");
- table.setEntityName("User");
-
- // 创建字段信息
- FieldInfo id = new FieldInfo("id", "BIGINT", "id", "Long");
- id.setPrimaryKey(true);
- id.setColumnComment("主键ID");
-
- FieldInfo username = new FieldInfo("username", "VARCHAR", "username", "String");
- username.setColumnComment("用户名");
- username.setLength(50);
-
- FieldInfo email = new FieldInfo("email", "VARCHAR", "email", "String");
- email.setColumnComment("邮箱");
- email.setLength(100);
-
- FieldInfo createTime = new FieldInfo("create_time", "DATETIME", "createTime", "LocalDateTime");
- createTime.setColumnComment("创建时间");
-
- table.setFields(Arrays.asList(id, username, email, createTime));
- table.setPrimaryKeys(Arrays.asList(id));
-
- return table;
- }
-}
\ No newline at end of file
diff --git a/db2java-web/pom.xml b/db2java-web/pom.xml
index 56a10fa..42b1567 100644
--- a/db2java-web/pom.xml
+++ b/db2java-web/pom.xml
@@ -8,7 +8,7 @@
3.14.0
- 21
+ 24
UTF-8
UTF-8
quarkus-bom
diff --git a/export/pom.xml b/export/pom.xml
new file mode 100644
index 0000000..9207263
--- /dev/null
+++ b/export/pom.xml
@@ -0,0 +1,58 @@
+
+
+ 4.0.0
+
+
+ top.yexuejc
+ db2java-parent
+ 1.0-SNAPSHOT
+ ../pom.xml
+
+
+ top.yexuejc
+ export
+ 1.0-SNAPSHOT
+
+
+ 24
+ 24
+ UTF-8
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+ com.baomidou
+ mybatis-plus
+ 3.5.3
+
+
+ org.projectlombok
+ lombok
+ 1.18.30
+ provided
+
+
+ org.springframework.boot
+ spring-boot-starter-data-jpa
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-dependencies
+ 3.5.6
+ pom
+ import
+
+
+
+
+
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 93670ef..087841e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -16,8 +16,9 @@
- 24
- 24
+
+ 17
+ 17
UTF-8