实例介绍
【实例简介】读取数据库表结构 自动生成增删改查代码
【实例截图】
【核心代码】
package com.isoftstone.autocoding; import com.isoftstone.autocoding.common.Column; import com.isoftstone.autocoding.common.Table; import com.isoftstone.autocoding.utils.CamelCaseUtils; import com.isoftstone.autocoding.utils.FileHelper; import com.mysql.jdbc.StringUtils; import freemarker.template.Configuration; import freemarker.template.Template; import org.apache.log4j.Logger; import javax.swing.filechooser.FileSystemView; import java.io.*; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.text.SimpleDateFormat; import java.util.*; public class EasyCodeApplication { private Logger logger = Logger.getLogger(this.getClass()); private Properties properties; /** * 读取配置文件 * * @throws Exception */ public EasyCodeApplication() throws Exception { properties = new Properties(); String fileDir = this.getClass().getClassLoader().getResource("generator.xml").getFile(); properties.loadFromXML(new FileInputStream(fileDir)); } /** * 解析数据表 * * @param tableName * @return * @throws Exception */ public Table parseTable(String tableName) throws Exception { String driverName = properties.getProperty("jdbc.driver"); String userName = properties.getProperty("jdbc.username"); String userPwd = properties.getProperty("jdbc.password"); String dbURL = properties.getProperty("jdbc.url"); String catalog = properties.getProperty("jdbc.catalog"); String schema = properties.getProperty("jdbc.schema"); schema = schema == null ? "%" : schema; String column = "%"; logger.debug("driver>>" driverName); logger.debug("url>>" dbURL); logger.debug("name>>" userName); logger.debug("password>>" userPwd); logger.debug("catalog>>" catalog); logger.debug("schema>>" schema); Class.forName(driverName); Connection conn = java.sql.DriverManager.getConnection(dbURL, userName, userPwd); DatabaseMetaData dmd = conn.getMetaData(); ResultSet rs = dmd.getColumns(catalog, schema, tableName, column); List<Column> columns = new ArrayList<Column>(); while (rs.next()) { Column c = new Column(); c.setLabel(rs.getString("REMARKS")); String name = rs.getString("COLUMN_NAME"); c.setName(CamelCaseUtils.toCamelCase(name)); c.setDbName(name); String dbType = rs.getString("TYPE_NAME"); int columnSize = rs.getInt("COLUMN_SIZE"); if (dbType.equals("TINYINT") && columnSize > 1) { c.setType("Integer"); } else if (dbType.equals("TINYINT") && columnSize == 1) { c.setType("Boolean"); } else { String type = properties.getProperty(dbType); c.setType(type == null ? "String" : type); } c.setDbType(dbType); c.setLength(rs.getInt("COLUMN_SIZE")); c.setDecimalDigits(rs.getInt("DECIMAL_DIGITS")); c.setNullable(rs.getBoolean("NULLABLE")); columns.add(c); } List<Column> pkColumns = new ArrayList<Column>(); ResultSet pkrs = dmd.getPrimaryKeys(catalog, schema, tableName); while (pkrs.next()) { Column c = new Column(); String name = pkrs.getString("COLUMN_NAME"); c.setName(CamelCaseUtils.toCamelCase(name)); c.setDbName(name); pkColumns.add(c); } conn.close(); Table t = new Table(); String prefiex = properties.getProperty("tableRemovePrefixes"); String name = tableName; if (prefiex != null && !"".equals(prefiex)) { name = tableName.split(prefiex)[0]; } t.setName(CamelCaseUtils.toCamelCase(name)); t.setDbName(tableName); t.setColumns(columns); t.setPkColumns(pkColumns); return t; } /** * <p>Discription:[生成映射文件和实体类]</p> * Created on 2019年4月4日 * * @param tableName 要声称映射文件和实体类的表名称 * @param tableDescAndCat 表描述 * @throws Exception */ public void gen(String tableName, String tableDescAndCat, String id, String modelId) throws Exception { Configuration cfg = new Configuration(Configuration.VERSION_2_3_21); String outRoot = properties.getProperty("outRoot"); //当输出地址为null时,文件放到桌面 if (StringUtils.isNullOrEmpty(outRoot)) { File desktopDir = FileSystemView.getFileSystemView() .getHomeDirectory(); outRoot = desktopDir.getAbsolutePath() "/Desktop/EasyCodeDemo"; } String basePackage = properties.getProperty("basePackage"); //获取当前日期 SimpleDateFormat sm_date = new SimpleDateFormat("yyyy年MM月dd日"); SimpleDateFormat sm_year = new SimpleDateFormat("yyyy年"); //将首字母转为大写 StringBuffer buffer = new StringBuffer(); String namePart1 = modelId.substring(0, 1).toUpperCase(); String namePart2 = modelId.substring(1); buffer.append(namePart1 namePart2); System.out.println(buffer); Map<String, Object> root = new HashMap<String, Object>(); Table t = this.parseTable(tableName); t.setTableDesc(tableDescAndCat.split("_")[0]); root.put("table", t); root.put("className", t.getNameUpper()); root.put("classNameLower", t.getName()); root.put("primaryKey", id); root.put("modelId", modelId); root.put("modelIdFirstUpper", buffer); root.put("package", basePackage); root.put("date", sm_date.format(new Date())); root.put("year", sm_year.format(new Date())); root.put("author", "wangchun"); root.put("email", "chunwangi@isoftstone.com"); String templateDir = this.getClass().getClassLoader().getResource("templates").getPath(); File tdf = new File(templateDir); List<File> files = FileHelper.findAllFile(tdf); for (File f : files) { String parentDir = ""; if (f.getParentFile().compareTo(tdf) != 0) { parentDir = f.getParent().split("templates")[1]; } cfg.setClassForTemplateLoading(this.getClass(), "/templates" parentDir); Template template = cfg.getTemplate(f.getName()); template.setEncoding("UTF-8"); String parentFileDir = FileHelper.genFileDir(parentDir, root); parentFileDir = parentFileDir.replace(".", "/"); String file = FileHelper.genFileDir(f.getName(), root).replace(".ftl", ".java"); System.out.println(file); File newFile = FileHelper.makeFile(outRoot parentFileDir "/" file); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFile), "UTF-8")); template.process(root, out); logger.debug("已生成文件:" outRoot parentFileDir "/" file); } } public static void main(String[] args) throws Exception { long time = System.currentTimeMillis(); EasyCodeApplication g = new EasyCodeApplication(); Map<String, String> map = new HashMap<String, String>(); map.put("tr_usergroup_menu", "用户组菜单表"); Iterator<Map.Entry<String, String>> it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, String> e = it.next(); //设置数据库主键字段 g.gen(e.getKey(), e.getValue(), "id", "id"); } System.out.println("-------------------模版文件生成完毕,时间:" (System.currentTimeMillis() - time) "毫秒 ----------------!!!"); } }
标签: 代码生成器
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论