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 org.guess.generate; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; import net.iharding.utils.DateUtil; import net.iharding.utils.FileUtils; import net.iharding.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 ThinkGem * @version 2013-06-21 */ public class Generate { private static Logger logger = LoggerFactory.getLogger(Generate.class); public static void main(String[] args) { // ========== ?? ==================== // ?????? // ?{packageName}/{moduleName}/{dao,entity,service,web}/{subModuleName}/{className} // packageName // ????applicationContext.xmlsrping-mvc.xml?base-package?packagesToScan?4? String packageName = "net.iharding.modulesmeta"; String moduleName = "meta"; // ???sys String className = "project"; // ??user String classAuthor = "Joe.zhang"; // String functionName = "??"; // ?? List<Field> fields = new ArrayList<Field>(); fields.add(new Field("projectCode", "?", "String")); fields.add(new Field("projectName", "??", "String")); fields.add(new Field("remark", "", "String")); fields.add(new Field("createDate", "", "Date")); fields.add(new Field("updateDate", "", "Date")); fields.add(new Field("createId", "", "createId")); fields.add(new Field("updateId", "", "updateId")); // ??? 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 = null; try { projectPath = new DefaultResourceLoader().getResource("").getFile(); // File projectPath = new File("D:/template"); while (!new File(projectPath.getPath() + separator + "src" + separator + "main").exists()) { projectPath = projectPath.getParentFile(); } logger.info("Project Path: {}", projectPath); // ? String tplPath = StringUtils.replace( projectPath.getAbsolutePath() + "/src/test/java/org/guess/generate/temp", "/", separator); logger.info("Template Path: {}", tplPath); // Java String javaPath = StringUtils.replaceEach( projectPath.getAbsolutePath() + "/src/main/java/" + StringUtils.lowerCase(packageName), new String[] { "/", "." }, new String[] { separator, separator }); // String javaPath = "D:/template"; logger.info("Java Path: {}", javaPath); String viewPath = StringUtils.replace( projectPath + "/src/main/webapp/WEB-INF/content/" + moduleName + "/" + className, "/", separator); // String viewPath = "D:/template"; // ??? Configuration cfg = new Configuration(); FileUtils.isFolderExitAndCreate(tplPath); cfg.setDirectoryForTemplateLoading(new File(tplPath)); // ??? Map<String, Object> model = Maps.newHashMap(); model.put("packageName", StringUtils.lowerCase(packageName)); model.put("moduleName", StringUtils.lowerCase(moduleName)); model.put("className", StringUtils.uncapitalize(className)); model.put("ClassName", StringUtils.capitalize(className)); model.put("classAuthor", StringUtils.isNotBlank(classAuthor) ? classAuthor : "Generate Tools"); model.put("classVersion", DateUtil.getCurrenDate()); model.put("functionName", functionName); model.put("tableName", model.get("moduleName") + "_" + model.get("className")); model.put("fields", fields); // ? Entity Template template = cfg.getTemplate("entity.ftl"); String content = FreeMarkers.renderTemplate(template, model); String filePath = javaPath + separator + model.get("moduleName") + separator + "model" + separator + model.get("ClassName") + ".java"; // writeFile(content, filePath); logger.info("Entity: {}", filePath); // ? Dao template = cfg.getTemplate("dao.ftl"); content = FreeMarkers.renderTemplate(template, model); filePath = javaPath + separator + model.get("moduleName") + separator + "dao" + separator + model.get("ClassName") + "Dao.java"; writeFile(content, filePath); logger.info("Dao: {}", filePath); // ? DaoImpl template = cfg.getTemplate("daoImpl.ftl"); content = FreeMarkers.renderTemplate(template, model); filePath = javaPath + separator + model.get("moduleName") + separator + "dao" + separator + "impl" + separator + model.get("ClassName") + "DaoImpl.java"; writeFile(content, filePath); logger.info("Dao: {}", filePath); // ? Service template = cfg.getTemplate("service.ftl"); content = FreeMarkers.renderTemplate(template, model); filePath = javaPath + separator + model.get("moduleName") + separator + "service" + separator + model.get("ClassName") + "Service.java"; writeFile(content, filePath); logger.info("Service: {}", filePath); // ? Service template = cfg.getTemplate("serviceImpl.ftl"); content = FreeMarkers.renderTemplate(template, model); filePath = javaPath + separator + model.get("moduleName") + separator + "service" + separator + "impl" + separator + model.get("ClassName") + "ServiceImpl.java"; writeFile(content, filePath); logger.info("Service: {}", filePath); // ? Controller template = cfg.getTemplate("controller.ftl"); content = FreeMarkers.renderTemplate(template, model); filePath = javaPath + separator + model.get("moduleName") + separator + "controller" + separator + model.get("ClassName") + "Controller.java"; writeFile(content, filePath); logger.info("Controller: {}", filePath); /* // ? list.jsp template = cfg.getTemplate("list.ftl"); content = FreeMarkers.renderTemplate(template, model); filePath = viewPath + separator + "list.jsp"; writeFile(content, filePath); logger.info("Controller: {}", filePath); // ? edit.jsp template = cfg.getTemplate("edit.ftl"); content = FreeMarkers.renderTemplate(template, model); filePath = viewPath + separator + "edit.jsp"; writeFile(content, filePath); logger.info("Controller: {}", filePath);*/ logger.info("Generate Success."); } catch (IOException e) { e.printStackTrace(); } } /** * * * @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("??"); } } catch (Exception e) { e.printStackTrace(); } } }