数据库实体类
java
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 分子公司基本信息变更代办
*
* @version 1.0
* @date 2023/11/21 01:01
*/
@EqualsAndHashCode(callSuper = true)
@Data
@TableName("ent_change_todo")
public class EntChangeTodoDO extends BaseDO {
/**
* id
*/
private Integer id;
/**
* 分子公司类型 (1-分公司,2-子公司)
*/
private Integer companyType;
/**
* 企业id
*/
private Long enterpriseId;
/**
* 公司名字
*/
private String companyName;
/**
* 企业类别
*/
private String companyCate;
/**
* 负责人或法人
*/
private String director;
/**
* 变更项json
*/
private String changeItemsJson;
/**
* 待办人部门名
*/
private String todoDeptName;
/**
* 待办人id
*/
private Long todoUserId;
/**
* 待办人名字
*/
private String todoUserName;
/**
* 待办状态(0-待处理, 1-同意(完成);2-驳回发起人;3-撤销)
*/
private Integer state;
/**
* 待办业务key
*/
private String todoKey;
/**
* 审批意见
*/
private String approvalComment;
}
工具类
java
import com.google.common.base.CaseFormat;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.StreamUtils;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @version 1.0
* @date 2023/11/29 16:31
*/
public class Entity2SQLTest2 {
Pattern fieldPattern = Pattern.compile("/\\**\\n\\s+\\*(\\s*(.+))\\n\\s+\\*/\\n\\s+\\w+\\s+(\\w+)\\s+(\\w+);");
Pattern tableNamePattern = Pattern.compile("TableName\\(\"(\\w+)\"\\)");
@Test
public void testSQL() {
String result = entity2SQL("tes.txt");
System.out.println(result);
}
private String entity2SQL(String entityClassPath) {
Resource resource = new ClassPathResource(entityClassPath);
String entityClassStr;
try {
InputStream inputStream = resource.getInputStream();
entityClassStr = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new IllegalStateException();
}
Matcher matcherTable = tableNamePattern.matcher(entityClassStr);
StringBuilder sqlBuilder = new StringBuilder("CREATE TABLE ");
while (matcherTable.find()) {
String tableName = matcherTable.group(1);
sqlBuilder.append('`').append(tableName).append('`').append(" (\n");
}
Matcher matcher = fieldPattern.matcher(entityClassStr);
while (matcher.find()) {
String comment = matcher.group(2);
String type = matcher.group(3);
String fieldName = matcher.group(4);
String sqlField = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, fieldName);
if (sqlField.equals("id")) {
sqlBuilder.append("`id` BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '主键id',\n");
continue;
}
sqlBuilder.append('`').append(sqlField).append('`');
if ("Integer".equals(type)) {
sqlBuilder.append(" INT ");
}
if ("Long".equals(type)) {
sqlBuilder.append(" BIGINT ");
}
if ("String".equals(type)) {
sqlBuilder.append(" VARCHAR(128) ");
}
if ("LocalDate".equals(type)) {
sqlBuilder.append(" DATE ");
}
if ("LocalDateTime".equals(type) || "Date".equals(type)) {
sqlBuilder.append(" DATETIME ");
}
if ("LocalTime".equals(type)) {
sqlBuilder.append(" TIME ");
}
if ("Boolean".equals(type) || "boolean".equals(type)) {
sqlBuilder.append(" TINYINT ");
}
sqlBuilder.append("DEFAULT NULL");
sqlBuilder.append(" COMMENT ").append('\'').append(comment).append("',\n");
}
int startDel = sqlBuilder.length() - 2;
int endDel = sqlBuilder.length();
sqlBuilder.delete(startDel, endDel);
sqlBuilder.append(");");
return sqlBuilder.toString();
}
}
输出结果
sql
CREATE TABLE `ent_change_todo` (
`id` BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '主键id',
`company_type` INT DEFAULT NULL COMMENT '分子公司类型 (1-分公司,2-子公司)',
`enterprise_id` BIGINT DEFAULT NULL COMMENT '企业id',
`company_name` VARCHAR(128) DEFAULT NULL COMMENT '公司名字',
`company_cate` VARCHAR(128) DEFAULT NULL COMMENT '企业类别',
`director` VARCHAR(128) DEFAULT NULL COMMENT '负责人或法人',
`change_items_json` VARCHAR(128) DEFAULT NULL COMMENT '变更项json',
`todo_dept_name` VARCHAR(128) DEFAULT NULL COMMENT '待办人部门名',
`todo_user_id` BIGINT DEFAULT NULL COMMENT '待办人id',
`todo_user_name` VARCHAR(128) DEFAULT NULL COMMENT '待办人名字',
`state` INT DEFAULT NULL COMMENT '待办状态(0-待处理, 1-同意(完成);2-驳回发起人;3-撤销)',
`todo_key` VARCHAR(128) DEFAULT NULL COMMENT '待办业务key',
`approval_comment` VARCHAR(128) DEFAULT NULL COMMENT '审批意见');