diff --git a/README.md b/README.md
index 1b0aa55..04f5116 100644
--- a/README.md
+++ b/README.md
@@ -6,9 +6,9 @@
SpringBootCodeGenerator
----
-基于SpringBoot2+Freemarker的代码生成器。用DDL语句生成JPA/JdbcTemplate/Mybatis相关代码,目前以JPA的为主,各大模板也在陆续优化。
+基于SpringBoot2+Freemarker的代码生成器,用DDL语句生成JPA/JdbcTemplate/Mybatis相关代码,支持mysql/oracle/pgsql三大数据库。目前以JPA的为主,各大模板也在陆续优化。欢迎大家提交模板和交流想法!
-感谢bejson三叔将他部署在http://java.bejson.com/generator上
+另外,感谢bejson三叔将他部署在http://java.bejson.com/generator上
| 访问路径 | http://127.0.0.1:1234/generator |
@@ -16,6 +16,7 @@ SpringBootCodeGenerator
| CSDN博客 | http://blog.csdn.net/moshowgame |
| |
| 更新日期 | 更新内容 |
+| 20180925 | 优化SQL表和字段备注的推断,包括pgsql/oralce的comment on column/table情况处理等。 |
| 20180918 | 优化SQL类型推断。优化PrimaryKey判断。修复jpacontroller中Repository拼写错误问题。 |
| 20180917 | 全新首页,静态文件全部采用CDN。新增jdbcTemplate模块。 |
| 20180916-2 | 优化oracle支持,优化DDL语句中"或者'或者空格的支持。 |
@@ -24,6 +25,16 @@ SpringBootCodeGenerator
| 20180913 | 修复字段没有描述以及类型为DATE型导致的问题。新增JPA的Controller模板。 |
| 20180831 | 初始化项目。新增JPA系列Entity+Repository模板。 |
+
+
+| 类名 | 说明 |
+| tableName | sql中的表名 |
+| className | java类名 |
+| classComment | java类备注 |
+| fieldName | 字段名 |
+| fieldComment | 字段备注 |
+
+
diff --git a/generator-web/src/main/java/com/softdev/system/generator/util/TableParseUtil.java b/generator-web/src/main/java/com/softdev/system/generator/util/TableParseUtil.java
index 8c58c34..e106520 100644
--- a/generator-web/src/main/java/com/softdev/system/generator/util/TableParseUtil.java
+++ b/generator-web/src/main/java/com/softdev/system/generator/util/TableParseUtil.java
@@ -29,7 +29,7 @@ public class TableParseUtil {
if (tableSql==null || tableSql.trim().length()==0) {
throw new CodeGenerateException("Table structure can not be empty.");
}
- tableSql = tableSql.trim().replaceAll("'","`").replaceAll("\"","`").toLowerCase();
+ tableSql = tableSql.trim().replaceAll("'","`").replaceAll("\"","`").replaceAll(",",",").toLowerCase();
// table Name
String tableName = null;
@@ -65,10 +65,11 @@ public class TableParseUtil {
// class Comment
String classComment = null;
- if (tableSql.contains("COMMENT=")) {
- String classCommentTmp = tableSql.substring(tableSql.lastIndexOf("COMMENT=")+8).trim();
- if (classCommentTmp.contains("'") || classCommentTmp.indexOf("'")!=classCommentTmp.lastIndexOf("'")) {
- classCommentTmp = classCommentTmp.substring(classCommentTmp.indexOf("'")+1, classCommentTmp.lastIndexOf("'"));
+ //mysql是comment=,pgsql/oracle是comment on table,
+ if (tableSql.contains("comment=")) {
+ String classCommentTmp = tableSql.substring(tableSql.lastIndexOf("comment=")+8).replaceAll("`","").trim();
+ if (classCommentTmp.indexOf(" ")!=classCommentTmp.lastIndexOf(" ")) {
+ classCommentTmp = classCommentTmp.substring(classCommentTmp.indexOf(" ")+1, classCommentTmp.lastIndexOf(" "));
}
if (classCommentTmp!=null && classCommentTmp.trim().length()>0) {
classComment = classCommentTmp;
@@ -76,22 +77,35 @@ public class TableParseUtil {
//修复表备注为空问题
classComment = className;
}
+ }else if(tableSql.contains("comment on table")) {
+ //COMMENT ON TABLE CT_BAS_FEETYPE IS 'CT_BAS_FEETYPE';
+ String classCommentTmp = tableSql.substring(tableSql.lastIndexOf("comment on table")+17).trim();
+ //证明这是一个常规的COMMENT ON TABLE xxx IS 'xxxx'
+ if (classCommentTmp.contains("`")) {
+ classCommentTmp = classCommentTmp.substring(classCommentTmp.indexOf("`")+1);
+ classCommentTmp = classCommentTmp.substring(0,classCommentTmp.indexOf("`"));
+ classComment = classCommentTmp;
+ }else{
+ //非常规的没法分析
+ classComment = tableName;
+ }
}else{
//修复表备注为空问题
- classComment = className;
+ classComment = tableName;
}
// field List
List fieldList = new ArrayList();
+ // 正常( ) 内的一定是字段相关的定义。
String fieldListTmp = tableSql.substring(tableSql.indexOf("(")+1, tableSql.lastIndexOf(")"));
- // replave "," by "," in comment
- Matcher matcher = Pattern.compile("\\ COMMENT '(.*?)\\'").matcher(fieldListTmp); // "\\{(.*?)\\}"
+ // 匹配 comment,替换备注里的小逗号, 防止不小心被当成切割符号切割
+ Matcher matcher = Pattern.compile("\\ comment '(.*?)\\'").matcher(fieldListTmp); // "\\{(.*?)\\}"
while(matcher.find()){
String commentTmp = matcher.group();
- commentTmp = commentTmp.replaceAll("\\ COMMENT '|\\'", ""); // "\\{|\\}"
+ commentTmp = commentTmp.replaceAll("\\ comment '|\\'", ""); // "\\{|\\}"
if (commentTmp.contains(",")) {
String commentTmpFinal = commentTmp.replaceAll(",", ",");
@@ -150,14 +164,24 @@ public class TableParseUtil {
fieldClass = String.class.getSimpleName();
}
- // field comment
+ // field comment,MySQL的一般位于field行,而pgsql和oralce多位于后面。
String fieldComment = null;
- if (columnLine.contains("COMMENT")) {
- String commentTmp = fieldComment = columnLine.substring(columnLine.indexOf("COMMENT")+7).trim(); // '用户ID',
- if (commentTmp.contains("'") || commentTmp.indexOf("'")!=commentTmp.lastIndexOf("'")) {
- commentTmp = commentTmp.substring(commentTmp.indexOf("'")+1, commentTmp.lastIndexOf("'"));
+ if (columnLine.contains("comment")) {
+ String commentTmp = columnLine.substring(columnLine.indexOf("comment")+7).trim(); // '用户ID',
+ if (commentTmp.contains("`") || commentTmp.indexOf("`")!=commentTmp.lastIndexOf("`")) {
+ commentTmp = commentTmp.substring(commentTmp.indexOf("`")+1, commentTmp.lastIndexOf("`"));
}
fieldComment = commentTmp;
+ }else if(tableSql.contains("comment on column")&&tableSql.contains("."+columnName+" is `")){
+ //新增对pgsql/oracle的字段备注支持
+ //COMMENT ON COLUMN public.check_info.check_name IS '检查者名称';
+ Matcher columnCommentMatcher = Pattern.compile("."+columnName+" is `").matcher(tableSql); // "\\{(.*?)\\}"
+ while(columnCommentMatcher.find()){
+ String columnCommentTmp = columnCommentMatcher.group();
+ System.out.println(columnCommentTmp);
+ fieldComment = tableSql.substring(tableSql.indexOf(columnCommentTmp)+columnCommentTmp.length()).trim();
+ fieldComment = fieldComment.substring(0,fieldComment.indexOf("`")).trim();
+ }
}else{
//修复comment不存在导致报错的问题
fieldComment = columnName;
@@ -175,7 +199,7 @@ public class TableParseUtil {
}
if (fieldList.size() < 1) {
- throw new CodeGenerateException("Table structure anomaly.");
+ throw new CodeGenerateException("表结构分析失败,请检查语句或者提交issue给我");
}
ClassInfo codeJavaInfo = new ClassInfo();