Skip to content

Commit

Permalink
issue #33
Browse files Browse the repository at this point in the history
  • Loading branch information
enilu committed Aug 22, 2019
1 parent eb33ba2 commit ace92ae
Show file tree
Hide file tree
Showing 39 changed files with 2,058 additions and 83 deletions.
103 changes: 28 additions & 75 deletions docs/ecosystem/code-generator.md
Original file line number Diff line number Diff line change
@@ -1,76 +1,27 @@
# guns-lite代码生成工具
# 代码生成

## 使用方法

- 准备工作
```shell

-- 克隆本项目
git clone https://github.com/enilu/sbvue-coder.git

-- 安装到本地仓库
cd sbvue-coder
mvn install

--在guns-lite的guns-entity的pom.xml文件中添加依赖:guns-lite/guns-entity/pom.xml

## 用法
- 在guns-entity/pom.xml中添加依赖
```xml
<dependency>
<groupId>cn.enilu</groupId>
<artifactId>sbvue-coder</artifactId>
<version>1.0</version>
<artifactId>guns-generator</artifactId>
<version>${project.version}</version>
</dependency>
```
- 下载intellij代码生成插件,在插件中心搜索并安装插件:webflash-generator

- 在Intellij IDEA中新建一个Run Config,如下图所示
![run config](./doc/run_config.jpg)
- 上图三个配置分别为
- 1,运行的代码生成主类
- 2,代码生成参数,详见下面说明
- 3,在哪个模块中执行代码生成:guns-entity,因为代码生成是通过读取guns-entity中的实体类的注解实现,所以这里选择guns-entity

- 新建配置文件:guns-entity/src/main/resources/code/code.json,配置文件内容如下,下面内容指定了生成的相关代码分别放在哪个模块下,考虑到有的同学可能会更改项目名和模块名,增加该配置,开发人员可以自行配置模块名
```json
{
"codeConfig": {
"type": "cn.enilu.sbvue.code.CodeConfig",
"fields": {
entityModel: "guns-entity",
daoModel: "guns-dao",
serviceModel: "guns-service",
controllerModel: "guns-api",
viewModel: "guns-admin-vuejs"
}
}
}
```

## 代码生成参数
执行生成的时候,在Program arguments配置为:-h 运行后将会输出下面参数帮助提示

```shell
usage: Generator [options] [all|entity|service|controller|view]
-f,--force force generate file even if file exists
-h,--help show help message
-i,--include <arg> include table pattern
-module,--module <arg> current module name
-p,--package <arg> base package name,default:cn.enilu.guns
-u,--base-uri <arg> base uri prefix, default is /
-x,--exclude <arg> exclude table pattern
```
比如在Program arguments配置 -mudule test 将会根据guns-entity/src/main/java/cn/enilu/guns/bean/entity/test/目录下所有的java类生成其CRUD相关代码
如果程序运行参数更改为 -mudule test -i t_test_boy 将会根据上述目录下的Boy实体生成其相关CRUD代码,注意这里-i 后面的参数值是实体中配置的表名称,而不是实体名本身。
测试实体Boy内容如下
- 写好实体类,例如:
```java
package cn.enilu.guns.bean.entity.test;
import cn.enilu.guns.bean.entity.BaseEntity;
import lombok.Data;
import org.hibernate.annotations.Table;

import javax.persistence.Column;
import javax.persistence.Entity;

