GenerateView.java
5.32 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
package com.taover.business;
import java.io.File;
import java.io.StringWriter;
import java.sql.Connection;
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.base.template.model.bussiness.ViewModel;
import com.taover.db.TableColumn;
import com.taover.db.Tools;
import com.taover.tools.StringUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class GenerateView {
public static void main(String args[]) throws SQLException{
//读取配置文件
Properties properties = Tools.getProperties();
//取数据库连接
Connection conn = Tools.getConnection(properties);
//初始化要生成的表列表
List<String> tableNameList = new ArrayList<String>();
String outputPath = properties.getProperty("OutputPath");
String schameName = properties.getProperty("SchameName");
String createSchame = properties.getProperty("CreateSchame");
String dbLikeStr = properties.getProperty("DBLikeStr");
String dbType = properties.getProperty("dbtype");
String dbPackagePath = properties.getProperty("packageName");
String tableName = properties.getProperty("TableName");
//WebRootPath读取
String webRootPath = properties.getProperty("WebRootPath");
if("mysql".equalsIgnoreCase(dbType)){
//schameName = schameName.toUpperCase();
}
if ("ALL".equalsIgnoreCase(tableName) ) {
String sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '"+schameName+"'";
ResultSet rs = Tools.executeQuery(Tools.createStmt(conn), sql);
if (null == rs)
return ;
List<String> result = new ArrayList<String>();
try {
while (rs.next()) {
tableNameList.add(rs.getString("Table_name"));
}
} catch (SQLException e) {
e.printStackTrace();
result = null;
}
Tools.close(rs);
} else {
if("mysql".equalsIgnoreCase(dbType)){
tableName = tableName.toUpperCase();
}
String[] TableNameArr = tableName.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, schameName, tableNameCurr, dbLikeStr, dbType);
if (columnsInTable == null || columnsInTable.size() == 0) {
System.out.println(tableNameCurr + " : 生成失败,得不到表中列明");
continue;
}
TableColumn pkcolum = TableColumn.getPKColum(columnsInTable);
if (pkcolum == null) {
System.out.println(tableNameCurr + " : 生成失败,未设置主键");
continue;
}
//生成view
generateView(webRootPath, tableNameCurr, columnsInTable, false);
System.out.println(tableNameCurr + " : 生成结束");
}
conn.close();
System.out.println("全部结束");
}
public static void generateView(String webRootPath, String tableName, Vector<TableColumn> columnsInTable, boolean coverWhenFileExists) {
try{
String controllerMapping = StringUtil.formatBeanNameFirstLow(tableName).toLowerCase();
ViewModel model = ViewModel.createViewModel(controllerMapping, webRootPath, columnsInTable);
File f = new File(Tools.getPath() + Constants.TEMPATE_PATH);
Configuration cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(f);
StringWriter htmlOut = new StringWriter();
Template htmlTemplate = cfg.getTemplate(Constants.TEMPATE_HTMLNAME, "UTF-8");
htmlTemplate.process(model, htmlOut);
File destFile = new File(
webRootPath+File.separator+"manage"+File.separator+"html"+File.separator+controllerMapping+".html");
if(coverWhenFileExists || !destFile.exists()){
Tools.createFile(webRootPath+File.separator+"manage"+File.separator+"html", controllerMapping+".html", htmlOut.toString());
}else{
System.out.println("文件生成:"+destFile.getName()+"文件已经存在,未覆盖!");
}
StringWriter jsOut = new StringWriter();
Template jsTemplate = cfg.getTemplate(Constants.TEMPATE_JSNAME, "UTF-8");
jsTemplate.process(model, jsOut);
File destFile2 = new File(
webRootPath+File.separator+"manage"+File.separator+"js"+File.separator+controllerMapping+".js");
if(coverWhenFileExists || !destFile2.exists()){
Tools.createFile(webRootPath+File.separator+"manage"+File.separator+"js", controllerMapping+".js", jsOut.toString());
}else{
System.out.println("文件生成:"+destFile2.getName()+"文件已经存在,未覆盖!");
}
StringWriter htmlLayuiOut = new StringWriter();
Template htmlLayuiTemplate = cfg.getTemplate(Constants.TEMPATE_HTMLLAYUINAME, "UTF-8");
htmlLayuiTemplate.process(model, htmlLayuiOut);
File destLayuiFile = new File(
webRootPath+File.separator+"views"+File.separator+controllerMapping+".html");
if(coverWhenFileExists || !destLayuiFile.exists()){
Tools.createFile(webRootPath+File.separator+"views", controllerMapping+".html", htmlLayuiOut.toString());
}else{
System.out.println("文件生成:"+destFile.getName()+"文件已经存在,未覆盖!");
}
}catch(Exception e){
e.printStackTrace();
}
}
}