通过实体类⽣成建表SQL语句实现⽅法
import java.io.File;
import java.io.FileOutputStream;
import flect.Field;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.hql.internal.ast.SqlGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Test {
private static final Logger logger = Logger(SqlGenerator.class);
public static void main(String[] args) {
//实体类所在的package在磁盘上的绝对路径
String packageName = "d:/workspace/";
//⽣成sql的⽂件夹
String filePath = "I:/create/";
//项⽬中实体类的路径
String prefix = "ity.";
String className = "";
StringBuffer sqls = new StringBuffer();
//获取包下的所有类名称
List<String> list = getAllClasses(packageName);
for (String str : list) {
className = prefix + str.substring(0, str.lastIndexOf("."));
String sql = generateSql(className, filePath);
sqls.append(sql);
}
System.out.String());
String(), filePath + "report.sql");
}
/**
* 根据实体类⽣成建表语句
* @author
* @date 2019年1⽉14⽇
* @param className 全类名
* @param filePath 磁盘路径如 : d:/workspace/
*/
public static String generateSql(String className,String filePath){
try {
Class<?> clz = Class.forName(className);
className = SimpleName();
Field[] fields = DeclaredFields();
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) NOT NULL AUTO_INCREMENT,")
.append(" \n "+column)
.append(" \n PRIMARY KEY (`id`) USING BTREE,")
.append("\n INDEX `id`(`id`) USING BTREE")
.append(" \n ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci;");            String();
} catch (ClassNotFoundException e) {
logger.debug("该类未到!");
return null;
return null;
}
}
/**
* 获取包下的所有类名称,获取的结果类似于 XXX.java
* @author
* @date 2019年1⽉14⽇
* @param packageName
* @return
*/
public static List<String> getAllClasses(String packageName){
List<String> classList = new ArrayList<String>();
String className="";
File f = new File(packageName);
ists() && f.isDirectory()){
File[] files = f.listFiles();
for (File file : files) {
className = Name();
classList.add(className);
}
return classList;
}else{
logger.debug("包路径未到!");
return null;
}
}
/**
* 将string 写⼊sql⽂件
* @author
* @date 2019年1⽉14⽇
* @param str
* @param path
*/
public static void StringToSql(String str,String path){
byte[] sourceByte = Bytes();
if(null != sourceByte){
try {
File file = new File(path);    //⽂件路径(路径+⽂件名)
if (!ists()) {  //⽂件不存在则创建⽂件,先创建⽬录
File dir = new Parent());
dir.mkdirs();
}
FileOutputStream outStream = new FileOutputStream(file);    //⽂件输出流⽤于将数据写⼊⽂件                outStream.write(sourceByte);
outStream.flush();
outStream.close();  //关闭⽂件输出流
System.out.println("⽣成成功");
} catch (Exception e) {
e.printStackTrace();
}
}
}mkdirs方法
}
⽣成的代码截图如下:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。