工具的执行逻辑如下图所示:
如上分析,得知完成代码生成器需要以下几个操作:
<dependencies>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.20</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
(1) UI页面获取的数据库配置,封装到数据库实体类中
//数据库实体类
public class DataBase {
private static String mysqlUrl = "jdbc:mysql://[ip]:[port]/[db]?
useUnicode=true&characterEncoding=UTF8";
private static String oracleUrl = "jdbc:oracle:thin:@[ip]:[port]:[db]";
private String dbType;//数据库类型
private String driver;
private String userName;
private String passWord;
private String url;
public DataBase() {}
public DataBase(String dbType) {
this(dbType,"127.0.0.1","3306","");
}
public DataBase(String dbType,String db) {
this(dbType,"127.0.0.1","3306",db);
}
public DataBase(String dbType,String ip,String port,String db) {
this.dbType = dbType;
if("MYSQL".endsWith(dbType.toUpperCase())) {
this.driver="com.mysql.jdbc.Driver";
this.url=mysqlUrl.replace("[ip]",ip).replace("[port]",port).replace("
[db]",db);
}else{
this.driver="oracle.jdbc.driver.OracleDriver";
this.url=oracleUrl.replace("[ip]",ip).replace("[port]",port).replace("
[db]",db);
}
}
public String getDbType() {
return dbType;
}
public void setDbType(String dbType) {
this.dbType = dbType;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
(2) UI页面获取的自动生成工程配置,封装到设置实体类中
public class Settings {
private String project="example";
private String pPackage="com.example.demo";
private String projectComment;
private String author;
private String path1="com";
private String path2="example";
private String path3="demo";
private String pathAll;
public Settings(String project, String pPackage, String projectComment, String
author) {
if(StringHelper.isNotBlank(project)) {
this.project = project;
}
if(StringHelper.isNotBlank(pPackage)) {
this.pPackage = pPackage;
}
this.projectComment = projectComment;
this.author = author;
String[] paths = pPackage.split("\\.");
path1 = paths[0];
path2 = paths.length>1?paths[1]:path2;
path3 = paths.length>2?paths[2]:path3;
pathAll = pPackage.replaceAll(".","/");
}
public Map<String, Object> getSettingMap(){
Map<String, Object> map = new HashMap<>();
Field[] declaredFields = Settings.class.getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
try{
map.put(field.getName(), field.get(this));
}catch (Exception e){}
}
return map;
}
public String getProject() {
return project;
}
public void setProject(String project) {
this.project = project;
}
public String getpPackage() {
return pPackage;
}
public void setpPackage(String pPackage) {
this.pPackage = pPackage;
}
public String getProjectComment() {
return projectComment;
}
public void setProjectComment(String projectComment) {
this.projectComment = projectComment;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPath1() {
return path1;
}
public void setPath1(String path1) {
this.path1 = path1;
}
public String getPath2() {
return path2;
}
public void setPath2(String path2) {
this.path2 = path2;
}
public String getPath3() {
return path3;
}
public void setPath3(String path3) {
this.path3 = path3;
}
public String getPathAll() {
return pathAll;
}
public void setPathAll(String pathAll) {
this.pathAll = pathAll;
}
}
(3) 将查询数据表的元数据封装到Table实体类
public class Table {
private String name;//表名称
private String name2;//处理后的表名称
private String comment;//介绍
private String key;// 主键列
private List<Column> columns;//列集合
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getName2() {
return name2;
}
public void setName2(String name2) {
this.name2 = name2;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public List<Column> getColumns() {
return columns;
}
public void setColumns(List<Column> columns) {
this.columns = columns;
}
}
(4)将查询数据字段的元数据封装到Column实体类
/**
* 列对象
*/
public class Column {
private String columnName;//列名称
private String columnName2;//列名称(处理后的列名称)
private String columnType;//列类型
private String columnDbType;//列数据库类型
private String columnComment;//列备注D
private String columnKey;//是否是主键
public String getColumnName() {
return columnName;
}
public void setColumnName(String columnName) {
this.columnName = columnName;
}
public String getColumnName2() {
return columnName2;
}
public void setColumnName2(String columnName2) {
this.columnName2 = columnName2;
}
public String getColumnType() {
return columnType;
}
public void setColumnType(String columnType) {
this.columnType = columnType;
}
public String getColumnDbType() {
return columnDbType;
}
public void setColumnDbType(String columnDbType) {
this.columnDbType = columnDbType;
}
public String getColumnComment() {
return columnComment;
}
public void setColumnComment(String columnComment) {
this.columnComment = columnComment;
}
public String getColumnKey() {
return columnKey;
}
public void setColumnKey(String columnKey) {
this.columnKey = columnKey;
}
}
导入资料中提供的工具类
FileUtils:文件处理工具类:
PropertiesMaps:
StringHelper: