根据JAVA实体⽣成SQL建表语句根据JAVA实体⽣成SQL建表语句
以mysql为例:oracle或SQL Server修改拼接字符串即可。ls;
slf4j.Slf4j;
构造函数和析构函数均不能被说明为虚函数import java.io.File;
import java.io.FileOutputStream;
import flect.Field;
import java.util.HashMap;
import java.util.Map;
/**
* @Title SqlGenerator
* @Description 根据JAVA实体⽣成SQL建表语句⼯具
* @Copyright: 版权所有 (c) 2018 - 2019
mysql语句转oracle* @Company: wt
* @Author root
* @Version 1.0.0
* @Create 19-4-1 下午4:22
*/
@Slf4j
public class SqlGenerator {
public static Map<String, String> property2SqlColumnMap =new HashMap<>();
static{
property2SqlColumnMap.put("integer","INT");
property2SqlColumnMap.put("short","tinyint");
新浪博客为什么停止服务property2SqlColumnMap.put("long","bigint");
property2SqlColumnMap.put("bigdecimal","decimal(19,2)");
property2SqlColumnMap.put("double","double precision not null");
property2SqlColumnMap.put("float","float");
property2SqlColumnMap.put("boolean","bit");
property2SqlColumnMap.put("timestamp","datetime");
property2SqlColumnMap.put("date","datetime");
property2SqlColumnMap.put("string","VARCHAR(500)");
}
public static String generateSql(String className,String tableName,String primaryKey,String filePath){ try{
Class<?> clz = Class.forName(className);
className = SimpleName();
Field[] fields = DeclaredFields();
StringBuffer column =new StringBuffer();
for(Field f : fields){
Name().equals(primaryKey)){
continue;
}
//column.append(" \n `"+f.getName()+"`").append(varchar);
column.append(getColumnSql(f));
}
String sqlPrimaryKey =StringUtils.camelToUnderline(primaryKey).toUpperCase();
StringBuffer sql =new StringBuffer();
sql.append("\n DROP TABLE IF EXISTS `"+tableName+"`; ")
.append(" \n CREATE TABLE `"+tableName+"`  (")
.append(" \n `"+sqlPrimaryKey+"` bigint(20) NOT NULL AUTO_INCREMENT,")
.append(" \n "+column)
.append(" \n PRIMARY KEY (`"+sqlPrimaryKey+"`)")
.append(" \n ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci;");            String sqlText = String();
StringToSql(sqlText,filePath);rasher
return sqlText;
}catch(ClassNotFoundException e){
log.debug("SQL⽣成异常:",e);
return null;
}
}
private static String getColumnSql(Field field){
String tpl ="\n `%s` %s DEFAULT NULL,";
String typeName = Type().getSimpleName().toLowerCase();
String sqlType = (typeName);
if(sqlType ==null|| sqlType.isEmpty()){
log.Name()+":"+Type().getName()+" 需要单独创建表");
return"";
}
String column =StringUtils.Name()).toUpperCase();
String sql = String.format(tpl,UpperCase());
return sql;
}java11安装教程
private 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){
log.debug("保存SQL⽂件异常:",e);
java无法安装显示脚本错误
}
}
}
}
注:复杂属性将不会⾃动⽣成,完成后需⼿动添加。

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