Commit 5e1f3c98 authored by 李家智's avatar 李家智
Browse files

新增加项目生成

parent ab42eb86
......@@ -5,6 +5,8 @@
基本技术栈来源于我为电子工业出版社编写的的[<<Spring Boot 2 精髓 >>](http://ibeetl.com/sb2/#more) (这本书每一章也有各种例子,但Springboot-plus 更偏向于应用而不是教学)
当前版本:1.0.0.Snapshot
技术交流群:219324263
开源地址:https://gitee.com/xiandafu/springboot-plus
......@@ -12,8 +14,12 @@
![doc/readme/user.png](doc/readme/user.png)
![doc/readme/user.png](doc/readme/role.png)![doc/readme/user.png](doc/readme/data.png)![doc/readme/user.png](doc/readme/codegen.png)![doc/readme/user.png](doc/readme/codegen2.png)
![doc/readme/user.png](doc/readme/role.png)
![doc/readme/user.png](doc/readme/data.png)
![doc/readme/user.png](doc/readme/codePorject.png)
![doc/readme/user.png](doc/readme/codegen.png)
![doc/readme/user.png](doc/readme/codegen2.png)
![doc/readme/user.png](doc/readme/excelExport.png)
# 1 使用说明
......@@ -45,140 +51,16 @@ spring.datasource.password=123456
SpringBoot-plus 是一个适合大系统拆分成小系统的架构,或者是一个微服务系统,因此,如果你需要创建自己的业务系统,比如,一个CMS子系统,建议你不要在SpringBoot-Plus 添加代码,应该是新建立一个maven工程,依赖admin-core,或者依赖admin-console(如果你有后台管理需求,通常都有,但不是必须的)
创建一个业务子系统,需要如下方式
* 在IDE里创建一个Maven工程
* 将Maven工程改造为Spring Boot工程,如果你不熟悉Spring Boot,可以参考SpringBoot-plus 或者copy如下片段
~~~xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>cms</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RC1</version>
</parent>
<!-- plus依赖 -->
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>admin-core</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>admin-console</artifactId>
<version>1.0</version>
</dependency>
<!-- 其他Spring Boot依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- 其他你自己需要的类库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.5</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
~~~
### 1.2.1 创建启动程序
新创建一个CMSApplication 入口
~~~java
package com.sample.cms;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.WebApplicationInitializer;
@SpringBootApplication
@EnableCaching
@ComponentScan(basePackages= {"com.sample.cms","com.ibeetl.admin"})
public class CMSApplication extends SpringBootServletInitializer implements WebApplicationInitializer {
public static void main(String[] args) {
SpringApplication.run(CMSApplication.class, args);
}
}
~~~
运行此程序,然后再次访问http://127.0.0.1:8080/ 你会发现你的的CMS子系统已经具备了所有基础功能,你只需要向此工程添加跟CMS相关功能即可
> 如果你不理解Spring Boot,建议你购买我的书来学习Spring Boot
创建子系统,可以进入代码生成>子系统生成, 输入maven项目路径,还有包名,就可以直接生成一个可运行的基于SpringBoot-Plus 的子系统
### 1.2.2 创建Controller
### 1.2.1 配置子系统
### 1.2.3 创建Service
### 1.2.2 添加代码
### 1.2.4 创建Dao
## 1.3 代码生成
## 1.3 业务代码生成
在介绍如何利用Plus开发系统之前,先介绍代码生成功能,此功能可以生成前后端代码总计14个文件,你可以通过预览功能了解如何开发这个系统
......
package com.ibeetl.admin.core.web;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
......@@ -53,6 +63,114 @@ public class CoreCodeGenController {
}
@GetMapping(MODEL + "/project.do")
public ModelAndView project() {
ModelAndView view = new ModelAndView("/core/codeGen/project.html");
File file = new File(MavenProjectTarget.detectRootPath());
String root = file.getParent();
//设置生成项目为当前运行项目的上一级项目
view.addObject("path",root+File.separator+"sample");
view.addObject("basePackage","com.corp.xxx");
return view;
}
@PostMapping(MODEL + "/projectGen.json")
@ResponseBody
public JsonResult project(String path,String basePackage,String includeConsole) throws IOException {
//includeConsole 当前版本忽略,总是添加一个系统管理功能,可以在pom中移除console
//生成maven项目结构
File maven = new File(path);
maven.mkdirs();
File src = new File(maven,"src");
src.mkdirs();
File main = new File(src,"main");
main.mkdir();
File test = new File(src,"test");
test.mkdir();
File javsSource = new File(main,"java");
javsSource.mkdir();
File resource = new File(main,"resources");
resource.mkdir();
File sql = new File(resource,"sql");
sql.mkdir();
File staticFile = new File(resource,"static");
staticFile.mkdir();
File templatesFile = new File(resource,"templates");
templatesFile.mkdir();
String codePath = basePackage.replace(".", "/");
File codeFile = new File(javsSource,codePath);
codeFile.mkdirs();
Configuration conf = Configuration.defaultConfiguration();
String tempalteRoot = "codeTemplate/maven/";
ClasspathResourceLoader loader = new ClasspathResourceLoader(Thread.currentThread().getContextClassLoader(),tempalteRoot);
GroupTemplate gt = new GroupTemplate(loader,conf);
FileWriter fw = null;
//先生成入口程序
Template mainJavaTempalte = gt.getTemplate("/main.java");
mainJavaTempalte.binding("basePackage", basePackage);
fw = new FileWriter(new File(codeFile,"MainApplication.java"));
mainJavaTempalte.renderTo(fw);
//生成pom文件
Template pomTemplate = gt.getTemplate("/pomTemplate.xml");
int index = basePackage.lastIndexOf(".");
String projectGrop = basePackage.substring(0, index);
String projectName = basePackage.substring(index+1);
pomTemplate.binding("group", projectGrop);
pomTemplate.binding("project", projectName);
fw = new FileWriter(new File(maven,"pom.xml"));
pomTemplate.renderTo(fw);
//复制当前项目的配置文件
File config = copy(resource,"application.properties");
copy(resource,"beetl.properties");
copy(resource,"btsql-ext.properties");
copy(resource,"banner.txt");
//修改application.properties的配置,改成手工添加
// Properties ps = new Properties();
// ps.load(new FileReader(config));
//// String str = ps.getProperty("beetlsql.basePackag");
// ps.put("beetlsql.basePackag", "ibeetl.com,"+basePackage);
// ps.store(new FileWriter(config), "");
//
return JsonResult.success();
}
private File copy(File root,String fileName) throws IOException {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream input = loader.getResourceAsStream(fileName);
if(input==null) {
log.info("copy "+fileName+" error,不存在");
return null;
}
File target = new File(root,fileName);
FileOutputStream output = new FileOutputStream(target);
try {
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
} finally {
input.close();
output.close();
}
return target;
}
@PostMapping(MODEL + "/table.json")
@ResponseBody
public JsonResult<List<Entity>> getTable() {
......@@ -220,6 +338,9 @@ public class CoreCodeGenController {
return entity;
}
@GetMapping(MODEL + "/{table}/test.json")
@ResponseBody
public void test(@PathVariable String table) {
......
package ${basePackage};
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.WebApplicationInitializer;
@SpringBootApplication
@EnableCaching
@ComponentScan(basePackages= {"${basePackage}","com.ibeetl.admin"})
public class MainApplication extends SpringBootServletInitializer implements WebApplicationInitializer {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
\ No newline at end of file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>${group}</groupId>
<artifactId>${project}</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<commons-lang3.version>3.3.2</commons-lang3.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RC1</version>
</parent><!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.5</version>
</dependency>
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>admin-core</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>admin-console</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<!-- Add Spring repositories -->
<!-- (you don't need this if you are using a .RELEASE version) -->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
\ No newline at end of file
......@@ -28,9 +28,12 @@ layui.define([], function(exports) {
var form = $('#updateForm');
var formPara = form.serializeJson();
Common.post("/core/codeGen/sql.json", formPara, callback);
},
genProject:function(form,callback){
var formPara = form.serializeJson();
Common.post("/core/codeGen/projectGen.json", formPara, callback);
}
};
exports('codeApi',api);
......
layui.define([ 'form','codeApi'], function(exports) {
var form = layui.form;
var codeApi = layui.codeApi;
var view = {
init:function(){
this.initSubmit();
},
initSubmit:function(){
$("#genProject").click(function(){
codeApi.genProject($('#projectForm'),function(){
Common.info("生成项目成功,请用IDE导入新的项目");
});
});
$("#genProject-cancel").click(function(){
Lib.closeFrame();
});
}
}
exports('project',view);
});
\ No newline at end of file
<div class="layui-row">
<div class="layui-form-item" style="position:absolute;right:10px;bottom:0px;">
<button class="layui-btn layui-btn-danger" type="button" id="${id}">保存</button>
<button class="layui-btn layui-btn-danger" type="button" id="${id}">${text!"保存"}</button>
<button class="layui-btn" type="button" id="${id}-cancel">取消</button>
</div>
</div>
\ No newline at end of file
<!--# layout("/common/layout.html",{"jsBase":"/js/core/codeGen/"}){ -->
<form class="layui-form layui-form-pane" id="projectForm">
<div class="layui-form-item">
<label class="layui-form-label">路径</label>
<div class="layui-input-block">
<input type="text" name="path" required lay-verify="required" value="${path}" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">包名</label>
<div class="layui-input-block">
<input type="text" name="basePackage" required lay-verify="required" value="${basePackage}" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">添加管理</label>
<div class="layui-input-inline">
<input type="checkbox" name="includeConsole" lay-skin="switch">
</div>
</div>
<layui:submitButtons id="genProject" text="立即生成工程" />
</form>
<!--#} -->
<script>
layui.use(['project'], function(){
var project = layui.project
project.init();
});
</script>
doc/readme/codegen.png

79.3 KB | W: | H:

doc/readme/codegen.png

78.9 KB | W: | H:

doc/readme/codegen.png
doc/readme/codegen.png
doc/readme/codegen.png
doc/readme/codegen.png
  • 2-up
  • Swipe
  • Onion skin
doc/readme/codegen2.png

94.8 KB | W: | H:

doc/readme/codegen2.png

95.1 KB | W: | H:

doc/readme/codegen2.png
doc/readme/codegen2.png
doc/readme/codegen2.png
doc/readme/codegen2.png
  • 2-up
  • Swipe
  • Onion skin
doc/readme/codeoverview.png

71.7 KB | W: | H:

doc/readme/codeoverview.png

60.3 KB | W: | H:

doc/readme/codeoverview.png
doc/readme/codeoverview.png
doc/readme/codeoverview.png
doc/readme/codeoverview.png
  • 2-up
  • Swipe
  • Onion skin
doc/readme/data.png

59 KB | W: | H:

doc/readme/data.png

60.4 KB | W: | H:

doc/readme/data.png
doc/readme/data.png
doc/readme/data.png
doc/readme/data.png
  • 2-up
  • Swipe
  • Onion skin
doc/readme/role.png

79.4 KB | W: | H:

doc/readme/role.png

73.3 KB | W: | H:

doc/readme/role.png
doc/readme/role.png
doc/readme/role.png
doc/readme/role.png
  • 2-up
  • Swipe
  • Onion skin
doc/readme/user.png

323 KB | W: | H:

doc/readme/user.png

68.3 KB | W: | H:

doc/readme/user.png
doc/readme/user.png
doc/readme/user.png
doc/readme/user.png
  • 2-up
  • Swipe
  • Onion skin
......@@ -10,7 +10,7 @@ Target Server Type : MYSQL
Target Server Version : 50624
File Encoding : 65001
Date: 2018-02-28 16:05:04
Date: 2018-03-01 11:53:01
*/
SET FOREIGN_KEY_CHECKS=0;
......@@ -211,7 +211,7 @@ CREATE TABLE `core_function` (
`PARENT_ID` int(65) DEFAULT NULL,
`TYPE` varchar(4) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=183 DEFAULT CHARSET=utf8;
) ENGINE=InnoDB AUTO_INCREMENT=184 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of core_function
......@@ -235,6 +235,7 @@ INSERT INTO `core_function` VALUES ('167', 'workflow.admin', '工作流监控',
INSERT INTO `core_function` VALUES ('180', 'code.query', '代码生成测试', null, null, '12', 'FN0');
INSERT INTO `core_function` VALUES ('181', 'blog.query', '博客查询功能', null, '', '182', 'FN0');
INSERT INTO `core_function` VALUES ('182', 'blog', '博客测试', null, '/admin/blog/index.do', '0', 'FN0');
INSERT INTO `core_function` VALUES ('183', 'code.project', '项目生成', '2018-03-01 09:38:17.068000', '/core/codeGen/project.do', '12', 'FN0');
-- ----------------------------
-- Table structure for core_menu
......@@ -251,7 +252,7 @@ CREATE TABLE `core_menu` (
`SEQ` int(65) DEFAULT NULL,
`ICON` varchar(255) DEFAULT NULL COMMENT '图标',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8;
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of core_menu
......@@ -264,13 +265,15 @@ INSERT INTO `core_menu` VALUES ('13', '菜单项', '菜单项', null, '8', 'MENU
INSERT INTO `core_menu` VALUES ('14', '功能点管理', '功能点管理', null, '9', 'MENU_M', '18', '5', null);
INSERT INTO `core_menu` VALUES ('15', '字典数据管理', '字典数据管理', null, '13', 'MENU_M', '18', '6', null);
INSERT INTO `core_menu` VALUES ('16', '审计查询', '审计查询', null, '18', 'MENU_M', '19', '7', null);
INSERT INTO `core_menu` VALUES ('17', '代码生成', '代码生成', null, '12', 'MENU_M', '19', '8', null);
INSERT INTO `core_menu` VALUES ('17', '代码生成', '代码生成', null, '12', 'MENU_M', '24', '8', '');
INSERT INTO `core_menu` VALUES ('18', '基础管理', '基础管理', null, null, 'MENU_N', '8', '1', null);
INSERT INTO `core_menu` VALUES ('19', '监控管理', '监控管理', null, null, 'MENU_N', '8', '2', null);
INSERT INTO `core_menu` VALUES ('20', '流程监控', '流程监控', null, '167', 'MENU_M', '19', '3', null);
INSERT INTO `core_menu` VALUES ('21', '角色功能授权', '角色功能授权', null, '10', 'MENU_M', '18', '8', null);
INSERT INTO `core_menu` VALUES ('22', '角色数据授权', '角色数据授权', null, '11', 'MENU_M', '18', '9', null);
INSERT INTO `core_menu` VALUES ('23', '博客测试', '博客测试1', null, '182', 'MENU_M', '19', '9', '');
INSERT INTO `core_menu` VALUES ('24', '代码生成导航', '代码生成', '2018-03-01 09:39:31.096000', null, 'MENU_N', '8', '1', '');
INSERT INTO `core_menu` VALUES ('25', '子系统生成', '子系统生成', '2018-03-01 09:42:35.839000', '183', 'MENU_M', '24', '1', '');
-- ----------------------------
-- Table structure for core_org
......@@ -324,7 +327,7 @@ INSERT INTO `core_role` VALUES ('15', 'admin', 'ivy', '2017-09-06 05:35:04.00000
INSERT INTO `core_role` VALUES ('17', '123', '我', '2017-09-06 21:23:03.000000', 'R0');
INSERT INTO `core_role` VALUES ('18', '23', '234', '2017-09-06 21:41:03.000000', 'R0');
INSERT INTO `core_role` VALUES ('19', '132484', '1', '2017-09-06 21:42:02.000000', 'R0');
INSERT INTO `core_role` VALUES ('173', 'dept.admin', '部门辅助管理员', '2017-10-25 10:29:03.000000', 'R0');
INSERT INTO `core_role` VALUES ('173', 'dept.admin', '部门辅助管理员', null, 'R0');
-- ----------------------------
-- Table structure for core_role_function
......@@ -343,7 +346,7 @@ CREATE TABLE `core_role_function` (
-- Records of core_role_function
-- ----------------------------
INSERT INTO `core_role_function` VALUES ('1', '1', '1', '5', null);
INSERT INTO `core_role_function` VALUES ('2', '1', '2', '1', null);
INSERT INTO `core_role_function` VALUES ('2', '1', '2', '4', null);
INSERT INTO `core_role_function` VALUES ('3', '1', '3', '5', null);
INSERT INTO `core_role_function` VALUES ('4', '2', '2', '2', null);
INSERT INTO `core_role_function` VALUES ('5', '3', '2', '5', null);
......
Supports Markdown
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