最近有个事情比较搞笑.
客户端研发比较托大.测试环境的数据录入,他们居然不管.让需求录入数据.
需求也很苦闷.
正所谓当仁不让,这点事情对于本猫来说不算困难.
使用freemaker 2.3.23
(2.2.8这个版本会出现各种奇怪的错误)
JAVA代码
主要是获取数据库中表的元数据,包括所有的字段名称和类型,以及主键的信息.删除和更新要依靠主键信息.
-
package org.sqlserver;
-
-
import java.io.File;
-
import java.io.FileWriter;
-
import java.io.IOException;
-
import java.sql.Connection;
-
import java.sql.DatabaseMetaData;
-
import java.sql.DriverManager;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
import java.util.List;
-
import java.util.Map;
-
-
import freemarker.template.Configuration;
-
import freemarker.template.Template;
-
import freemarker.template.TemplateException;
-
-
public class Main {
-
-
public static void main(String[] args) throws IOException, TemplateException, SQLException, ClassNotFoundException {
-
-
String catalog = "boss";
-
String schema = "dbo";
-
String table = "t_group_effect_range_fireworks";
-
String basepackage = "org.sqlserver";
-
String className = "EffectFireWorks";
-
-
String basepath = Main.class.getClass().getResource("/").getPath();
-
basepath = basepath.replaceAll("build/classes/", "");
-
-
Template javaTemp = getTemplate("ManagerTemplate.ftl");
-
Template htmlTemp = getTemplate("HTMLTemplate.ftl");
-
List list = getMetadata(catalog, schema, table);
-
List primaryKey = getPrimaryKey(list);
-
Map rootMap = new HashMap();
-
rootMap.put("package", basepackage + "." + className.toLowerCase());
-
rootMap.put("classname", className);
-
rootMap.put("tablename", catalog + "." + schema + "." + table);
-
rootMap.put("primaryKey", primaryKey);
-
rootMap.put("keys", list);
-
String targetJavaDir = basepath + "src/" + basepackage.replaceAll("\\.", "/") + "\\" + className.toLowerCase()
-
+ "/";
-
new File(targetJavaDir).mkdirs();
-
FileWriter writer = new FileWriter(targetJavaDir + className + ".java");
-
javaTemp.process(rootMap, writer);
-
writer.flush();
-
writer.close();
-
-
writer = new FileWriter(basepath + "WebContent/" + className.toLowerCase() + ".html");
-
htmlTemp.process(rootMap, writer);
-
writer.flush();
-
writer.close();
-
}
-
-
public static Template getTemplate(String name) throws IOException {
-
Configuration cfg = new Configuration();
-
cfg.setDirectoryForTemplateLoading(new File(Main.class.getClass().getResource("/").getPath()));
-
Template temp = cfg.getTemplate(name);
-
temp.setEncoding("utf-8");
-
return temp;
-
}
-
-
private static List getPrimaryKey(List list) {
-
List primaryKey = new ArrayList();
-
for (Column c : list) {
-
if (c.isPrimaryKey()) {
-
primaryKey.add(c);
-
}
-
}
-
return primaryKey;
-
}
-
-
private static List getMetadata(String catalog, String schema, String tableName)
-
throws ClassNotFoundException, SQLException {
-
Connection con = getConnection();
-
DatabaseMetaData m = con.getMetaData();
-
-
ResultSet rs = m.getColumns(catalog, schema, tableName, null);
-
List list = new ArrayList();
-
while (rs.next()) {
-
Column col = new Column();
-
col.setName(rs.getString("COLUMN_NAME"));
-
col.setType(rs.getString("TYPE_NAME"));
-
list.add(col);
-
}
-
rs.close();
-
rs = m.getPrimaryKeys(catalog, schema, tableName);
-
while (rs.next()) {
-
String primarykeyColumn = rs.getString("COLUMN_NAME");
-
for (Column col : list) {
-
if (primarykeyColumn.equals(col.getName())) {
-
col.setPrimaryKey(true);
-
}
-
}
-
}
-
rs.close();
-
return list;
-
}
-
-
private static Connection getConnection() throws SQLException, ClassNotFoundException {
-
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
-
String dbURL = "jdbc:sqlserver://127.0.0.1:3306;DatabaseName=roomdb";
-
String userName = "sa";
-
String userPwd = "sa";
-
Class.forName(driverName);
-
Connection dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
-
dbConn.setAutoCommit(false);
-
return dbConn;
-
}
-
}
ITPUB对于泛型显示不出来,所以上面的代码,会报错.
需要修改为List《Column》
封装元数据的类
-
package org.sqlserver;
-
-
public class Column {
-
private String name = "";
-
private String type = "";
-
private boolean isPrimaryKey = false;
-
-
@Override
-
public String toString() {
-
return this.name + "," + this.type + "," + this.isPrimaryKey;
-
}
-
-
public String getName() {
-
return name;
-
}
-
-
public void setName(String name) {
-
this.name = name;
-
}
-
-
public String getType() {
-
return type;
-
}
-
-
public void setType(String type) {
-
this.type = type;
-
}
-
-
public boolean isPrimaryKey() {
-
return isPrimaryKey;
-
}
-
-
public void setPrimaryKey(boolean isPrimaryKey) {
-
this.isPrimaryKey = isPrimaryKey;
-
}
-
-
}
Servlet的模板 ManagerTemplate.ftl
HTML的模板 HTMLTemplate.ftl
HTMLTemplate.zip
界面使用了jQuery和EasyUI
也是第一次使用freemaker
模板生成之后,直接刷新Eclipse运行即可.
当然文字方面,可能还需要做细节的微调.
不过可以保证一个新建的表,10分钟之内,就可以有一个带简单增删改查的界面.

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-1837431/
,如需转载,请注明出处,否则将追究法律责任。