需求说明:公司做项目前进行需求分析,确定表结构后需要建表,如果照着表格去敲,那就太麻烦了,所以想到了自动生成SQL语句。
思路大概就是:解析Excel,拼接SQL语句,输出SQL文件。
第三方jar包:POI(解析Excel)
先建立一个maven项目。
pom依赖:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.transition</groupId> <artifactId>excel-to-sql</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.17</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans --> <dependency> <groupId>org.apache.xmlbeans</groupId> <artifactId>xmlbeans</artifactId> <version>2.6.0</version> </dependency> <!-- https://mvnrepository.com/artifact/dom4j/dom4j --> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> </dependencies> <build> <plugins> <!-- 编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
目录结构
Keys.Java
package com.transition.core.common; public class Keys { public static final String TABLE_PRE = "DROP TABLE IF EXISTS "; public static final String TABLE_SUFF = "ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT="; public static final String C_T = "CREATE TABLE "; public static final String N_N = " NOT NULL "; public static final String A_I = "AUTO_INCREMENT"; public static final String P_K = "PRIMARY KEY "; public static final String F_K = " FOREIGN KEY "; public static final String COMMENT = " COMMENT "; public static final String DEFAULT = " DEFAULT "; }
DataTable.Java
package com.transition.core.entity; import java.util.ArrayList; import java.util.List; /** * 数据库表 */ public class DataTable { private String tableName; // 数据库表名 private String comment; //数据库注释 private List<TableField> fields = new ArrayList<>(); public DataTable() { } public DataTable(String tableName, String comment) { this.tableName = tableName; this.comment = comment; } public String getTableName() { return tableName; } public void setTableName(String tableName) { this.tableName = tableName; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } public List<TableField> getFields() { return fields; } public void setFields(List<TableField> fields) { this.fields = fields; } @Override public String toString() { return "DataTable{" + "tableName='" + tableName + '\'' + ", comment='" + comment + '\'' + ", fields=" + fields + '}'; } }