Commit c0da9046 by naan1993

Guns模板生成器新增Dao和Service生成

parent 5c20c7dd
...@@ -14,6 +14,16 @@ public class ContextConfig { ...@@ -14,6 +14,16 @@ public class ContextConfig {
private String bizChName; //业务名称 private String bizChName; //业务名称
private String bizEnName; //业务英文名称 private String bizEnName; //业务英文名称
private String bizEnBigName;//业务英文名称(大写) private String bizEnBigName;//业务英文名称(大写)
private String moduleName; //模块名称
private Boolean controllerSwitch = true; //是否生成控制器代码开关
private Boolean indexPageSwitch = true; //主页
private Boolean addPageSwitch = true; //添加页面
private Boolean editPageSwitch = true; //编辑页面
private Boolean jsSwitch = true; //js
private Boolean infoJsSwitch = true; //详情页面js
private Boolean daoSwitch = true; //dao
private Boolean serviceSwitch = true; //service
public String getBizEnBigName() { public String getBizEnBigName() {
return bizEnBigName; return bizEnBigName;
...@@ -47,4 +57,76 @@ public class ContextConfig { ...@@ -47,4 +57,76 @@ public class ContextConfig {
public void setProjectPath(String projectPath) { public void setProjectPath(String projectPath) {
this.projectPath = projectPath; this.projectPath = projectPath;
} }
public String getModuleName() {
return moduleName;
}
public void setModuleName(String moduleName) {
this.moduleName = moduleName;
}
public Boolean getControllerSwitch() {
return controllerSwitch;
}
public void setControllerSwitch(Boolean controllerSwitch) {
this.controllerSwitch = controllerSwitch;
}
public Boolean getIndexPageSwitch() {
return indexPageSwitch;
}
public void setIndexPageSwitch(Boolean indexPageSwitch) {
this.indexPageSwitch = indexPageSwitch;
}
public Boolean getAddPageSwitch() {
return addPageSwitch;
}
public void setAddPageSwitch(Boolean addPageSwitch) {
this.addPageSwitch = addPageSwitch;
}
public Boolean getEditPageSwitch() {
return editPageSwitch;
}
public void setEditPageSwitch(Boolean editPageSwitch) {
this.editPageSwitch = editPageSwitch;
}
public Boolean getJsSwitch() {
return jsSwitch;
}
public void setJsSwitch(Boolean jsSwitch) {
this.jsSwitch = jsSwitch;
}
public Boolean getInfoJsSwitch() {
return infoJsSwitch;
}
public void setInfoJsSwitch(Boolean infoJsSwitch) {
this.infoJsSwitch = infoJsSwitch;
}
public Boolean getDaoSwitch() {
return daoSwitch;
}
public void setDaoSwitch(Boolean daoSwitch) {
this.daoSwitch = daoSwitch;
}
public Boolean getServiceSwitch() {
return serviceSwitch;
}
public void setServiceSwitch(Boolean serviceSwitch) {
this.serviceSwitch = serviceSwitch;
}
} }
package com.stylefeng.guns.core.template.config;
/**
* Dao模板生成的配置
*
* @author fengshuonan
* @date 2017-05-07 22:12
*/
public class DaoConfig {
private String daoPathTemplate = "\\src\\main\\java\\com\\stylefeng\\guns\\modular\\system\\dao\\{}Dao.java";
private String xmlPathTemplate = "\\src\\main\\java\\com\\stylefeng\\guns\\modular\\system\\dao\\mapping\\{}Dao.xml";
private String packageName = "com.stylefeng.guns.modular.system.dao";
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public String getDaoPathTemplate() {
return daoPathTemplate;
}
public void setDaoPathTemplate(String daoPathTemplate) {
this.daoPathTemplate = daoPathTemplate;
}
public String getXmlPathTemplate() {
return xmlPathTemplate;
}
public void setXmlPathTemplate(String xmlPathTemplate) {
this.xmlPathTemplate = xmlPathTemplate;
}
}
package com.stylefeng.guns.core.template.config; package com.stylefeng.guns.core.template.config;
/** /**
* 控制器模板生成的配置 * 页面 模板生成的配置
* *
* @author fengshuonan * @author fengshuonan
* @date 2017-05-07 22:12 * @date 2017-05-07 22:12
......
package com.stylefeng.guns.core.template.config;
import java.util.ArrayList;
import java.util.List;
/**
* Service模板生成的配置
*
* @author fengshuonan
* @date 2017-05-07 22:12
*/
public class ServiceConfig {
private ContextConfig contextConfig;
private String servicePathTemplate = "\\src\\main\\java\\com\\stylefeng\\guns\\modular\\system\\service\\I{}Service.java";
private String serviceImplPathTemplate = "\\src\\main\\java\\com\\stylefeng\\guns\\modular\\system\\service\\impl\\{}ServiceImpl.java";
private String packageName = "com.stylefeng.guns.modular.system.service";
private List<String> serviceImplImports;
public ServiceConfig(ContextConfig contextConfig) {
this.contextConfig = contextConfig;
init();
}
private void init() {
ArrayList<String> imports = new ArrayList<>();
imports.add("org.springframework.stereotype.Service");
imports.add("com.stylefeng.guns.modular.system.service.I" + contextConfig.getBizEnBigName() + "Service");
this.serviceImplImports = imports;
}
public String getServicePathTemplate() {
return servicePathTemplate;
}
public void setServicePathTemplate(String servicePathTemplate) {
this.servicePathTemplate = servicePathTemplate;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public String getServiceImplPathTemplate() {
return serviceImplPathTemplate;
}
public void setServiceImplPathTemplate(String serviceImplPathTemplate) {
this.serviceImplPathTemplate = serviceImplPathTemplate;
}
public List<String> getServiceImplImports() {
return serviceImplImports;
}
public void setServiceImplImports(List<String> serviceImplImports) {
this.serviceImplImports = serviceImplImports;
}
public ContextConfig getContextConfig() {
return contextConfig;
}
public void setContextConfig(ContextConfig contextConfig) {
this.contextConfig = contextConfig;
}
}
...@@ -58,4 +58,30 @@ public class SimpleTemplateEngine extends GunsTemplateEngine { ...@@ -58,4 +58,30 @@ public class SimpleTemplateEngine extends GunsTemplateEngine {
generateFile("gunsTemplate/Controller.java.btl", controllerPath); generateFile("gunsTemplate/Controller.java.btl", controllerPath);
System.out.println("生成控制器成功!"); System.out.println("生成控制器成功!");
} }
@Override
protected void generateDao() {
String daoPath = ToolUtil.format(super.getContextConfig().getProjectPath() + super.getDaoConfig().getDaoPathTemplate(),
ToolUtil.firstLetterToUpper(super.getContextConfig().getBizEnName()));
generateFile("gunsTemplate/Dao.java.btl", daoPath);
System.out.println("生成Dao成功!");
String mappingPath = ToolUtil.format(super.getContextConfig().getProjectPath() + super.getDaoConfig().getXmlPathTemplate(),
ToolUtil.firstLetterToUpper(super.getContextConfig().getBizEnName()));
generateFile("gunsTemplate/Mapping.xml.btl", mappingPath);
System.out.println("生成Dao Mapping xml成功!");
}
@Override
protected void generateService() {
String servicePath = ToolUtil.format(super.getContextConfig().getProjectPath() + super.getServiceConfig().getServicePathTemplate(),
ToolUtil.firstLetterToUpper(super.getContextConfig().getBizEnName()));
generateFile("gunsTemplate/Service.java.btl", servicePath);
System.out.println("生成Service成功!");
String serviceImplPath = ToolUtil.format(super.getContextConfig().getProjectPath() + super.getServiceConfig().getServiceImplPathTemplate(),
ToolUtil.firstLetterToUpper(super.getContextConfig().getBizEnName()));
generateFile("gunsTemplate/ServiceImpl.java.btl", serviceImplPath);
System.out.println("生成ServiceImpl成功!");
}
} }
package com.stylefeng.guns.core.template.engine.base; package com.stylefeng.guns.core.template.engine.base;
import com.stylefeng.guns.core.template.config.ContextConfig; import com.stylefeng.guns.core.template.config.*;
import com.stylefeng.guns.core.template.config.ControllerConfig;
import com.stylefeng.guns.core.template.config.PageConfig;
/** /**
* 模板生成父类 * 模板生成父类
...@@ -12,9 +10,29 @@ import com.stylefeng.guns.core.template.config.PageConfig; ...@@ -12,9 +10,29 @@ import com.stylefeng.guns.core.template.config.PageConfig;
*/ */
public class AbstractTemplateEngine { public class AbstractTemplateEngine {
private ContextConfig contextConfig = new ContextConfig(); //全局配置 protected ContextConfig contextConfig; //全局配置
private ControllerConfig controllerConfig = new ControllerConfig(); //控制器的配置 protected ControllerConfig controllerConfig; //控制器的配置
private PageConfig pageConfig = new PageConfig(); //页面的控制器 protected PageConfig pageConfig; //页面的控制器
protected DaoConfig daoConfig; //Dao配置
protected ServiceConfig serviceConfig; //Service配置
public void initConfig() {
if (this.contextConfig == null) {
contextConfig = new ContextConfig();
}
if (this.controllerConfig == null) {
controllerConfig = new ControllerConfig();
}
if (this.pageConfig == null) {
pageConfig = new PageConfig();
}
if (this.daoConfig == null) {
daoConfig = new DaoConfig();
}
if (this.serviceConfig == null) {
serviceConfig = new ServiceConfig(contextConfig);
}
}
public PageConfig getPageConfig() { public PageConfig getPageConfig() {
return pageConfig; return pageConfig;
...@@ -39,5 +57,21 @@ public class AbstractTemplateEngine { ...@@ -39,5 +57,21 @@ public class AbstractTemplateEngine {
public void setControllerConfig(ControllerConfig controllerConfig) { public void setControllerConfig(ControllerConfig controllerConfig) {
this.controllerConfig = controllerConfig; this.controllerConfig = controllerConfig;
} }
public DaoConfig getDaoConfig() {
return daoConfig;
}
public void setDaoConfig(DaoConfig daoConfig) {
this.daoConfig = daoConfig;
}
public ServiceConfig getServiceConfig() {
return serviceConfig;
}
public void setServiceConfig(ServiceConfig serviceConfig) {
this.serviceConfig = serviceConfig;
}
} }
...@@ -46,6 +46,8 @@ public abstract class GunsTemplateEngine extends AbstractTemplateEngine { ...@@ -46,6 +46,8 @@ public abstract class GunsTemplateEngine extends AbstractTemplateEngine {
public void configTemplate(Template template){ public void configTemplate(Template template){
template.binding("controller", super.getControllerConfig()); template.binding("controller", super.getControllerConfig());
template.binding("context", super.getContextConfig()); template.binding("context", super.getContextConfig());
template.binding("dao", super.getDaoConfig());
template.binding("service", super.getServiceConfig());
} }
public void generateFile(String template,String filePath){ public void generateFile(String template,String filePath){
...@@ -64,12 +66,35 @@ public abstract class GunsTemplateEngine extends AbstractTemplateEngine { ...@@ -64,12 +66,35 @@ public abstract class GunsTemplateEngine extends AbstractTemplateEngine {
} }
public void start() { public void start() {
generateController(); //配置之间的相互依赖
generatePageHtml(); super.initConfig();
generatePageAddHtml();
generatePageEditHtml(); //生成模板
generatePageJs(); if(super.contextConfig.getControllerSwitch()){
generatePageInfoJs(); generateController();
}
if(super.contextConfig.getIndexPageSwitch()){
generatePageHtml();
}
if(super.contextConfig.getAddPageSwitch()){
generatePageAddHtml();
}
if(super.contextConfig.getEditPageSwitch()){
generatePageEditHtml();
}
if(super.contextConfig.getJsSwitch()){
generatePageJs();
}
if(super.contextConfig.getInfoJsSwitch()){
generatePageInfoJs();
}
if(super.contextConfig.getDaoSwitch()){
generateDao();
}
if(super.contextConfig.getServiceSwitch()){
generateService();
}
} }
protected abstract void generatePageEditHtml(); protected abstract void generatePageEditHtml();
...@@ -84,4 +109,8 @@ public abstract class GunsTemplateEngine extends AbstractTemplateEngine { ...@@ -84,4 +109,8 @@ public abstract class GunsTemplateEngine extends AbstractTemplateEngine {
protected abstract void generateController(); protected abstract void generateController();
protected abstract void generateDao();
protected abstract void generateService();
} }
package ${dao.packageName};
/**
* ${context.bizChName}Dao
*
* @author fengshuonan
* @Date ${tool.currentTime()}
*/
public interface ${context.bizEnBigName}Dao {
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${dao.packageName}.${context.bizEnBigName}Dao">
</mapper>
\ No newline at end of file
package ${service.packageName};
/**
* ${context.bizChName}Service
*
* @author fengshuonan
* @Date ${tool.currentTime()}
*/
public interface I${context.bizEnBigName}Service {
}
package ${service.packageName}.impl;
<% for(import in service.serviceImplImports!){ %>
import ${import};
<% } %>
/**
* ${context.bizChName}Dao
*
* @author fengshuonan
* @Date ${tool.currentTime()}
*/
@Service
public class ${context.bizEnBigName}ServiceImpl implements I${context.bizEnBigName}Service {
}
...@@ -16,8 +16,11 @@ public class TemplateGenerator { ...@@ -16,8 +16,11 @@ public class TemplateGenerator {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
ContextConfig contextConfig = new ContextConfig(); ContextConfig contextConfig = new ContextConfig();
contextConfig.setBizChName("代码生成"); contextConfig.setBizChName("啊哈");
contextConfig.setBizEnName("code"); contextConfig.setBizEnName("haha");
contextConfig.setAddPageSwitch(false);
contextConfig.setEditPageSwitch(false);
GunsTemplateEngine gunsTemplateEngine = new SimpleTemplateEngine(); GunsTemplateEngine gunsTemplateEngine = new SimpleTemplateEngine();
gunsTemplateEngine.setContextConfig(contextConfig); gunsTemplateEngine.setContextConfig(contextConfig);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment