/** * 生成到文件 * * @param tpl * @param model * @param replaceFile * @return */ public static String generateToFile( GenTemplate tpl, Map<String, Object> model, boolean isReplaceFile) { // 获取生成文件 String fileName = Global.getProjectPath() + File.separator + StringUtils.replaceEach( FreeMarkers.renderString(tpl.getFilePath() + "/", model), new String[] {"//", "/", "."}, new String[] {File.separator, File.separator, File.separator}) + FreeMarkers.renderString(tpl.getFileName(), model); logger.debug(" fileName === " + fileName); // 获取生成文件内容 String content = FreeMarkers.renderString(StringUtils.trimToEmpty(tpl.getContent()), model); logger.debug(" content === \r\n" + content); // 如果选择替换文件,则删除原文件 if (isReplaceFile) { FileUtils.deleteFile(fileName); } // 创建并写入文件 if (FileUtils.createFile(fileName)) { FileUtils.writeToFile(fileName, content, true); logger.debug(" file create === " + fileName); return "生成成功:" + fileName + "<br/>"; } else { logger.debug(" file extents === " + fileName); return "文件已存在:" + fileName + "<br/>"; } }
/** * 设置属性,支持自定义方言类和制定数据库的方式 <code>dialectClass</code>,自定义方言类。可以不配置这项 <ode>dbms</ode> 数据库类型,插件支持的数据库 * <code>sqlPattern</code> 需要拦截的SQL ID * * @param p 属性 */ protected void initProperties(Properties p) { Dialect dialect = null; String dbType = Global.getConfig("jdbc.type"); if ("db2".equals(dbType)) { dialect = new DB2Dialect(); } else if ("derby".equals(dbType)) { dialect = new DerbyDialect(); } else if ("h2".equals(dbType)) { dialect = new H2Dialect(); } else if ("hsql".equals(dbType)) { dialect = new HSQLDialect(); } else if ("mysql".equals(dbType)) { dialect = new MySQLDialect(); } else if ("oracle".equals(dbType)) { dialect = new OracleDialect(); } else if ("postgre".equals(dbType)) { dialect = new PostgreSQLDialect(); } else if ("mssql".equals(dbType) || "sqlserver".equals(dbType)) { dialect = new SQLServer2005Dialect(); } else if ("sybase".equals(dbType)) { dialect = new SybaseDialect(); } if (dialect == null) { throw new RuntimeException("mybatis dialect error."); } DIALECT = dialect; // _SQL_PATTERN = p.getProperty("sqlPattern"); // _SQL_PATTERN = Global.getConfig("mybatis.pagePattern"); // if (StringUtils.isEmpty(_SQL_PATTERN)) { // throw new RuntimeException("sqlPattern property is not found!"); // } }
/** * 获取数据模型 * * @param genScheme * @param genTable * @return */ public static Map<String, Object> getDataModel(GenScheme genScheme) { Map<String, Object> model = Maps.newHashMap(); model.put("packageName", StringUtils.lowerCase(genScheme.getPackageName())); model.put( "lastPackageName", StringUtils.substringAfterLast((String) model.get("packageName"), ".")); model.put("moduleName", StringUtils.lowerCase(genScheme.getModuleName())); model.put("subModuleName", StringUtils.lowerCase(genScheme.getSubModuleName())); model.put("className", StringUtils.uncapitalize(genScheme.getGenTable().getClassName())); model.put("ClassName", StringUtils.capitalize(genScheme.getGenTable().getClassName())); model.put("functionName", genScheme.getFunctionName()); model.put("functionNameSimple", genScheme.getFunctionNameSimple()); model.put( "functionAuthor", StringUtils.isNotBlank(genScheme.getFunctionAuthor()) ? genScheme.getFunctionAuthor() : UserUtils.getUser().getName()); model.put("functionVersion", DateUtils.getDate()); model.put( "urlPrefix", model.get("moduleName") + (StringUtils.isNotBlank(genScheme.getSubModuleName()) ? "/" + StringUtils.lowerCase(genScheme.getSubModuleName()) : "") + "/" + model.get("className")); model.put( "viewPrefix", // StringUtils.substringAfterLast(model.get("packageName"),".")+"/"+ model.get("urlPrefix")); model.put( "permissionPrefix", model.get("moduleName") + (StringUtils.isNotBlank(genScheme.getSubModuleName()) ? ":" + StringUtils.lowerCase(genScheme.getSubModuleName()) : "") + ":" + model.get("className")); model.put("dbType", Global.getConfig("jdbc.type")); model.put("table", genScheme.getGenTable()); return model; }