Java tutorial
/** * Copyright © 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.sishuok.es.generate; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.DefaultResourceLoader; import com.google.common.collect.Maps; import com.sishuok.es.generate.utils.DateUtils; import com.sishuok.es.generate.utils.FileUtils; import com.sishuok.es.generate.utils.FreeMarkers; import freemarker.template.Configuration; import freemarker.template.Template; /** * ?? * @author ThinkGem * @version 2013-06-21 */ public class Generate { private static Logger logger = LoggerFactory.getLogger(Generate.class); public static void main(String[] args) throws Exception { // ========== ?? ==================== // ?????? // ?{packageName}/{moduleName}/{dao,entity,service,web}/{subModuleName}/{className} // packageName ????applicationContext.xmlsrping-mvc.xml?base-package?packagesToScan?4? String packageName = "com.sishuok.es"; String sysName = "sys"; // ??sys?showcase?maintain?personal?shop String moduleName = "xxs"; // ???? String tableName = "sys_xxs_attribute"; // user String className = "XxsAttribute"; // ??User String permissionName = "sys:xxsAttribute";//?????????? String folderName = "xxs";//?? String classAuthor = "xxs"; // ThinkGem String functionName = "??????"; // ?? // ??? //Boolean isEnable = false; Boolean isEnable = true; // ========== ?? ==================== if (!isEnable) { logger.error("????isEnable = true"); return; } if (StringUtils.isBlank(moduleName) || StringUtils.isBlank(moduleName) || StringUtils.isBlank(className) || StringUtils.isBlank(functionName)) { logger.error("??????????????"); return; } // ? String separator = File.separator; // ? File projectPath = new DefaultResourceLoader().getResource("").getFile(); while (!new File(projectPath.getPath() + separator + "src" + separator + "main").exists()) { projectPath = projectPath.getParentFile(); } logger.info("Project Path: {}", projectPath); // ? String tplPath = StringUtils.replace(projectPath + "/src/main/java/com/sishuok/es/generate/template", "/", separator); logger.info("Template Path: {}", tplPath); // Java String javaPath = StringUtils.replaceEach( projectPath + "/src/main/java/" + StringUtils.lowerCase(packageName), new String[] { "/", "." }, new String[] { separator, separator }); logger.info("Java Path: {}", javaPath); // String viewPath = StringUtils.replace(projectPath + "/src/main/webapp/WEB-INF/jsp/admin", "/", separator); logger.info("View Path: {}", viewPath); // ??? Configuration cfg = new Configuration(); cfg.setDefaultEncoding("UTF-8"); cfg.setDirectoryForTemplateLoading(new File(tplPath)); // ??? Map<String, String> model = Maps.newHashMap(); model.put("packageName", StringUtils.lowerCase(packageName)); // model.put("sysName", StringUtils.lowerCase(sysName)); //??? model.put("moduleName", StringUtils.lowerCase(moduleName)); //??? model.put("tableName", StringUtils.lowerCase(tableName)); // model.put("className", StringUtils.uncapitalize(className)); //??? model.put("permissionName", permissionName); //???? model.put("ClassName", StringUtils.capitalize(className)); //?? model.put("classAuthor", StringUtils.isNotBlank(classAuthor) ? classAuthor : "Generate Tools"); // model.put("classVersion", DateUtils.getDate()); // model.put("functionName", functionName); //??? model.put("folderName", folderName); //?? model.put("urlPrefix", model.get("moduleName") + "_" + model.get("className")); //jsp?? model.put("viewPrefix", //StringUtils.substringAfterLast(model.get("packageName"),".")+"/"+ model.get("urlPrefix")); model.put("permissionPrefix", model.get("sysName") + ":" + model.get("moduleName")); //?? // ? Entity Template template = cfg.getTemplate("entity.ftl"); String content = FreeMarkers.renderTemplate(template, model); String filePath = javaPath + separator + model.get("sysName") + separator + model.get("moduleName") + separator + "entity" + separator + model.get("ClassName") + ".java"; System.out.println("Entity filePath" + filePath); writeFile(content, filePath); logger.info("Entity: {}", filePath); // ? Repository template = cfg.getTemplate("repository.ftl"); content = FreeMarkers.renderTemplate(template, model); filePath = javaPath + separator + model.get("sysName") + separator + model.get("moduleName") + separator + "repository" + separator + separator + model.get("ClassName") + "Repository.java"; System.out.println("repository filePath" + filePath); writeFile(content, filePath); logger.info("Dao: {}", filePath); // ? Service template = cfg.getTemplate("service.ftl"); content = FreeMarkers.renderTemplate(template, model); filePath = javaPath + separator + model.get("sysName") + separator + model.get("moduleName") + separator + "service" + separator + separator + model.get("ClassName") + "Service.java"; System.out.println("Service filePath" + filePath); writeFile(content, filePath); logger.info("Service: {}", filePath); // ? ??Controller template = cfg.getTemplate("frontController.ftl"); content = FreeMarkers.renderTemplate(template, model); filePath = javaPath + separator + model.get("sysName") + separator + model.get("moduleName") + separator + "web" + separator + "controller" + separator + "front" + separator + model.get("ClassName") + "Controller.java"; System.out.println("Controller filePath" + filePath); writeFile(content, filePath); logger.info("Controller: {}", filePath); // ? ??Controller template = cfg.getTemplate("adminController.ftl"); content = FreeMarkers.renderTemplate(template, model); filePath = javaPath + separator + model.get("sysName") + separator + model.get("moduleName") + separator + "web" + separator + "controller" + separator + "admin" + separator + model.get("ClassName") + "Controller.java"; System.out.println("Controller filePath" + filePath); writeFile(content, filePath); logger.info("Controller: {}", filePath); // ? editForm template = cfg.getTemplate("editForm.ftl"); content = FreeMarkers.renderTemplate(template, model); filePath = viewPath + separator + model.get("sysName") + separator + model.get("folderName") + separator + "editForm.jsp"; System.out.println("---------------------------------------------------"); System.out.println("ViewForm filePath" + filePath); writeFile(content, filePath); logger.info("ViewForm: {}", filePath); // ? list template = cfg.getTemplate("list.ftl"); content = FreeMarkers.renderTemplate(template, model); filePath = viewPath + separator + model.get("sysName") + separator + model.get("folderName") + separator + "list.jsp"; writeFile(content, filePath); System.out.println("ViewListfilePath" + filePath); logger.info("ViewList: {}", filePath); // ? searcheForm template = cfg.getTemplate("searchForm.ftl"); content = FreeMarkers.renderTemplate(template, model); filePath = viewPath + separator + model.get("sysName") + separator + model.get("folderName") + separator + "searchForm.jsp"; writeFile(content, filePath); System.out.println("searcheForm filePath" + filePath); logger.info("ViewList: {}", filePath); // ? listTable template = cfg.getTemplate("listTable.ftl"); content = FreeMarkers.renderTemplate(template, model); filePath = viewPath + separator + model.get("sysName") + separator + model.get("folderName") + separator + "listTable.jsp"; writeFile(content, filePath); System.out.println("listTable filePath" + filePath); logger.info("ViewList: {}", filePath); logger.info("Generate Success."); } /** * * @param content * @param filePath */ public static void writeFile(String content, String filePath) { try { if (FileUtils.createFile(filePath)) { FileOutputStream fos = new FileOutputStream(filePath); Writer writer = new OutputStreamWriter(fos, "UTF-8"); BufferedWriter bufferedWriter = new BufferedWriter(writer); bufferedWriter.write(content); bufferedWriter.close(); writer.close(); } else { logger.info("??"); } } catch (Exception e) { e.printStackTrace(); } } }