feiyu02
2024-08-13 b8cc591541b88dd2bb93f111f8e8075842dce7ca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package cn.flightfeather.supervision.common.utils;
 
import cn.flightfeather.supervision.domain.ds1.entity.*;
 
import javax.persistence.Column;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlElement;
import java.io.*;
import java.lang.reflect.Field;
 
 
public class CreateTable {
 
 
 
 
    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        createTable(NightConstruction.class, null);
        createTable(SceneConstructionSite.class, null);
        createTable(SceneDevice.class, null);
        createTable(SceneMixingPlant.class, null);
        createTable(SceneStorageYard.class, null);
        createTable(SceneWharf.class, null);
//        List<Class<?>> class1 = PackageUtil.getClass("com.flightfeather.sewage.domain.entity", true);
//        for (Class<?> class2 : class1) {
//            createTable(class2, null);
//        }
    }
    public static void createTable(Class obj,String tableName) throws IOException{
 
 
 
 
        Field[] fields = null;
        fields = obj.getDeclaredFields();
        Class annotationType = null;
        Object param = null;
        String column = null;
        XmlElement xmlElement = null;
        StringBuilder sb = null;
        sb = new StringBuilder(50);
        if(tableName==null||tableName.equals("")){
//未传表明默认用类名
            tableName = ((Table) obj.getAnnotation(Table.class)).name();
            tableName = tableName.substring(tableName.lastIndexOf(".")+1);
        }
//        sb.append("\r\ndrop table if exists  '").append(tableName).append("';\r\n");
        sb.append("create table ").append(tableName).append(" ( \r\n");
        System.out.println(tableName);
        boolean firstId = true;
        File file = null;
        int count = 0;
        for(Field f:fields){
            column = f.getAnnotation(Column.class).name();
//            column = columnToUpperCase(column);
            sb.append("").append(column).append("");
            System.out.println(column+","+f.getType());
            param = f.getType();
            String string = f.getType().toString();
            String substring = string.substring(string.lastIndexOf(".")+1);
//sb.append(column);//一般第一个是主键
            if(substring.equals("String")){
                if (firstId) {
                    sb.append(" VARCHAR(16) NOT NULL");
                } else {
                    sb.append(" VARCHAR(50)");
                }
            } else if (substring.equals("Byte")){
                sb.append(" tinyint(2)");
            } else if (substring.equals("BigDecimal")){
                sb.append(" decimal(9,6)");
            } else if (substring.equals("Integer")){
                sb.append(" int(11)");
            } else if (substring.equals("Date")){
                sb.append(" datetime");
            } else if (substring.equals("Float")){
                sb.append(" float(6,2)");
            }
            else {
                sb.append(" ").append(substring);//根据需要自行修改
            }
            if(firstId){//类型转换
                sb.append(" PRIMARY KEY");
                firstId = false;
            } else {
                sb.append(" DEFAULT NULL");
            }
//获取字段中包含fieldMeta的注解  
//2、获取属性上的所有注释
/*Annotation[] allAnnotations = f.getAnnotations();
for(Annotation an : allAnnotations){
sb.append(" COMMIT '");
xmlElement = (XmlElement)an;
annotationType = an.annotationType();
param = ((XmlElement) an).name();
System.out.println("属性【"+f.getName()+"-----的注释类型有: " + param);
sb.append(param).append("'");
}*/
            count++;
            int length = fields.length;
            if (count <length) {
                sb.append(",\n ");
            }
        }
        String sql = null;
        sql = sb.toString();
        sql = sb.substring(0, sql.length())+" )ENGINE =INNODB DEFAULT  CHARSET= utf8;\r\n";
        file = new File("WebContent/createTable/建表.txt");
        if (!file.getParentFile().exists()) {
            if (!file.getParentFile().mkdirs()) {
            }
        }
        if (!file.exists()) {
            file.createNewFile();
        }
        System.out.println("文件路径:"+file.getAbsolutePath());
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,true)));
        out.write(sql) ;
        out.flush();
        out.close() ;
 
 
    }
 
    public static String columnToUpperCase(String column) {
        char[] columnArr = column.toCharArray();
        int index = -1;
        for (int i = 1; i < columnArr.length; i++) {
            if (columnArr[i] >= 'A' && columnArr[i] <= 'Z') {
                index = i;
                break;
            }
        }
        if (index == -1) {
            column = column.toUpperCase();
            return column;
        }else {
            column = (column.substring(0, index)+"_"+columnToUpperCase(column.substring(index))).toUpperCase();
        }
 
        return column;
    }
 
 
 
}