From 47e3087067abd35e6337c011f96d2338c0bb1aae Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期四, 25 七月 2024 13:39:31 +0800
Subject: [PATCH] 优化自动化代码

---
 src/main/java/org/yeshi/utils/generater/mybatis/MyBatisMapperUtil.java |   99 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 99 insertions(+), 0 deletions(-)

diff --git a/src/main/java/org/yeshi/utils/generater/mybatis/MyBatisMapperUtil.java b/src/main/java/org/yeshi/utils/generater/mybatis/MyBatisMapperUtil.java
index e8427d3..e6e7c37 100644
--- a/src/main/java/org/yeshi/utils/generater/mybatis/MyBatisMapperUtil.java
+++ b/src/main/java/org/yeshi/utils/generater/mybatis/MyBatisMapperUtil.java
@@ -14,6 +14,7 @@
 import org.dom4j.DocumentHelper;
 import org.dom4j.io.OutputFormat;
 import org.dom4j.io.XMLWriter;
+import org.springframework.data.annotation.Id;
 import org.yeshi.utils.generater.entity.MybatisColumnData;
 
 public class MyBatisMapperUtil {
@@ -409,6 +410,101 @@
         }
     }
 
+
+    public static String createSQL(Class<?> clz){
+        List<AttributeColumnMap> keysList = new ArrayList<>();
+        Field[] fields = clz.getDeclaredFields();
+        for (Field fd : fields) {
+            Annotation[] as = fd.getAnnotations();
+            AttributeColumnMap columnMap=null;
+            for (Annotation a : as) {
+                if (a instanceof Column) {
+                    Column c = (Column) a;
+                    columnMap = new AttributeColumnMap(fd.getName(), c.name(), fd.getType().getName());
+                    columnMap.length = c.length();
+                    break;
+                }
+            }
+            for (Annotation a : as) {
+                if(a instanceof Id){
+                    // 涓婚敭
+                    columnMap.mainKey = true;
+                    break;
+                }
+            }
+            if(columnMap!=null){
+                keysList.add(columnMap);
+            }
+        }
+        String tableName = "";
+        Annotation[] as = clz.getAnnotations();
+        for (Annotation a : as) {
+            if (a instanceof Table) {
+                Table t = (Table) a;
+                tableName = t.value();
+            }
+        }
+
+        StringBuffer buffer=new StringBuffer();
+        buffer.append("create table `").append(tableName).append("`(\n");
+        for(AttributeColumnMap columnMap:keysList){
+            String mysqlType = ColumnParseUtil.getMysqlType(columnMap.type);
+
+            buffer.append("\t`").append(columnMap.column).append("`");
+
+
+            if(columnMap.mainKey) {
+                if(mysqlType.equalsIgnoreCase("bigint")) {
+                    buffer.append(" bigint(20) ");
+                    buffer.append(" unsigned NOT NULL AUTO_INCREMENT ");
+                }else if(mysqlType.equalsIgnoreCase("varchar")){
+                    buffer.append(" varchar(32) ");
+                    buffer.append(" NOT NULL ");
+                }
+                buffer.append(",\n");
+                buffer.append("\tPRIMARY KEY (`"+columnMap.column+"`),\n");
+            }else{
+                String lengthStr=null;
+                int length = columnMap.length;
+                switch(mysqlType){
+                    case "varchar":
+                        if(length<=0){
+                            length = 32;
+                        }
+                        lengthStr="("+length+")";
+                        break;
+                    case "int":
+                        if(length<=0){
+                            length = 11;
+                        }
+                        lengthStr="("+length+")";
+                        break;
+                    case "datetime":
+                        lengthStr="";
+                        break;
+
+                    case "decimal":
+                        if(length<=0){
+                            length = 8;
+                        }
+                        lengthStr="("+length+",2)";
+                        break;
+                     default:
+                         lengthStr="";
+                }
+                buffer.append(mysqlType).append(lengthStr).append(" DEFAULT NULL,\n");
+            }
+        }
+
+        String fs=buffer.toString().trim();
+        if(fs.endsWith(",")){
+            fs = fs.substring(0,fs.length()-1);
+        }
+        fs+= "\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
+        return fs;
+    }
+
+
     private static AttributeColumnMap getAttributeColumnMapByAttribute(String attributeName,
                                                                        List<AttributeColumnMap> list) {
         for (AttributeColumnMap acm : list)
@@ -434,6 +530,8 @@
     String attribute;
     String column;
     String type;
+    boolean mainKey;
+    int length;
 
     public AttributeColumnMap(String attribute, String column, String type) {
         this.attribute = attribute;
@@ -441,6 +539,7 @@
         this.type = type;
     }
 
+
     public AttributeColumnMap() {
 
     }

--
Gitblit v1.8.0