提交 06ae5409 authored 作者: chenshiying's avatar chenshiying

[新增] 生成sql

上级 1a3a8041
package com.loit.common.script;
import com.loit.common.script.dto.BuildEnvEnum;
import com.loit.common.script.dto.SqlDDLDataDTO;
import com.loit.common.utils.ListUtil;
import com.loit.common.utils.StringUtils;
import com.loit.common.utils.excel.ImportExcel;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.entity.ContentType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@Slf4j
public class GeneratorSqlDdlScript {
protected static String root_path = "F:\\9Git140\\loit-build-common\\loit-build-component\\loit-build-deploy-env\\src\\main\\resources\\bin";
/**
* 构建环境 生产或者 准生产
* TODO 构建前需修改环境
*/
private static BuildEnvEnum buildEnvEnum = BuildEnvEnum.PRE_PRODUCT;
protected static String root_path_jenkins_full = root_path + "\\jenkinsDeploy";
public static void main(String[] args) {
try {
//String filePathStr = Thread.currentThread().getContextClassLoader().getResource("deployInfo.xlsx").getPath();
String filePathStr = "F:\\9Git140\\loit-build-common\\loit-build-component\\loit-build-deploy-env\\src\\main\\resources\\createSql.xlsx";
File pdfFile = new File(filePathStr);
FileInputStream fileInputStream = new FileInputStream(pdfFile);
MultipartFile multipartFile = new MockMultipartFile(pdfFile.getName(), pdfFile.getName(),
ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
ImportExcel ei = new ImportExcel(multipartFile, 0, 0);
List<SqlDDLDataDTO> sqlDDLDataDTOInitList = ei.getDataList(SqlDDLDataDTO.class);
if (ListUtil.isEmpty(sqlDDLDataDTOInitList)) {
return;
}
String tableChineseName = "";
String tableEnglishName = "";
// 是否自增
boolean autoIncrement = false;
List<SqlDDLDataDTO> ddlInfoList = new ArrayList<>();
for (SqlDDLDataDTO grayVersionDataDTO : sqlDDLDataDTOInitList) {
String fieldName = grayVersionDataDTO.getFieldName();
if (StringUtils.isEmpty(fieldName)) {
continue;
}
ddlInfoList.add(grayVersionDataDTO);
if (StringUtils.isNotEmpty(grayVersionDataDTO.getTableChineseName())) {
tableChineseName = grayVersionDataDTO.getTableChineseName();
}
if (StringUtils.isNotEmpty(grayVersionDataDTO.getTableEnglishName())) {
tableEnglishName = grayVersionDataDTO.getTableEnglishName();
}
if (StringUtils.isNotEmpty(grayVersionDataDTO.getAutoIncrement())) {
autoIncrement = "Y".equals(grayVersionDataDTO.getAutoIncrement());
}
}
StringBuffer ddl = new StringBuffer();
ddl.append("CREATE TABLE " + "`" + tableEnglishName + "` (" + "\r\n");
Iterator<SqlDDLDataDTO> iterator = ddlInfoList.iterator();
while (iterator.hasNext()) {
SqlDDLDataDTO sqlDDLDataDTO = iterator.next();
String fieldName = sqlDDLDataDTO.getFieldName();
String fieldType = sqlDDLDataDTO.getFieldType();
String fieldLength = sqlDDLDataDTO.getFieldLength();
if(StringUtils.isEmpty(fieldLength)) {
fieldLength = "0";
}
String fieldComment = sqlDDLDataDTO.getFieldComment();
String ifKey = sqlDDLDataDTO.getIfKey();
String ifnullValue = sqlDDLDataDTO.getIfnullValue();
ddl.append(
"`" + fieldName + "` "
+ fieldType
+ (!"0".equals(fieldLength) ? "(" + fieldLength + ")" : "")
+ ("Y".equals(autoIncrement) ? " PRIMARY KEY " : "")
+ ("Y".equals(autoIncrement) ? " AUTO_INCREMENT " : "")
+ ("N".equals(ifnullValue) ? " NOT NULL " : " DEFAULT NULL")
+ " COMMENT '" + fieldComment + "'"
+ (iterator.hasNext() ? ",\r\n" : "\r\n")
);
// if ("Y".equals(ifKey)) {
// autoIncrement = true;
// }
}
if (ddl.toString().endsWith(",\r\n")) {
ddl = ddl.deleteCharAt(ddl.length() - 3);
// ddl.append("\r\n");
}
ddl.append(") ENGINE=InnoDB " + (autoIncrement ? "AUTO_INCREMENT=1" : "") + " DEFAULT CHARSET=utf8 "
+ (!"".equals(tableChineseName) ? "COMMENT = '" + tableChineseName + "'" : "") + ";\r\n");
ddl.append("-- --------------------------------------------------------------------------------\r\n");
System.out.println(ddl.toString());
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}
package com.loit.common.script.dto;
import com.loit.common.utils.excel.annotation.ExcelField;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
@ApiModel(value = "SqlDDLDataDTO", description = "生成建表sqlDTO")
public class SqlDDLDataDTO implements Serializable {
private static final long serialVersionUID = -6587921299183759035L;
@ApiModelProperty(value = "表中文名")
@ExcelField(title = "表中文名", sort = 1)
private String tableChineseName;
@ApiModelProperty(value = "表英文名")
@ExcelField(title = "表英文名", sort = 2)
private String tableEnglishName;
@ApiModelProperty(value = "字段注释")
@ExcelField(title = "字段注释", sort = 3)
private String fieldComment;
@ApiModelProperty(value = "字段名")
@ExcelField(title = "字段名", sort = 4)
private String fieldName;
@ApiModelProperty(value = "字段类型")
@ExcelField(title = "字段类型", sort = 5)
private String fieldType;
@ApiModelProperty(value = "字段长度")
@ExcelField(title = "字段长度", sort = 6)
private String fieldLength;
@ApiModelProperty(value = "是否未空")
@ExcelField(title = "是否未空", sort = 7)
private String ifnullValue;
@ApiModelProperty(value = "是否主键")
@ExcelField(title = "是否主键", sort = 8)
private String ifKey;
@ApiModelProperty(value = "是否自动递增")
@ExcelField(title = "是否自动递增", sort = 9)
private String autoIncrement;
public String getTableEnglishName() {
return tableEnglishName;
}
public void setTableEnglishName(String tableEnglishName) {
this.tableEnglishName = tableEnglishName;
}
public String getTableChineseName() {
return tableChineseName;
}
public void setTableChineseName(String tableChineseName) {
this.tableChineseName = tableChineseName;
}
public String getFieldComment() {
return fieldComment;
}
public void setFieldComment(String fieldComment) {
this.fieldComment = fieldComment;
}
public String getFieldName() {
return fieldName;
}
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
public String getFieldType() {
return fieldType;
}
public void setFieldType(String fieldType) {
this.fieldType = fieldType;
}
public String getFieldLength() {
return fieldLength;
}
public void setFieldLength(String fieldLength) {
this.fieldLength = fieldLength;
}
public String getIfnullValue() {
return ifnullValue;
}
public void setIfnullValue(String ifnullValue) {
this.ifnullValue = ifnullValue;
}
public String getIfKey() {
return ifKey;
}
public void setIfKey(String ifKey) {
this.ifKey = ifKey;
}
public String getAutoIncrement() {
return autoIncrement;
}
public void setAutoIncrement(String autoIncrement) {
this.autoIncrement = autoIncrement;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论