@Entity(name="t_test_boy")
@Table(appliesTo = "t_test_boy",comment = "男孩")
@Data
Expand All @@ -84,19 +35,21 @@ public class Boy extends BaseEntity {
@Column(name = "has_girl_friend",columnDefinition = "TINYINT COMMENT '是否有女朋友'")
private Boolean hasGirFriend;
}
```
注意如果要根据实体生成代码,实体中的@Table注解要配置为@org.hibernate.annotations.Table而不是能配置为:@javax.persistence.Table,因为前者可以配置注释comment
以Boy实体为例,执行代码生成后将会生成如下文件:
![run code_add](./doc/code_add.jpg)
代码生成后,在guns-admin-vuejs的路由配置文件中guns-admin-vuejs/src/store/modules/permission.js,增加如下红框内容:
![run router](./doc/router.jpg)
启动guns-api和guns-admin-vuejs,配置和菜单和角色权限后就可以访问刚刚生成的代码功能了
![run code_result](./doc/code_result.jpg)
```
- 上面实体类注意事项
- @Table注解要使用org.hibernate.annotations.Table 不要使用javax.persistence.Table
- @Table注解 必须配置表名(applyiesTo)和注释(comment)
- @Column注解必须配置columnDefinition来表述列信息(英文全部大写):包括类型,注释COMMENT
- 实体类必须继承BaseEntity
- 实体类准备好了后,打开实体类,右键点击“Generator"-->"web-flash-mvc",弹出如下图所示对话框
![code-generator](./doc/code-generate.png)
**注意**不用更改对话框中的配置(大部分没有什么作用)
- 运行生成代码后,将会生成controller,service,repository,以及对应的增上改查页面和js,以Boy为例,生成的代码如下所示:
![generate-result](./doc/generate-result.png)
- 代码生成后,在系统中配置对应的菜单和权限,即可使用
![菜单配置](./doc/menu1.png)
![菜单配置](./doc/menu2.png)
![菜单配置](./doc/menu-list.png)
![权限配置](./doc/role.png)

![功能预览](./doc/boy.png)
Binary file added docs/ecosystem/doc/boy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/ecosystem/doc/code-generate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/ecosystem/doc/code_add.jpg
Binary file not shown.
Binary file removed docs/ecosystem/doc/code_result.jpg
Binary file not shown.
Binary file added docs/ecosystem/doc/generate-result.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/ecosystem/doc/menu-list.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/ecosystem/doc/menu1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/ecosystem/doc/menu2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/ecosystem/doc/role.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/ecosystem/doc/router.jpg
Binary file not shown.
Binary file removed docs/ecosystem/doc/run_config.jpg
Binary file not shown.
2 changes: 1 addition & 1 deletion guns-admin/src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## 开发环境配置
spring.datasource.url=jdbc:mysql://enilu-db:3306/gunslite?useUnicode=true&characterEncoding=UTF8
spring.datasource.url=jdbc:mysql://localhost:3306/gunslite?useUnicode=true&characterEncoding=UTF8
spring.datasource.username=gunslite
spring.datasource.password=gunslite123

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ MenuInfoDlg.editSubmit = function () {
*/
MenuInfoDlg.onClickDept = function (e, treeId, treeNode) {
$("#pcodeName").attr("value", MenuInfoDlg.ztreeInstance.getSelectedVal());
$("#pcode").attr("value", treeNode.id);
$("#pcode").attr("value", treeNode.code);
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public interface MenuRepository extends BaseRepository<Menu,Long> {
List getMenus();
@Query(nativeQuery = true,value="select menuid from t_sys_relation where roleid=?1")
List getMenuIdsByRoleId(Integer roleId);
@Query(nativeQuery = true,value = "SELECT m1.id AS id, ( CASE WHEN (m2.id = 0 OR m2.id IS NULL) THEN 0 ELSE m2.id END ) AS pId, m1. NAME AS NAME, ( CASE WHEN (m2.id = 0 OR m2.id IS NULL) THEN 'true' ELSE 'false' END ) AS isOpen FROM t_sys_menu m1 LEFT JOIN t_sys_menu m2 ON m1.pcode = m2. CODE ORDER BY m1.id ASC")
@Query(nativeQuery = true,value = "SELECT m1.id AS id, ( CASE WHEN (m2.id = 0 OR m2.id IS NULL) THEN 0 ELSE m2.id END ) AS pId, m1. NAME AS NAME, ( CASE WHEN (m2.id = 0 OR m2.id IS NULL) THEN 'true' ELSE 'false' END ) AS isOpen,m1.code FROM t_sys_menu m1 LEFT JOIN t_sys_menu m2 ON m1.pcode = m2. CODE ORDER BY m1.id ASC")
List menuTreeList();
@Query(nativeQuery = true,value = "SELECT m1.id AS id, ( CASE WHEN (m2.id = 0 OR m2.id IS NULL) THEN 0 ELSE m2.id END ) AS pId, m1. NAME AS NAME, ( CASE WHEN (m2.id = 0 OR m2.id IS NULL) THEN 'true' ELSE 'false' END ) AS isOpen, ( CASE WHEN (m3.ID = 0 OR m3.ID IS NULL) THEN 'false' ELSE 'true' END ) \"checked\" FROM t_sys_menu m1 LEFT JOIN t_sys_menu m2 ON m1.pcode = m2. CODE LEFT JOIN ( SELECT ID FROM t_sys_menu WHERE ID IN (?1)) m3 ON m1.id = m3.id ORDER BY m1.id ASC")
List menuTreeListByMenuIds(List<Long> menuIds);
Expand Down
5 changes: 5 additions & 0 deletions guns-entity/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@


<dependencies>
<dependency>
<groupId>cn.enilu</groupId>
<artifactId>guns-generator</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>cn.enilu</groupId>
<artifactId>guns-utils</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class ZTreeNode {
private Long pId;//父节点id

private String name;//节点名称
private String code;//节点编号

private Boolean open;//是否打开节点

Expand Down Expand Up @@ -45,6 +46,14 @@ public void setName(String name) {
this.name = name;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public Boolean getOpen() {
return open;
}
Expand Down
10 changes: 5 additions & 5 deletions guns-entity/src/main/resources/code/code.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"codeConfig": {
"type": "cn.enilu.sbvue.code.CodeConfig",
"type": "cn.enilu.flash.code.CodeConfig",
"fields": {
entityModel: "guns-entity",
daoModel: "guns-dao1",
serviceModel: "guns-service2",
controllerModel: "guns-api",
viewModel: "guns-admin-vuejs"
daoModel: "guns-dao",
serviceModel: "guns-service",
controllerModel: "guns-admin",
viewModel: "guns-admin"
}
}
}
49 changes: 49 additions & 0 deletions guns-generator/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>guns-lite</artifactId>
<groupId>cn.enilu</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>guns-generator</artifactId>
<properties>
<java-version>1.8</java-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<evo-inflector-version>1.0.1</evo-inflector-version>
<velocity-version>1.7</velocity-version>
<commons-cli-version>1.2</commons-cli-version>
<spring.boot.version>2.1.1.RELEASE</spring.boot.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.atteo</groupId>
<artifactId>evo-inflector</artifactId>
<version>${evo-inflector-version}</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>${commons-cli-version}</version>
</dependency>

<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>${velocity-version}</version>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutz</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cn.enilu.flash.code;

import org.nutz.ioc.Ioc;

import java.util.Map;

/**
* 基础的数据结构加载器<br>
* @author : zhangtao <br>
* 创建日期: 16-7-12<br>
*/
public abstract class AbstractLoader {

public abstract Map<String, TableDescriptor> loadTables(Ioc ioc,
String basePackageName, String baseUri,
String servPackageName,
String repositoryPackageName,
String modPackageName) throws Exception;


}
63 changes: 63 additions & 0 deletions guns-generator/src/main/java/cn/enilu/flash/code/CodeConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cn.enilu.flash.code;

public class CodeConfig {
private String entityModel = "guns-entity";
private String daoModel = "guns-dao";
private String serviceModel = "guns-service";
private String controllerModel = "guns-api";
private String viewModel = "guns-admin-vuejs";
public String getModel(String type){
switch (type){
case "model":
return entityModel;
case "repository":
return daoModel;
case "service":
return serviceModel;
case "controller":
return controllerModel;
case "view":
return viewModel;
}
return null;
}
public String getEntityModel() {
return entityModel;
}

public void setEntityModel(String entityModel) {
this.entityModel = entityModel;
}

public String getDaoModel() {
return daoModel;
}

public void setDaoModel(String daoModel) {
this.daoModel = daoModel;
}

public String getServiceModel() {
return serviceModel;
}

public void setServiceModel(String serviceModel) {
this.serviceModel = serviceModel;
}

public String getControllerModel() {
return controllerModel;
}

public void setControllerModel(String controllerModel) {
this.controllerModel = controllerModel;
}

public String getViewModel() {
return viewModel;
}

public void setViewModel(String viewModel) {
this.viewModel = viewModel;
}
}
Loading

0 comments on commit ace92ae

Please sign in to comment.