Reverse entity classes generated SQL database tables

com.xq.tmall.util Package; 

Import org.slf4j.Logger;
Import org.slf4j.LoggerFactory;

Import java.io.File;
Import java.io.FileOutputStream;
Import as java.lang.reflect.Field;
Import Classes in java.util .ArrayList;
Import java.util.List;

public class SqlGenerator {
Private static Logger Logger = Final LoggerFactory.getLogger (SqlGenerator.class);

{public static void main (String [] args)
// package on the disk where the entity class absolute path
String packageName = "E: / the Java / resourcesproject / Micro / 925 / Tmall_demo / src / main / the Java / COM / XQ / Tmall / the Entity";
// generate sql folders
String filePath = "E: / create / ";
path item entity class //
String prefix =" com.xq.tmall.entity ".;
ClassName = String "";

the StringBuffer sqls the StringBuffer new new = ();
// get all the class name in the package
List <String> List = getAllClasses (the packageName);
for (STR String: List) {
className = prefix + str.substring ( 0, str.lastIndexOf ( "."));
String SQL = generateSql (className, filePath);
sqls.append (SQL);
}
System.out.println (sqls.toString ());
StringToSql (sqls.toString () , filePath + "report.sql");

}
/ **
* built according to the entity classes generated table statement
* @author
* @date 2018 Nian 4 Yue 11 Ri
* @param className full class name
* @param filePath disk path such as: d : / Workspace /
* /
public static String generateSql(String className,String filePath){
try {
Class<?> clz = Class.forName(className);
className = clz.getSimpleName();
Field[] fields = clz.getDeclaredFields();
StringBuffer column = new StringBuffer();
String varchar = " varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,";
for (Field f : fields) {
column.append(" \n `"+f.getName()+"`").append(varchar);
}
StringBuffer sql = new StringBuffer();
sql.append("\n DROP TABLE IF EXISTS `"+className+"`; ")
.append(" \n CREATE TABLE `"+className+"` (")
.append ( "\ n-id`` int (. 11) the AUTO_INCREMENT the NOT NULL, ")
.append (" \ n-"+ column)
.append (" \ n-a PRIMARY KEY ( `id`) the USING BTREE,")
.append ( "\ n-id` the INDEX` ( `id`) BTREE the USING")
.append ( "\ n-). 1 the InnoDB the CHARACTER the SET ENGINE = = = UTF8 the COLLATE utf8_general_ci the AUTO_INCREMENT =;");
return sql.toString ();
} the catch ( E a ClassNotFoundException) {
logger.debug ( "class not found!");
return null;
}

}

/ **
* Get the name of all the classes in the package, similar to the results obtained xxx.java
* @author
* 2018 years @date April 11
* @param packageName
* @return
* /
public static List<String> getAllClasses(String packageName){
List<String> classList = new ArrayList<String>();
String className="";
File f = new File(packageName);
if(f.exists() && f.isDirectory()){
File[] files = f.listFiles();
for (File file : files) {
className = file.getName();
classList.add(className);
}
return classList;
}else{
logger.debug("包路径未找到!");
return null;
}
}
/**
* 将string 写入sql文件
* @author
* @date 2018年4月11日
* @param str
@Param path *
* /
public static void StringToSql (STR String, String path) {
byte [] = sourceByte str.getBytes ();
IF (null = sourceByte!) {
The try {
File File = new new File (path); // file path (path + filename)
(! File.Exists ()) {// IF the file does not exist, create a file, create a directory
file dir = new new file (file.getParent ());
dir.mkdirs ();
file .createNewFile ();
}
a FileOutputStream the outStream new new = a FileOutputStream (file); // file output stream for writing data to files
outStream.write (sourceByte);
outStream.flush ();
outStream.close (); // close file output flow
System.out.println ( "successful generation");
} the catch (Exception E) {
e.printStackTrace ();
}
}
}
}

Guess you like

Origin www.cnblogs.com/cjeandailynotes/p/11584306.html