0
点赞
收藏
分享

微信扫一扫

自己开发java代码生成工具


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

截图

自己开发java代码生成工具_数据库


举报

相关推荐

0 条评论