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 apm.generate; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.util.Map; import apm.common.utils.DateUtils; import apm.common.utils.FileUtils; import apm.common.utils.FreeMarkers; 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 freemarker.template.Configuration; import freemarker.template.Template; /** * ?? * @author * @version 2014-6-5 ?9:57:12 */ public class Generate { private static Logger logger = LoggerFactory.getLogger(Generate.class); public static void parameters(String packageName, String moduleName, String className, String classAuthor, String functionName, String tableName, String entityContent, String flag) throws Exception { // ========== ?? ==================== // ?????? // ?{packageName}/{moduleName}/{dao,entity,service,web}/{subModuleName}/{className} // packageName ????applicationContext.xmlsrping-mvc.xml?base-package?packagesToScan?4? //String packageName = "lms.modules";//??? //String moduleName = "test"; //???sys String subModuleName = ""; // ?????? //String className = "test"; // ??user //String classAuthor = "htd"; // ThinkGem //String functionName = ""; //?? // ??? Boolean isEnable = true; // ========== ?? ==================== if (!isEnable) { //logger.error("????isEnable = true"); System.out.println("????isEnable = true"); return; } if (StringUtils.isBlank(moduleName) || StringUtils.isBlank(moduleName) || StringUtils.isBlank(className) || StringUtils.isBlank(functionName)) { //logger.error("??????????????"); System.out.println("??????????????"); 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); System.out.println("-------------------------------------------------"); System.out.println(":" + projectPath); // ? String tplPath = StringUtils.replace(projectPath + "/src/main/java/apm/generate/template", "/", separator); //logger.info("Template Path: {}", tplPath); System.out.println("?:" + tplPath); // Java String javaPath = StringUtils.replaceEach( projectPath + "/src/main/java/" + StringUtils.lowerCase(packageName), new String[] { "/", "." }, new String[] { separator, separator }); //logger.info("Java Path: {}", javaPath); System.out.println("Java:" + javaPath); // String viewPath = StringUtils.replace(projectPath + "/src/main/webapp/WEB-INF/views", "/", separator); //logger.info("View Path: {}", viewPath); System.out.println(":" + viewPath); // ??? Configuration cfg = new Configuration(); cfg.setDirectoryForTemplateLoading(new File(tplPath)); // ??? Map<String, String> model = Maps.newHashMap(); model.put("packageName", StringUtils.lowerCase(packageName)); model.put("moduleName", moduleName); model.put("subModuleName", StringUtils.isNotBlank(subModuleName) ? "." + StringUtils.lowerCase(subModuleName) : ""); model.put("className", StringUtils.uncapitalize(className)); 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("tableName", tableName); model.put("urlPrefix", "/" + model.get("className")); model.put("viewUrlPrefix", "/" + model.get("moduleName")); model.put("returnPrefix", "modules" + //StringUtils.substringAfterLast(model.get("packageName"),".")+"/"+ model.get("viewUrlPrefix") + "/" + model.get("className")); model.put("viewPrefix", //StringUtils.substringAfterLast(model.get("packageName"),".")+"/"+ model.get("urlPrefix")); model.put("permissionPrefix", model.get("className")); String[] strs = entityContent.split("##"); model.put("columnContent", strs[0]); model.put("entityContent", strs[1]); System.out.println("-------------------------?------------------------"); Template template = null; String content = null; String filePath = null; if (flag.indexOf("entity") != -1 || flag.length() == 0) { // ? Entity template = cfg.getTemplate("entity.ftl"); content = FreeMarkers.renderTemplate(template, model); filePath = javaPath + separator + model.get("moduleName") + separator + "entity" + separator + StringUtils.lowerCase(subModuleName) + model.get("ClassName") + ".java"; writeFile(content, filePath); System.out.println("? Entity:" + filePath); } if (flag.indexOf("dao") != -1 || flag.length() == 0) { // ? Dao template = cfg.getTemplate("dao.ftl"); content = FreeMarkers.renderTemplate(template, model); filePath = javaPath + separator + model.get("moduleName") + separator + "dao" + separator + StringUtils.lowerCase(subModuleName) + model.get("ClassName") + "Dao.java"; writeFile(content, filePath); System.out.println("? Dao:" + filePath); } if (flag.indexOf("service") != -1 || flag.length() == 0) { // ? Service template = cfg.getTemplate("service.ftl"); content = FreeMarkers.renderTemplate(template, model); filePath = javaPath + separator + model.get("moduleName") + separator + "service" + separator + StringUtils.lowerCase(subModuleName) + model.get("ClassName") + "Service.java"; writeFile(content, filePath); System.out.println("? Service:" + filePath); } if (flag.indexOf("controler") != -1 || flag.length() == 0) { // ? Controller template = cfg.getTemplate("controller.ftl"); content = FreeMarkers.renderTemplate(template, model); filePath = javaPath + separator + model.get("moduleName") + separator + "web" + separator + StringUtils.lowerCase(subModuleName) + model.get("ClassName") + "Controller.java"; writeFile(content, filePath); System.out.println("? Controller:" + filePath); } if (flag.indexOf("form") != -1 || flag.length() == 0) { // ? ViewForm template = cfg.getTemplate("viewForm.ftl"); content = FreeMarkers.renderTemplate(template, model); filePath = viewPath + separator + StringUtils.substringAfterLast(model.get("packageName"), ".") + separator + model.get("moduleName") + separator + StringUtils.lowerCase(subModuleName) + model.get("className") + "Form.jsp"; writeFile(content, filePath); System.out.println("? ViewForm:" + filePath); } if (flag.indexOf("list") != -1 || flag.length() == 0) { // ? ViewList template = cfg.getTemplate("viewList.ftl"); content = FreeMarkers.renderTemplate(template, model); filePath = viewPath + separator + StringUtils.substringAfterLast(model.get("packageName"), ".") + separator + model.get("moduleName") + separator + StringUtils.lowerCase(subModuleName) + model.get("className") + "List.jsp"; writeFile(content, filePath); System.out.println("? ViewList:" + filePath); } } /** * * @param content * @param filePath */ public static void writeFile(String content, String filePath) { try { if (FileUtils.createFile(filePath)) { FileWriter fileWriter = new FileWriter(filePath, true); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write(content); bufferedWriter.close(); fileWriter.close(); } else { //logger.info("??"); System.out.println("??"); } } catch (Exception e) { e.printStackTrace(); } } }