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 |  126 ++++++++++++++++++++++++++++++++++++++----
 1 files changed, 114 insertions(+), 12 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 8487d86..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 {
@@ -85,16 +86,16 @@
                 }
                 if (type.equalsIgnoreCase("Date")) {
                     String tempProperty = "min" + property.substring(0, 1).toUpperCase() + property.substring(1);
-                    buffer.append(String.format("\tpublic %s %s", type, tempProperty));
-                    queryColumnData.add(new MybatisColumnData(columnName, tempProperty, type));
+                    buffer.append(String.format("\tpublic %s %s;", type, tempProperty));
+                    queryColumnData.add(new MybatisColumnData(columnName, tempProperty, type,ColumnParseUtil.getJDBCType(genericType)));
                     buffer.append("\n\t");
                     tempProperty = "max" + property.substring(0, 1).toUpperCase() + property.substring(1);
-                    buffer.append(String.format("\tpublic %s %s", type, tempProperty));
-                    queryColumnData.add(new MybatisColumnData(columnName, tempProperty, type));
+                    buffer.append(String.format("\tpublic %s %s;", type, tempProperty));
+                    queryColumnData.add(new MybatisColumnData(columnName, tempProperty, type,ColumnParseUtil.getJDBCType(genericType)));
                     buffer.append("\n\t");
                 } else {
-                    buffer.append(String.format("\tpublic %s %s", type, property));
-                    queryColumnData.add(new MybatisColumnData(columnName, property, type));
+                    buffer.append(String.format("\tpublic %s %s;", type, property));
+                    queryColumnData.add(new MybatisColumnData(columnName, property, type,ColumnParseUtil.getJDBCType(genericType)));
                     buffer.append("\n\t");
                 }
             }
@@ -115,6 +116,8 @@
         return queryColumnData;
     }
 
+
+
     public static void createMapper(Class<?> clz) {
         // 鐢熸垚mapper java鏂囦欢
         String pks = getDaoPackageName(clz);
@@ -126,12 +129,13 @@
         buffer.append(String.format("public interface %sMapper extends BaseMapper<%s> {", clz.getSimpleName(), clz.getSimpleName()));
         buffer.append("\n\n\t");
 
-        buffer.append(String.format("%s selectByPrimaryKeyForUpdate(@Param(\"id\") id);",clz.getSimpleName()));
+        //TODO 纭畾ID绫诲瀷
+        buffer.append(String.format("%s selectByPrimaryKeyForUpdate(@Param(\"id\") Long id);", clz.getSimpleName()));
         buffer.append("\n\n\t");
 
-        buffer.append(String.format("List<%s> list(@Param(\"query\") query);", clz.getSimpleName()));
+        buffer.append(String.format("List<%s> list(@Param(\"query\") DaoQuery query);", clz.getSimpleName()));
         buffer.append("\n\n\t");
-        buffer.append("long count(@Param(\"query\") query);");
+        buffer.append("long count(@Param(\"query\") DaoQuery query);");
         buffer.append("\n\n\t");
 
         List<MybatisColumnData> queryColumnData = createMapperDaoQuery(importPosition, buffer, clz);
@@ -236,7 +240,7 @@
             select.addText("select");
             org.dom4j.Element include = select.addElement("include");
             include.addAttribute("refid", "Base_Column_List");
-            select.addText(String.format("from %s where %s = #{%s,jdbcType=BIGINT}", tableName, idKeys.column,
+            select.addText(String.format(" from %s where %s = #{%s,jdbcType=BIGINT}", tableName, idKeys.column,
                     idKeys.attribute));
 
 
@@ -247,7 +251,7 @@
             select.addText("select");
             include = select.addElement("include");
             include.addAttribute("refid", "Base_Column_List");
-            select.addText(String.format("from %s where %s = #{%s,jdbcType=BIGINT} for update", tableName, idKeys.column,
+            select.addText(String.format(" from %s where %s = #{%s,jdbcType=BIGINT} for update", tableName, idKeys.column,
                     idKeys.attribute));
 
             //娣诲姞sql
@@ -274,7 +278,7 @@
             select.addText("select");
             include = select.addElement("include");
             include.addAttribute("refid", "Base_Column_List");
-            select.addText(String.format("from %s where 1=1", tableName));
+            select.addText(String.format(" from %s where 1=1", tableName));
 
             include = select.addElement("include");
             include.addAttribute("refid", "listWhereSQL");
@@ -406,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)
@@ -431,6 +530,8 @@
     String attribute;
     String column;
     String type;
+    boolean mainKey;
+    int length;
 
     public AttributeColumnMap(String attribute, String column, String type) {
         this.attribute = attribute;
@@ -438,6 +539,7 @@
         this.type = type;
     }
 
+
     public AttributeColumnMap() {
 
     }

--
Gitblit v1.8.0