package cn.flightfeather.supervision.infrastructure.utils; import cn.flightfeather.supervision.domain.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(DeviceInfo.class, null); createTable(HWFile.class, null); createTable(HWRecord.class, null); createTable(OverallEvaluation.class, null); createTable(Punishment.class, null); createTable(VehicleBaseInfo.class, null); createTable(VOCHourValue.class, null); // List> 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 = '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; } }