GenerateCode.java
6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.taover.codegenerate.bazhuayun;
import java.io.File;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import com.taover.codegenerate.bazhuayun.model.ApiModel;
import com.taover.codegenerate.db.TableColumn;
import com.taover.codegenerate.db.Tools;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class GenerateCode {
public static void main(String args[]){
try {
generate("com.taover.wxorder", "ALL", "D:"+File.separator+"dblist", "127.0.0.1", "3306", "tylife", "lexi365", "8zyun_wxorder");
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection(String host, String port, String user, String password, String dbName) {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
Properties prop = new Properties();
prop.setProperty("user", user);
prop.setProperty("password", password);
prop.setProperty("remarks", "true");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/"+dbName, prop);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void generate(String basePackage, String tableNameStr, String srcPath, String host, String port, String user, String password, String dbName) throws Exception{
//取数据库连接
Connection conn = getConnection(host, port, user, password, dbName);
//初始化要生成的表列表
List<String> tableNameList = new ArrayList<String>();
if ("ALL".equalsIgnoreCase(tableNameStr) ) {
String sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '"+dbName+"'";
ResultSet rs = Tools.executeQuery(Tools.createStmt(conn), sql);
if (null == rs) return ;
try {
while (rs.next()) {
tableNameList.add(rs.getString("Table_name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
Tools.close(rs);
} else {
String[] TableNameArr = tableNameStr.split(",");
for (int i = 0; i < TableNameArr.length; i++)
tableNameList.add(TableNameArr[i]);
}
for (int i = 0; i < tableNameList.size(); i++) {
String tableNameCurr = (String) tableNameList.get(i);
//取得表中所有列
Vector<TableColumn> columnsInTable = TableColumn.GetTableInfo(conn, dbName, tableNameCurr, "%", "mysql");
if (columnsInTable == null || columnsInTable.size() == 0) {
System.out.println(tableNameCurr + " : 生成失败,得不到表中列明");
continue;
}
TableColumn pkcolum = TableColumn.getPKColum(columnsInTable);
if (pkcolum == null) {
System.out.println(tableNameCurr + " : 生成失败,未设置主键");
continue;
}
ApiModel apiModel = new ApiModel(dbName, tableNameCurr, columnsInTable, "1.0.0", basePackage);
generateDto(srcPath, apiModel);
generateEntity(srcPath, apiModel);
generateManageController(srcPath, apiModel);
generateV1Controller(srcPath, apiModel);
generateRepository(srcPath, apiModel);
generateService(srcPath, apiModel);
generateVo(srcPath, apiModel);
}
}
public static String renderByTemplate(String templateName, ApiModel apiModel) throws Exception{
Configuration cfg = new Configuration();
//String comPath = GenerateCode.class.getResource("/").getPath();
//File f = new File(comPath+File.separator+"com"+File.separator+"taover"+File.separator+"codegenerate"+File.separator+"bazhuayun"+File.separator+"template");
//cfg.setDirectoryForTemplateLoading(f);
cfg.setClassForTemplateLoading(GenerateCode.class, "/template/bazhuayun");
Template template = cfg.getTemplate(templateName);
StringWriter out = new StringWriter();
template.process(apiModel, out);
return out.toString();
}
public static void generateDto(String srcPath, ApiModel apiModel) throws Exception{
String modelPath = apiModel.getBasePackage().replace(".", File.separator);
String filePath = srcPath+File.separator+modelPath+File.separator+"bean"+File.separator+"dto";
Tools.createFile(filePath, apiModel.getDtoClassSimpleName()+".java", renderByTemplate("DtoTemplate.ftl", apiModel), false);
}
public static void generateEntity(String srcPath, ApiModel apiModel) throws Exception{
String modelPath = apiModel.getBasePackage().replace(".", File.separator);
String filePath = srcPath+File.separator+modelPath+File.separator+"bean"+File.separator+"entity";
Tools.createFile(filePath, apiModel.getEntityClassSimpleName()+".java", renderByTemplate("EntityTemplate.ftl", apiModel), false);
}
public static void generateManageController(String srcPath, ApiModel apiModel) throws Exception{
String modelPath = apiModel.getBasePackage().replace(".", File.separator);
String filePath = srcPath+File.separator+modelPath+File.separator+"controller"+File.separator+"manage";
Tools.createFile(filePath, apiModel.getControllerClassName()+".java", renderByTemplate("ManageControllerTemplate.ftl", apiModel), false);
}
public static void generateV1Controller(String srcPath, ApiModel apiModel) throws Exception{
String modelPath = apiModel.getBasePackage().replace(".", File.separator);
String filePath = srcPath+File.separator+modelPath+File.separator+"controller"+File.separator+"v1";
Tools.createFile(filePath, apiModel.getControllerClassName()+".java", renderByTemplate("V1ControllerTemplate.ftl", apiModel), false);
}
public static void generateRepository(String srcPath, ApiModel apiModel) throws Exception{
String modelPath = apiModel.getBasePackage().replace(".", File.separator);
String filePath = srcPath+File.separator+modelPath+File.separator+"repository";
Tools.createFile(filePath, apiModel.getRepositoryClassSimpleName()+".java", renderByTemplate("RepositoryTemplate.ftl", apiModel), false);
}
public static void generateService(String srcPath, ApiModel apiModel) throws Exception{
String modelPath = apiModel.getBasePackage().replace(".", File.separator);
String filePath = srcPath+File.separator+modelPath+File.separator+"service";
Tools.createFile(filePath, apiModel.getServiceClassSimpleName()+".java", renderByTemplate("ServiceTemplate.ftl", apiModel), false);
String fileImplPath = srcPath+File.separator+modelPath+File.separator+"service"+File.separator+"impl";
Tools.createFile(fileImplPath, apiModel.getServiceImplClassSimpleName()+".java", renderByTemplate("ServiceImplTemplate.ftl", apiModel), false);
}
public static void generateVo(String srcPath, ApiModel apiModel) throws Exception{
String modelPath = apiModel.getBasePackage().replace(".", File.separator);
String filePath = srcPath+File.separator+modelPath+File.separator+"bean"+File.separator+"vo";
Tools.createFile(filePath, apiModel.getVoClassSimpleName()+".java", renderByTemplate("VoTemplate.ftl", apiModel), false);
}
}