博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自己开发java代码生成工具
阅读量:2023 次
发布时间:2019-04-28

本文共 3594 字,大约阅读时间需要 11 分钟。

1. 背景

在公司做项目,特别是业务系统的时候,大量的表单和增删改查,而且后台Ui经常用一些easyui等jquery ui框架,数据库一张表对应一个domian.表字段对应domain的属性,也对应这前台js json等数据。这样后台代码和json都是有规律可循。大量的复制粘贴 很枯燥麻烦,因此决定做一个代码自动生成工具。

1. 界面

2.用到的技术与核心代码

 2.1 jdbc DatabaseMetaData(连接数据库 读取表结构)

public Connection getConnection() {

/** 声明Connection连接对象 */
Connection conn = null;
try {
/** 使用Class.forName()方法自动创建这个驱动程序的实例且自动调用DriverManager来注册它 */
Class.forName(DB_DRIVER);
/** 通过DriverManager的getConnection()方法获取数据库连接 */
Properties props = new Properties();
props.put("user", DB_USERNAME);
props.put("password", DB_PASSWORD);
props.put("remarksReporting", "true");//设置能读取到 column comment
conn = DriverManager.getConnection(DB_URL, props);
} catch (Exception ex) {
pane.setText("<span style=\"color:red;\">error:" + ex.toString()
+ "查看控制台</span>");
ex.printStackTrace();
}
return conn;
}

public HashMap<String, Object> loadDbTableInfo(String table) {

HashMap<String, Object> root = new HashMap<String, Object>();
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
dbCon = new DBConnection(this.dbUrl, this.dbDriver, this.dbUser,
this.dbPwd, this.pane);
Connection con = dbCon.getConnection();
ResultSet rs;
try {
DatabaseMetaData dbmd = con.getMetaData();
rs = dbmd.getColumns(null, "%", table, "%");
while (rs.next()) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("COLUMN_NAME", rs.getString("COLUMN_NAME")
.toLowerCase());
map.put("REMARKS", rs.getString("REMARKS"));
map.put("TYPE_NAME", rs.getString("TYPE_NAME"));
list.add(map);
}
rs = dbmd.getTables(null, "%", table, new String[] { "TABLE" });
if (rs.next()) {
root.put("tableComment", rs.getString("REMARKS"));// table
// comment
}
root.put("list", list);
root.put("title", tableName);// tableName
root.put("domainName", domainName);
return root;
} catch (Exception e) {
pane.append("error:" + e.toString() + "查看控制台\n");
e.printStackTrace();
} finally {
dbCon.closeConnection(con);
}
return root;
}

2.2 freemarker (根据订制的模版生成 文件)

/**

* 初始化
*/
public void init() {
File file = new File("bin/resource/template");
cfg = new Configuration();
try {
cfg.setDirectoryForTemplateLoading(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
pane.append("error:" + e.toString() + "查看控制台\n");
}
}

/**

* 生成新文件
* @param root
*            信息封装
* @param newFileName
*            新文件名字
* @param templeName
*            模版名字
* @param newFilePath
*            新文件路径
*/
public void startMake(HashMap<String, Object> root, String newFileName,
String templeName, String newFilePath) {
Template t;
try {
t = cfg.getTemplate(templeName);
Writer out = new OutputStreamWriter(new FileOutputStream(
newFilePath + "/" + newFileName), "utf-8");
t.process(root, out);
out.flush();
out.close();
pane.append(newFilePath + "/" + newFileName + "生成成功\n");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
pane.append("error:" + e.toString() + "查看控制台\n");
}
}
public void begin() {
HashMap<String, Object> root = this.loadDbTableInfo(tableName);
this.startMake(root, "I" + this.domainName + "Svc.java", "svc.ftl",
svcPath);// 生成svc文件
this.startMake(root, this.domainName + "Impl.java", "Impl.ftl",
implPath);// 生成Impl文件
this.startMake(root, this.domainName + "_Form.html", "Form.ftl",
pagesPath);// 生成form 页面
this.startMake(root, this.domainName + "_View.html", "View.ftl",
pagesPath);// 生成view 页面
this.startMake(root, this.domainName + ".html", "mainhtml.ftl",
pagesPath);// 生成main.html 页面
this.startMake(root, this.domainName + ".js", "js.ftl",
pagesPath);// 生成js 页面
}

2.3美化swing界面 substance 皮肤包

SubstanceLookAndFeel.setSkin(new CremeSkin());//substance skin

截图

 下载连接

你可能感兴趣的文章
禅与摩托车维修艺术语录摘抄(1)
查看>>
bash 命令执行环境
查看>>
bash 环境
查看>>
bash 作业控制
查看>>
物理内存的管理
查看>>
高效能人士的七个习惯——由内而外全面造就自己
查看>>
为什么精英都是清单控(笔记)——工作清单
查看>>
怦然心动的人生整理魔法(笔记)——物品类别整理
查看>>
让人生发生戏剧性变化的整理魔法(笔记)
查看>>
怦然心动的人生整理魔法(笔记)
查看>>
什么是“怦然心动的感觉”
查看>>
按物品类别整理的心动收纳法(笔记)
查看>>
沟通的艺术(笔记)——前言
查看>>
有效沟通的基本原则(笔记)
查看>>
演讲、演讲人、听众
查看>>
有备演讲和即兴演讲的目的(笔记)
查看>>
番茄工作图解——序(笔记)
查看>>
为何要用番茄工作法(笔记)
查看>>
番茄工作法--背景(笔记)
查看>>
番茄工作法——方法(笔记)
查看>>