admin
2024-07-25 47e3087067abd35e6337c011f96d2338c0bb1aae
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() {
    }