Commit ee8491bd by naan1993

代码生成增加可以生成sql

parent 26eda777
...@@ -30,6 +30,7 @@ public class ContextConfig { ...@@ -30,6 +30,7 @@ public class ContextConfig {
private Boolean daoSwitch = true; //dao private Boolean daoSwitch = true; //dao
private Boolean serviceSwitch = true; //service private Boolean serviceSwitch = true; //service
private Boolean entitySwitch = true; //生成实体的开关 private Boolean entitySwitch = true; //生成实体的开关
private Boolean sqlSwitch = true; //生成sql的开关
public void init() { public void init() {
if (entityName == null) { if (entityName == null) {
...@@ -181,4 +182,12 @@ public class ContextConfig { ...@@ -181,4 +182,12 @@ public class ContextConfig {
public void setEntitySwitch(Boolean entitySwitch) { public void setEntitySwitch(Boolean entitySwitch) {
this.entitySwitch = entitySwitch; this.entitySwitch = entitySwitch;
} }
public Boolean getSqlSwitch() {
return sqlSwitch;
}
public void setSqlSwitch(Boolean sqlSwitch) {
this.sqlSwitch = sqlSwitch;
}
} }
package com.stylefeng.guns.core.template.config;
import com.stylefeng.guns.common.constant.state.IsMenu;
import com.stylefeng.guns.common.constant.state.MenuOpenStatus;
import com.stylefeng.guns.common.constant.state.MenuStatus;
import com.stylefeng.guns.common.persistence.model.Menu;
import com.stylefeng.guns.core.util.ToolUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* 全局配置
*
* @author fengshuonan
* @date 2017-05-08 20:21
*/
public class SqlConfig {
private String sqlPathTemplate;
private ContextConfig contextConfig;
private Connection connection;
private String parentMenuName;
private List<Menu> menus = new ArrayList<>(6);
public void init() {
this.sqlPathTemplate = "\\src\\main\\java\\{}.sql";
//根据父菜单查询数据库中的pcode和pcodes
String[] pcodeAndPcodes = getPcodeAndPcodes();
if (pcodeAndPcodes == null) {
System.err.println("父级菜单名称输入有误!!!!");
return;
}
//业务菜单
Menu menu = new Menu();
menu.setCode(contextConfig.getBizEnName());
menu.setPcode(pcodeAndPcodes[0]);
menu.setPcodes(pcodeAndPcodes[1] + "[" + pcodeAndPcodes[0] + "],");
menu.setName(contextConfig.getBizChName());
menu.setIcon("");
menu.setUrl("/" + contextConfig.getBizEnName());
menu.setNum(99);
menu.setLevels(2);
menu.setIsmenu(IsMenu.YES.getCode());
menu.setTips(null);
menu.setStatus(MenuStatus.ENABLE.getCode());
menu.setIsopen(MenuOpenStatus.CLOSE.getCode());
menus.add(menu);
//列表
Menu list = createSubMenu(menu);
list.setCode(contextConfig.getBizEnName() + "_list");
list.setName(contextConfig.getBizChName() + "列表");
list.setUrl("/" + contextConfig.getBizEnName() + "/list");
menus.add(list);
//添加
Menu add = createSubMenu(menu);
add.setCode(contextConfig.getBizEnName() + "_add");
add.setName(contextConfig.getBizChName() + "添加");
add.setUrl("/" + contextConfig.getBizEnName() + "/add");
menus.add(add);
//更新
Menu update = createSubMenu(menu);
update.setCode(contextConfig.getBizEnName() + "_update");
update.setName(contextConfig.getBizChName() + "更新");
update.setUrl("/" + contextConfig.getBizEnName() + "/update");
menus.add(update);
//删除
Menu delete = createSubMenu(menu);
delete.setCode(contextConfig.getBizEnName() + "_delete");
delete.setName(contextConfig.getBizChName() + "删除");
delete.setUrl("/" + contextConfig.getBizEnName() + "/delete");
menus.add(delete);
//详情
Menu detail = createSubMenu(menu);
detail.setCode(contextConfig.getBizEnName() + "_detail");
detail.setName(contextConfig.getBizChName() + "详情");
detail.setUrl("/" + contextConfig.getBizEnName() + "/detail");
menus.add(detail);
}
private Menu createSubMenu(Menu parentMenu) {
Menu menu = new Menu();
menu.setPcode(parentMenu.getCode());
menu.setPcodes(parentMenu.getPcodes() + "[" + parentMenu.getCode() + "],");
menu.setIcon("");
menu.setNum(99);
menu.setLevels(3);
menu.setIsmenu(IsMenu.NO.getCode());
menu.setTips(null);
menu.setStatus(MenuStatus.ENABLE.getCode());
menu.setIsopen(MenuOpenStatus.CLOSE.getCode());
return menu;
}
public String[] getPcodeAndPcodes() {
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement("select * from menu where name like ?");
preparedStatement.setString(1, "%" + parentMenuName + "%");
ResultSet results = preparedStatement.executeQuery();
while (results.next()) {
String pcode = results.getString("code");
String pcodes = results.getString("pcodes");
if (ToolUtil.isNotEmpty(pcode) && ToolUtil.isNotEmpty(pcodes)) {
String[] strings = {pcode, pcodes};
return strings;
} else {
return null;
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 释放资源
try {
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
public ContextConfig getContextConfig() {
return contextConfig;
}
public void setContextConfig(ContextConfig contextConfig) {
this.contextConfig = contextConfig;
}
public String getParentMenuName() {
return parentMenuName;
}
public void setParentMenuName(String parentMenuName) {
this.parentMenuName = parentMenuName;
}
public Connection getConnection() {
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
public String getSqlPathTemplate() {
return sqlPathTemplate;
}
public void setSqlPathTemplate(String sqlPathTemplate) {
this.sqlPathTemplate = sqlPathTemplate;
}
public List<Menu> getMenus() {
return menus;
}
public void setMenus(List<Menu> menus) {
this.menus = menus;
}
}
...@@ -14,15 +14,15 @@ public class SimpleTemplateEngine extends GunsTemplateEngine { ...@@ -14,15 +14,15 @@ public class SimpleTemplateEngine extends GunsTemplateEngine {
@Override @Override
protected void generatePageEditHtml() { protected void generatePageEditHtml() {
String path = ToolUtil.format(super.getContextConfig().getProjectPath() + getPageConfig().getPageEditPathTemplate(), String path = ToolUtil.format(super.getContextConfig().getProjectPath() + getPageConfig().getPageEditPathTemplate(),
super.getContextConfig().getBizEnName(),super.getContextConfig().getBizEnName()); super.getContextConfig().getBizEnName(), super.getContextConfig().getBizEnName());
generateFile( super.getContextConfig().getTemplatePrefixPath() + "/page_edit.html.btl", path); generateFile(super.getContextConfig().getTemplatePrefixPath() + "/page_edit.html.btl", path);
System.out.println("生成编辑页面成功!"); System.out.println("生成编辑页面成功!");
} }
@Override @Override
protected void generatePageAddHtml() { protected void generatePageAddHtml() {
String path = ToolUtil.format(super.getContextConfig().getProjectPath() + getPageConfig().getPageAddPathTemplate(), String path = ToolUtil.format(super.getContextConfig().getProjectPath() + getPageConfig().getPageAddPathTemplate(),
super.getContextConfig().getBizEnName(),super.getContextConfig().getBizEnName()); super.getContextConfig().getBizEnName(), super.getContextConfig().getBizEnName());
generateFile(super.getContextConfig().getTemplatePrefixPath() + "/page_add.html.btl", path); generateFile(super.getContextConfig().getTemplatePrefixPath() + "/page_add.html.btl", path);
System.out.println("生成添加页面成功!"); System.out.println("生成添加页面成功!");
} }
...@@ -30,7 +30,7 @@ public class SimpleTemplateEngine extends GunsTemplateEngine { ...@@ -30,7 +30,7 @@ public class SimpleTemplateEngine extends GunsTemplateEngine {
@Override @Override
protected void generatePageInfoJs() { protected void generatePageInfoJs() {
String path = ToolUtil.format(super.getContextConfig().getProjectPath() + getPageConfig().getPageInfoJsPathTemplate(), String path = ToolUtil.format(super.getContextConfig().getProjectPath() + getPageConfig().getPageInfoJsPathTemplate(),
super.getContextConfig().getBizEnName(),super.getContextConfig().getBizEnName()); super.getContextConfig().getBizEnName(), super.getContextConfig().getBizEnName());
generateFile(super.getContextConfig().getTemplatePrefixPath() + "/page_info.js.btl", path); generateFile(super.getContextConfig().getTemplatePrefixPath() + "/page_info.js.btl", path);
System.out.println("生成页面详情js成功!"); System.out.println("生成页面详情js成功!");
} }
...@@ -38,7 +38,7 @@ public class SimpleTemplateEngine extends GunsTemplateEngine { ...@@ -38,7 +38,7 @@ public class SimpleTemplateEngine extends GunsTemplateEngine {
@Override @Override
protected void generatePageJs() { protected void generatePageJs() {
String path = ToolUtil.format(super.getContextConfig().getProjectPath() + getPageConfig().getPageJsPathTemplate(), String path = ToolUtil.format(super.getContextConfig().getProjectPath() + getPageConfig().getPageJsPathTemplate(),
super.getContextConfig().getBizEnName(),super.getContextConfig().getBizEnName()); super.getContextConfig().getBizEnName(), super.getContextConfig().getBizEnName());
generateFile(super.getContextConfig().getTemplatePrefixPath() + "/page.js.btl", path); generateFile(super.getContextConfig().getTemplatePrefixPath() + "/page.js.btl", path);
System.out.println("生成页面js成功!"); System.out.println("生成页面js成功!");
} }
...@@ -46,7 +46,7 @@ public class SimpleTemplateEngine extends GunsTemplateEngine { ...@@ -46,7 +46,7 @@ public class SimpleTemplateEngine extends GunsTemplateEngine {
@Override @Override
protected void generatePageHtml() { protected void generatePageHtml() {
String path = ToolUtil.format(super.getContextConfig().getProjectPath() + getPageConfig().getPagePathTemplate(), String path = ToolUtil.format(super.getContextConfig().getProjectPath() + getPageConfig().getPagePathTemplate(),
super.getContextConfig().getBizEnName(),super.getContextConfig().getBizEnName()); super.getContextConfig().getBizEnName(), super.getContextConfig().getBizEnName());
generateFile(super.getContextConfig().getTemplatePrefixPath() + "/page.html.btl", path); generateFile(super.getContextConfig().getTemplatePrefixPath() + "/page.html.btl", path);
System.out.println("生成页面成功!"); System.out.println("生成页面成功!");
} }
...@@ -58,4 +58,12 @@ public class SimpleTemplateEngine extends GunsTemplateEngine { ...@@ -58,4 +58,12 @@ public class SimpleTemplateEngine extends GunsTemplateEngine {
generateFile(super.getContextConfig().getTemplatePrefixPath() + "/Controller.java.btl", controllerPath); generateFile(super.getContextConfig().getTemplatePrefixPath() + "/Controller.java.btl", controllerPath);
System.out.println("生成控制器成功!"); System.out.println("生成控制器成功!");
} }
@Override
protected void generateSqls() {
String path = ToolUtil.format(super.getContextConfig().getProjectPath() + super.sqlConfig.getSqlPathTemplate(),
ToolUtil.firstLetterToUpper(super.getContextConfig().getBizEnName()));
generateFile(super.getContextConfig().getTemplatePrefixPath() + "/menu_sql.sql.btl", path);
System.out.println("生成sql成功!");
}
} }
...@@ -15,6 +15,7 @@ public class AbstractTemplateEngine { ...@@ -15,6 +15,7 @@ public class AbstractTemplateEngine {
protected PageConfig pageConfig; //页面的控制器 protected PageConfig pageConfig; //页面的控制器
protected DaoConfig daoConfig; //Dao配置 protected DaoConfig daoConfig; //Dao配置
protected ServiceConfig serviceConfig; //Service配置 protected ServiceConfig serviceConfig; //Service配置
protected SqlConfig sqlConfig; //sql配置
public void initConfig() { public void initConfig() {
if (this.contextConfig == null) { if (this.contextConfig == null) {
...@@ -32,7 +33,9 @@ public class AbstractTemplateEngine { ...@@ -32,7 +33,9 @@ public class AbstractTemplateEngine {
if (this.serviceConfig == null) { if (this.serviceConfig == null) {
this.serviceConfig = new ServiceConfig(); this.serviceConfig = new ServiceConfig();
} }
if (this.sqlConfig == null) {
this.sqlConfig = new SqlConfig();
}
this.contextConfig.init(); this.contextConfig.init();
this.controllerConfig.setContextConfig(this.contextConfig); this.controllerConfig.setContextConfig(this.contextConfig);
...@@ -46,6 +49,9 @@ public class AbstractTemplateEngine { ...@@ -46,6 +49,9 @@ public class AbstractTemplateEngine {
this.pageConfig.setContextConfig(this.contextConfig); this.pageConfig.setContextConfig(this.contextConfig);
this.pageConfig.init(); this.pageConfig.init();
this.sqlConfig.setContextConfig(this.contextConfig);
this.sqlConfig.init();
} }
public PageConfig getPageConfig() { public PageConfig getPageConfig() {
...@@ -87,5 +93,13 @@ public class AbstractTemplateEngine { ...@@ -87,5 +93,13 @@ public class AbstractTemplateEngine {
public void setServiceConfig(ServiceConfig serviceConfig) { public void setServiceConfig(ServiceConfig serviceConfig) {
this.serviceConfig = serviceConfig; this.serviceConfig = serviceConfig;
} }
public SqlConfig getSqlConfig() {
return sqlConfig;
}
public void setSqlConfig(SqlConfig sqlConfig) {
this.sqlConfig = sqlConfig;
}
} }
...@@ -44,24 +44,25 @@ public abstract class GunsTemplateEngine extends AbstractTemplateEngine { ...@@ -44,24 +44,25 @@ public abstract class GunsTemplateEngine extends AbstractTemplateEngine {
groupTemplate.registerFunctionPackage("tool", new ToolUtil()); groupTemplate.registerFunctionPackage("tool", new ToolUtil());
} }
protected void configTemplate(Template template){ protected 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("dao", super.getDaoConfig());
template.binding("service", super.getServiceConfig()); template.binding("service", super.getServiceConfig());
template.binding("sqls", super.sqlConfig);
} }
protected void generateFile(String template,String filePath){ protected void generateFile(String template, String filePath) {
Template pageTemplate = groupTemplate.getTemplate(template); Template pageTemplate = groupTemplate.getTemplate(template);
configTemplate(pageTemplate); configTemplate(pageTemplate);
if(PlatformUtil.isWindows()){ if (PlatformUtil.isWindows()) {
filePath = filePath.replaceAll("/+|\\\\+","\\\\"); filePath = filePath.replaceAll("/+|\\\\+", "\\\\");
}else{ } else {
filePath = filePath.replaceAll("/+|\\\\+","/"); filePath = filePath.replaceAll("/+|\\\\+", "/");
} }
File file = new File(filePath); File file = new File(filePath);
File parentFile = file.getParentFile(); File parentFile = file.getParentFile();
if(!parentFile.exists()){ if (!parentFile.exists()) {
parentFile.mkdirs(); parentFile.mkdirs();
} }
try { try {
...@@ -76,24 +77,27 @@ public abstract class GunsTemplateEngine extends AbstractTemplateEngine { ...@@ -76,24 +77,27 @@ public abstract class GunsTemplateEngine extends AbstractTemplateEngine {
super.initConfig(); super.initConfig();
//生成模板 //生成模板
if(super.contextConfig.getControllerSwitch()){ if (super.contextConfig.getControllerSwitch()) {
generateController(); generateController();
} }
if(super.contextConfig.getIndexPageSwitch()){ if (super.contextConfig.getIndexPageSwitch()) {
generatePageHtml(); generatePageHtml();
} }
if(super.contextConfig.getAddPageSwitch()){ if (super.contextConfig.getAddPageSwitch()) {
generatePageAddHtml(); generatePageAddHtml();
} }
if(super.contextConfig.getEditPageSwitch()){ if (super.contextConfig.getEditPageSwitch()) {
generatePageEditHtml(); generatePageEditHtml();
} }
if(super.contextConfig.getJsSwitch()){ if (super.contextConfig.getJsSwitch()) {
generatePageJs(); generatePageJs();
} }
if(super.contextConfig.getInfoJsSwitch()){ if (super.contextConfig.getInfoJsSwitch()) {
generatePageInfoJs(); generatePageInfoJs();
} }
if (super.contextConfig.getSqlSwitch()) {
generateSqls();
}
} }
protected abstract void generatePageEditHtml(); protected abstract void generatePageEditHtml();
...@@ -108,4 +112,6 @@ public abstract class GunsTemplateEngine extends AbstractTemplateEngine { ...@@ -108,4 +112,6 @@ public abstract class GunsTemplateEngine extends AbstractTemplateEngine {
protected abstract void generateController(); protected abstract void generateController();
protected abstract void generateSqls();
} }
...@@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.generator.config.GlobalConfig; ...@@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig; import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig; import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.stylefeng.guns.core.template.config.ContextConfig; import com.stylefeng.guns.core.template.config.ContextConfig;
import com.stylefeng.guns.core.template.config.SqlConfig;
import com.stylefeng.guns.core.template.engine.SimpleTemplateEngine; import com.stylefeng.guns.core.template.engine.SimpleTemplateEngine;
import com.stylefeng.guns.core.template.engine.base.GunsTemplateEngine; import com.stylefeng.guns.core.template.engine.base.GunsTemplateEngine;
import com.stylefeng.guns.core.util.FileUtil; import com.stylefeng.guns.core.util.FileUtil;
...@@ -37,6 +38,8 @@ public abstract class AbstractGeneratorConfig { ...@@ -37,6 +38,8 @@ public abstract class AbstractGeneratorConfig {
*/ */
ContextConfig contextConfig = new ContextConfig(); ContextConfig contextConfig = new ContextConfig();
SqlConfig sqlConfig = new SqlConfig();
protected abstract void globalConfig(); protected abstract void globalConfig();
protected abstract void dataSourceConfig(); protected abstract void dataSourceConfig();
...@@ -60,16 +63,16 @@ public abstract class AbstractGeneratorConfig { ...@@ -60,16 +63,16 @@ public abstract class AbstractGeneratorConfig {
//controller没用掉,生成之后会自动删掉 //controller没用掉,生成之后会自动删掉
packageConfig.setController("TTT"); packageConfig.setController("TTT");
if(!contextConfig.getEntitySwitch()){ if (!contextConfig.getEntitySwitch()) {
packageConfig.setEntity("TTT"); packageConfig.setEntity("TTT");
} }
if(!contextConfig.getDaoSwitch()){ if (!contextConfig.getDaoSwitch()) {
packageConfig.setMapper("TTT"); packageConfig.setMapper("TTT");
packageConfig.setXml("TTT"); packageConfig.setXml("TTT");
} }
if(!contextConfig.getServiceSwitch()){ if (!contextConfig.getServiceSwitch()) {
packageConfig.setService("TTT"); packageConfig.setService("TTT");
packageConfig.setServiceImpl("TTT"); packageConfig.setServiceImpl("TTT");
} }
...@@ -79,7 +82,7 @@ public abstract class AbstractGeneratorConfig { ...@@ -79,7 +82,7 @@ public abstract class AbstractGeneratorConfig {
/** /**
* 删除不必要的代码 * 删除不必要的代码
*/ */
public void destory(){ public void destory() {
String outputDir = globalConfig.getOutputDir() + "/TTT"; String outputDir = globalConfig.getOutputDir() + "/TTT";
FileUtil.deleteDir(new File(outputDir)); FileUtil.deleteDir(new File(outputDir));
} }
...@@ -101,6 +104,8 @@ public abstract class AbstractGeneratorConfig { ...@@ -101,6 +104,8 @@ public abstract class AbstractGeneratorConfig {
public void doGunsGeneration() { public void doGunsGeneration() {
GunsTemplateEngine gunsTemplateEngine = new SimpleTemplateEngine(); GunsTemplateEngine gunsTemplateEngine = new SimpleTemplateEngine();
gunsTemplateEngine.setContextConfig(contextConfig); gunsTemplateEngine.setContextConfig(contextConfig);
sqlConfig.setConnection(dataSourceConfig.getConn());
gunsTemplateEngine.setSqlConfig(sqlConfig);
gunsTemplateEngine.start(); gunsTemplateEngine.start();
} }
} }
<% for(menu in sqls.menus!){ %>
INSERT INTO `guns`.`menu` (`code`, `pcode`, `pcodes`, `name`, `icon`, `url`, `num`, `levels`, `ismenu`, `tips`, `status`, `isopen`) VALUES ('${menu.code}', '${menu.pcode}', '${menu.pcodes}', '${menu.name}', '${menu.icon}', '${menu.url}', '${menu.num}', '${menu.levels}', '${menu.ismenu}', NULL, '${menu.status}', '${menu.isopen}');
<% } %>
\ No newline at end of file
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