├── .gitignore ├── LICENSE ├── README.md ├── lib └── ojdbc8.jar ├── pom.xml └── src ├── main ├── java │ └── pg │ │ └── laziji │ │ └── generator │ │ ├── GeneratorApplication.java │ │ ├── constant │ │ └── KeyConsts.java │ │ ├── model │ │ ├── Column.java │ │ ├── Table.java │ │ ├── TableItem.java │ │ └── TemplateContext.java │ │ ├── service │ │ ├── GeneratorService.java │ │ ├── TableService.java │ │ └── impl │ │ │ ├── gen │ │ │ ├── CodeGeneratorServiceImpl.java │ │ │ └── DocGeneratorServiceImpl.java │ │ │ └── table │ │ │ ├── BaseTableService.java │ │ │ ├── MySQLTableServiceImpl.java │ │ │ ├── OracleTableServiceImpl.java │ │ │ └── PostgreSQLTableServiceImpl.java │ │ └── util │ │ ├── ConfigUtils.java │ │ ├── SpringContextUtils.java │ │ └── TemplateUtils.java └── resources │ ├── application-example.yml │ ├── mybatis-default │ ├── Dao.java.vm │ ├── Mapper.xml.vm │ ├── Model.java.vm │ └── Query.java.vm │ ├── mybatis-plus │ ├── Entity.java.vm │ ├── Mapper.java.vm │ ├── Service.java.vm │ └── ServiceImpl.java.vm │ └── mybatis2 │ ├── Dao.java.vm │ ├── Model.java.vm │ ├── Query.java.vm │ └── Service.java.vm └── test └── java └── pg └── laziji └── generator └── ExampleTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Maven build files 2 | target 3 | *.log 4 | maven-eclipse.xml 5 | build.properties 6 | site-content 7 | *~ 8 | 9 | # IntelliJ IDEA files 10 | .idea 11 | .iws 12 | *.iml 13 | *.ipr 14 | 15 | # Eclipse files 16 | .settings 17 | .classpath 18 | .project 19 | .externalToolBuilders 20 | .checkstyle 21 | 22 | src/test/java/pg/laziji/generator/** 23 | !src/test/java/pg/laziji/generator/ExampleTest.java 24 | 25 | src/main/resources/*.yml 26 | !src/main/resources/application-example.yml 27 | 28 | output -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 辣子鸡 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Code Generator 2 | ![](https://img.shields.io/github/languages/top/github-laziji/code-generator.svg?style=flat) 3 | ![](https://img.shields.io/github/stars/gitHub-laziji/code-generator.svg?style=social) 4 | 5 | 6 | 7 | 自定义代码自动生成器, 目前包含Java数据库`Mapper, Dao, Service`层模板 8 | 9 | > 欢迎贡献各种模版 10 | 11 | # 已有模板 12 | 目前项目中包含这些模版在`resources`下, 如果模版不合适可以自己模仿其中的模版进行修改 13 | 14 | - `mybatis2` 是根据 [commons-mybatis](https://github.com/GitHub-Laziji/commons-mybatis) 通用`Mapper`编写的, 依赖`commons-mybatis 2.0` 15 | - `mybatis-default` 这个生成的是简单的`mybatis`实体类、Dao接口以及XML, 不依赖其他包 16 | - `mybatis-plus` 生成mybatis-plus使用的实体类以及Mapper文件, 依赖`mybatis-plus` 17 | 18 | 19 | # 配置文件 20 | 在`resources`下创建`application-${name}.yml`文件, `${name}`随意, 例如: `application-example.yml`, 可创建多个 21 | 22 | 配置文件属性: 23 | - `generator.datasource` 填入自己的项目数据库相关配置 24 | - `generator.package` 项目包名 25 | - `generator.template.base-path` 表示模版文件的路径目前可选模板请看 [已有模板](#已有模板) 26 | - `generator.template.output-paths` 模板输出相对路径, 每行一个, 格式为`[模板文件名]: [模板输出相对路径,可使用{}引入动态变量]`可选动态变量包含: 27 | - - `{packagePath}` 包文件路径, 例如: `com/xxx/xxx` 28 | - - `{className}` 类名, 由表名转成驼峰命名法得来 可覆盖 29 | - - `{lowercaseClassName}` 首字母小写的类名 30 | - - 以及其他`dynamicPath`域中的自定义属性, `'\'`为转义符, 其后的字符不进行解析 31 | 32 | 一般按以下配置即可, 数据库支持`mysql`和`oracle` 33 | ```yml 34 | generator: 35 | datasource: 36 | type: mysql 37 | url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxxx?characterEncoding=utf-8 38 | username: xxxxxx 39 | password: xxxxxx 40 | 41 | package: com.g.example 42 | template: 43 | base-path: mybatis-plus 44 | output-paths: | 45 | Entity.java.vm: src/main/java/{packagePath}/entity/{className}.java 46 | Mapper.java.vm: src/main/java/{packagePath}/mapper/{className}Mapper.java 47 | Service.java.vm: src/main/java/{packagePath}/service/{className}Service.java 48 | ServiceImpl.java.vm: src/main/java/{packagePath}/service/impl/{className}ServiceImpl.java 49 | ``` 50 | 51 | # 使用 52 | 在test文件下创建测试类 53 | - `@ActiveProfiles("example")`中填入刚才配置文件名的`name` 54 | - `tableNames`需要生成的表, 可以多个, `newBuilder`使用构造类 自定义类名, 以及其他参数 55 | - `zipPath` 代码导出路径 56 | 57 | 调用`generatorService.generateZip`传入参数可以是表名数组`String[]`或者`TableItem[]` 58 | 59 | 运行测试方法 60 | ```Java 61 | package pg.laziji.generator; 62 | 63 | import org.junit.Test; 64 | import org.junit.runner.RunWith; 65 | import org.springframework.boot.test.context.SpringBootTest; 66 | import org.springframework.test.context.ActiveProfiles; 67 | import org.springframework.test.context.junit4.SpringRunner; 68 | import pg.laziji.generator.model.TableItem; 69 | import pg.laziji.generator.service.GeneratorService; 70 | 71 | import javax.annotation.Resource; 72 | 73 | @ActiveProfiles("example") 74 | @RunWith(SpringRunner.class) 75 | @SpringBootTest 76 | public class ExampleTest { 77 | 78 | @Resource 79 | private GeneratorService generatorService; 80 | 81 | @Test 82 | public void test() { 83 | generatorService.generateZip(new TableItem[]{ 84 | TableItem.newBuilder() 85 | .tableName("table1") 86 | .dynamicPathVariable("className", "TableA") 87 | .build(), 88 | new TableItem("table2") 89 | }, "/home/code.zip"); 90 | } 91 | } 92 | ``` 93 | 94 | # 各个变量域包含的字段 95 | 96 | ### system 97 | 只能在VM模板中使用, 可以通过`TemplateContext.setSystemVariable(key, value)`添加和覆盖, 98 | 相当于全局变量, 初始包含以下信息, value的值可以是`Class`, 例如放入`CommonUtils.class` 在模板中就可以调用该类中的静态方法 99 | 参考下面的`config`和`utils` 100 | 101 | - `config` 可以在模板中通过这个获取所有配置文件信息`system.config.get("xxx")` 102 | - `utils` 工具类, 里面包含格式化当前时间的函数, 可以扩展`system.utils.time("yyyy/MM/dd HH:mm:ss")` 103 | 104 | - `username` 系统用户名 105 | - `computerName` 计算机名 106 | - `osName` 操作系统名称 107 | - `osArch` 架构 108 | - `osVersion` 系统版本 109 | 110 | ### dynamicPath 111 | 可以在VM模板和路径配置中使用, 只能存放``的键值对, 112 | 可以通过`TableItem.Builder.dynamicPathVariable(key, value)`添加和覆盖, 113 | 文件的动态路径变量 114 | 115 | - `packagePath` 包路径 例如`com/e/test` 116 | - `className` 类名 来自表名转驼峰命名法, 可覆盖 117 | - `lowercaseClassName` 首字母小写的类名 118 | 119 | 120 | ### template 121 | 只能在VM模板中使用, 和`dynamicPath`的区别是值可以是`Object`类型, 122 | 可以通过`TableItem.Builder.templateVariable(key, value)`添加和覆盖, 123 | value的值可以是`Class`, 例如放入`CommonUtils.class` 在模板中就可以调用该类中的静态方法 124 | 参考系统变量的`config`和`utils` 125 | 126 | - 默认无 -------------------------------------------------------------------------------- /lib/ojdbc8.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GitHub-Laziji/code-generator/564460d2684da17a20aa94a1cb02209cfe6d19a6/lib/ojdbc8.jar -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | pg.laziji 8 | code-generator 9 | 1.0-SNAPSHOT 10 | 11 | 12 | org.springframework.boot 13 | spring-boot-starter-parent 14 | 2.7.0 15 | 16 | 17 | 18 | 19 | UTF-8 20 | UTF-8 21 | 1.8 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web-services 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-test 32 | test 33 | 34 | 35 | junit 36 | junit 37 | 4.13.2 38 | test 39 | 40 | 41 | com.alibaba 42 | fastjson 43 | 2.0.3 44 | 45 | 46 | org.apache.commons 47 | commons-lang3 48 | 3.12.0 49 | 50 | 51 | org.apache.velocity 52 | velocity-engine-core 53 | 2.3 54 | 55 | 56 | commons-io 57 | commons-io 58 | 2.11.0 59 | 60 | 61 | commons-configuration 62 | commons-configuration 63 | 1.10 64 | 65 | 66 | 67 | 68 | mysql 69 | mysql-connector-java 70 | 8.0.29 71 | 72 | 73 | org.postgresql 74 | postgresql 75 | 42.3.4 76 | 77 | 78 | oracle8 79 | oracle8 80 | 8 81 | system 82 | ${basedir}/lib/ojdbc8.jar 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /src/main/java/pg/laziji/generator/GeneratorApplication.java: -------------------------------------------------------------------------------- 1 | package pg.laziji.generator; 2 | 3 | import org.springframework.boot.autoconfigure.SpringBootApplication; 4 | 5 | @SpringBootApplication 6 | public class GeneratorApplication { 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/pg/laziji/generator/constant/KeyConsts.java: -------------------------------------------------------------------------------- 1 | package pg.laziji.generator.constant; 2 | 3 | public interface KeyConsts { 4 | 5 | String PACKAGE_PATH = "packagePath"; 6 | String CLASS_NAME = "className"; 7 | String LOWERCASE_CLASS_NAME = "lowercaseClassName"; 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/pg/laziji/generator/model/Column.java: -------------------------------------------------------------------------------- 1 | package pg.laziji.generator.model; 2 | 3 | import org.apache.commons.lang.WordUtils; 4 | import org.apache.commons.lang3.StringUtils; 5 | 6 | public class Column { 7 | 8 | private String tableName; 9 | private String columnName; 10 | private String dataType; 11 | private String columnComment; 12 | private Integer columnSize; 13 | private Integer decimalDigits; 14 | private boolean nullAble; 15 | private boolean autoIncrement; 16 | 17 | private String attributeName; 18 | private String uppercaseAttributeName; 19 | private String attributeType; 20 | 21 | 22 | public boolean isAutoIncrement() { 23 | return autoIncrement; 24 | } 25 | 26 | public void setAutoIncrement(boolean autoIncrement) { 27 | this.autoIncrement = autoIncrement; 28 | } 29 | 30 | public boolean isNullAble() { 31 | return nullAble; 32 | } 33 | 34 | public void setNullAble(boolean nullAble) { 35 | this.nullAble = nullAble; 36 | } 37 | 38 | public String getTableName() { 39 | return tableName; 40 | } 41 | 42 | public void setTableName(String tableName) { 43 | this.tableName = tableName; 44 | } 45 | 46 | public Integer getColumnSize() { 47 | return columnSize; 48 | } 49 | 50 | public void setColumnSize(Integer columnSize) { 51 | this.columnSize = columnSize; 52 | } 53 | 54 | public Integer getDecimalDigits() { 55 | return decimalDigits; 56 | } 57 | 58 | public void setDecimalDigits(Integer decimalDigits) { 59 | this.decimalDigits = decimalDigits; 60 | } 61 | 62 | public String getColumnName() { 63 | return columnName; 64 | } 65 | 66 | public void setColumnName(String columnName) { 67 | this.columnName = columnName; 68 | if (this.columnName != null) { 69 | this.uppercaseAttributeName = WordUtils.capitalizeFully(this.columnName.toLowerCase(), new char[]{'_'}) 70 | .replace("_", ""); 71 | this.attributeName = StringUtils.uncapitalize(this.uppercaseAttributeName); 72 | } 73 | } 74 | 75 | public String getDataType() { 76 | return dataType; 77 | } 78 | 79 | public void setDataType(String dataType) { 80 | this.dataType = dataType; 81 | } 82 | 83 | public String getColumnComment() { 84 | return columnComment; 85 | } 86 | 87 | public void setColumnComment(String columnComment) { 88 | this.columnComment = columnComment; 89 | } 90 | 91 | public void setAttributeType(String attributeType) { 92 | this.attributeType = attributeType; 93 | } 94 | 95 | public String getAttributeType() { 96 | return attributeType; 97 | } 98 | 99 | public String getUppercaseAttributeName() { 100 | return uppercaseAttributeName; 101 | } 102 | 103 | public String getAttributeName() { 104 | return attributeName; 105 | } 106 | 107 | } 108 | -------------------------------------------------------------------------------- /src/main/java/pg/laziji/generator/model/Table.java: -------------------------------------------------------------------------------- 1 | package pg.laziji.generator.model; 2 | 3 | import org.apache.commons.lang.WordUtils; 4 | import org.apache.commons.lang3.StringUtils; 5 | 6 | import java.util.List; 7 | 8 | public class Table { 9 | 10 | private String tableName; 11 | private String tableType; 12 | private String tableComment; 13 | private List columns; 14 | 15 | private String className; 16 | private String lowercaseClassName; 17 | 18 | public String getTableName() { 19 | return tableName; 20 | } 21 | 22 | public void setTableName(String tableName) { 23 | this.tableName = tableName; 24 | if (this.tableName != null) { 25 | this.className = WordUtils.capitalizeFully(tableName.toLowerCase(), new char[]{'_'}) 26 | .replace("_", ""); 27 | this.lowercaseClassName = StringUtils.uncapitalize(this.className); 28 | } 29 | } 30 | 31 | public String getTableType() { 32 | return tableType; 33 | } 34 | 35 | public void setTableType(String tableType) { 36 | this.tableType = tableType; 37 | } 38 | 39 | public String getTableComment() { 40 | return tableComment; 41 | } 42 | 43 | public void setTableComment(String tableComment) { 44 | this.tableComment = tableComment; 45 | } 46 | 47 | public List getColumns() { 48 | return columns; 49 | } 50 | 51 | public void setColumns(List columns) { 52 | this.columns = columns; 53 | } 54 | 55 | public String getClassName() { 56 | return className; 57 | } 58 | 59 | public String getLowercaseClassName() { 60 | return lowercaseClassName; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/pg/laziji/generator/model/TableItem.java: -------------------------------------------------------------------------------- 1 | package pg.laziji.generator.model; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class TableItem { 7 | 8 | private String tableName; 9 | private Map dynamicPathVariables = new HashMap<>(); 10 | private Map templateVariables = new HashMap<>(); 11 | 12 | public static Builder newBuilder() { 13 | return new Builder(); 14 | } 15 | 16 | public TableItem() { 17 | 18 | } 19 | 20 | public TableItem(String tableName) { 21 | this.tableName = tableName; 22 | } 23 | 24 | public String getTableName() { 25 | return tableName; 26 | } 27 | 28 | public void setTableName(String tableName) { 29 | this.tableName = tableName; 30 | } 31 | 32 | public Map getDynamicPathVariables() { 33 | return dynamicPathVariables; 34 | } 35 | 36 | public void setDynamicPathVariables(Map dynamicPathVariables) { 37 | this.dynamicPathVariables = dynamicPathVariables; 38 | } 39 | 40 | public Map getTemplateVariables() { 41 | return templateVariables; 42 | } 43 | 44 | public void setTemplateVariables(Map templateVariables) { 45 | this.templateVariables = templateVariables; 46 | } 47 | 48 | public static class Builder { 49 | 50 | private TableItem item = new TableItem(); 51 | 52 | public Builder tableName(String tableName) { 53 | item.setTableName(tableName); 54 | return this; 55 | } 56 | 57 | public Builder dynamicPathVariable(String key, String value) { 58 | item.getDynamicPathVariables().put(key, value); 59 | return this; 60 | } 61 | 62 | public Builder templateVariable(String key, Object value) { 63 | item.getTemplateVariables().put(key, value); 64 | return this; 65 | } 66 | 67 | public TableItem build() { 68 | return item; 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/pg/laziji/generator/model/TemplateContext.java: -------------------------------------------------------------------------------- 1 | package pg.laziji.generator.model; 2 | 3 | import org.apache.commons.lang3.StringUtils; 4 | import org.springframework.core.env.Environment; 5 | import pg.laziji.generator.constant.KeyConsts; 6 | import pg.laziji.generator.util.ConfigUtils; 7 | import pg.laziji.generator.util.SpringContextUtils; 8 | import pg.laziji.generator.util.TemplateUtils; 9 | 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | import java.util.Properties; 13 | 14 | public class TemplateContext { 15 | 16 | private static final Map systemVariables = new HashMap<>(); 17 | 18 | private final Map dynamicPathVariables = new HashMap<>(); 19 | private Map templateVariables; 20 | private Table table; 21 | 22 | static { 23 | Properties properties = System.getProperties(); 24 | systemVariables.put("config", ConfigUtils.class); 25 | systemVariables.put("utils", TemplateUtils.class); 26 | 27 | systemVariables.put("username", System.getenv("USERNAME")); 28 | systemVariables.put("computerName", System.getenv("COMPUTERNAME")); 29 | systemVariables.put("osName", properties.getProperty("os.name")); 30 | systemVariables.put("osArch", properties.getProperty("os.arch")); 31 | systemVariables.put("osVersion", properties.getProperty("os.version")); 32 | } 33 | 34 | public static Builder newBuilder() { 35 | return new Builder(); 36 | } 37 | 38 | public static void setSystemVariable(String key, Object value) { 39 | systemVariables.put(key, value); 40 | } 41 | 42 | public Map toMap() { 43 | Map map = new HashMap<>(); 44 | map.put("system", systemVariables); 45 | 46 | map.put("dynamicPath", dynamicPathVariables); 47 | map.put("template", templateVariables); 48 | map.put("table", table); 49 | return map; 50 | } 51 | 52 | public Table getTable() { 53 | return table; 54 | } 55 | 56 | public void setTable(Table table) { 57 | this.table = table; 58 | } 59 | 60 | public Map getSystemVariables() { 61 | return systemVariables; 62 | } 63 | 64 | public Map getDynamicPathVariables() { 65 | return dynamicPathVariables; 66 | } 67 | 68 | public Map getTemplateVariables() { 69 | return templateVariables; 70 | } 71 | 72 | public void setTemplateVariables(Map templateVariables) { 73 | this.templateVariables = templateVariables; 74 | } 75 | 76 | 77 | public static class Builder { 78 | 79 | private Environment environment = SpringContextUtils.getBean(Environment.class); 80 | 81 | private TemplateContext context = new TemplateContext(); 82 | 83 | private Builder() { 84 | String packageName = environment.getProperty("generator.package", "pkg"); 85 | context.getDynamicPathVariables().put(KeyConsts.PACKAGE_PATH, packageName.replace(".", "/")); 86 | } 87 | 88 | public Builder table(Table table) { 89 | if (table != null) { 90 | context.setTable(table); 91 | context.getDynamicPathVariables().put(KeyConsts.CLASS_NAME, table.getClassName()); 92 | context.getDynamicPathVariables().put(KeyConsts.LOWERCASE_CLASS_NAME, table.getLowercaseClassName()); 93 | } 94 | return this; 95 | } 96 | 97 | public Builder dynamicPathVariables(Map variables) { 98 | if (variables != null) { 99 | Map cloneVariables = new HashMap<>(); 100 | for (Map.Entry entry : variables.entrySet()) { 101 | cloneVariables.put(entry.getKey(), entry.getValue()); 102 | } 103 | String className = cloneVariables.get(KeyConsts.CLASS_NAME); 104 | String lowercaseClassName = cloneVariables.get(KeyConsts.LOWERCASE_CLASS_NAME); 105 | if (StringUtils.isNotBlank(className)) { 106 | cloneVariables.put(KeyConsts.LOWERCASE_CLASS_NAME, StringUtils.uncapitalize(className)); 107 | } else if (StringUtils.isNotBlank(lowercaseClassName)) { 108 | cloneVariables.put(KeyConsts.CLASS_NAME, 109 | lowercaseClassName.substring(0, 1).toUpperCase() + lowercaseClassName.substring(1)); 110 | } 111 | for (Map.Entry entry : cloneVariables.entrySet()) { 112 | context.getDynamicPathVariables().put(entry.getKey(), entry.getValue()); 113 | } 114 | } 115 | return this; 116 | } 117 | 118 | public Builder templateVariables(Map variables) { 119 | context.setTemplateVariables(variables); 120 | return this; 121 | } 122 | 123 | public TemplateContext build() { 124 | return context; 125 | } 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /src/main/java/pg/laziji/generator/service/GeneratorService.java: -------------------------------------------------------------------------------- 1 | package pg.laziji.generator.service; 2 | 3 | public interface GeneratorService { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/pg/laziji/generator/service/TableService.java: -------------------------------------------------------------------------------- 1 | package pg.laziji.generator.service; 2 | 3 | import pg.laziji.generator.model.Table; 4 | 5 | public interface TableService { 6 | 7 | Table getTable(String tableName) throws Exception; 8 | } -------------------------------------------------------------------------------- /src/main/java/pg/laziji/generator/service/impl/gen/CodeGeneratorServiceImpl.java: -------------------------------------------------------------------------------- 1 | package pg.laziji.generator.service.impl.gen; 2 | 3 | import org.apache.commons.io.IOUtils; 4 | import org.apache.velocity.Template; 5 | import org.apache.velocity.VelocityContext; 6 | import org.apache.velocity.app.Velocity; 7 | import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | import org.springframework.beans.factory.annotation.Value; 11 | import org.springframework.stereotype.Service; 12 | import pg.laziji.generator.model.Table; 13 | import pg.laziji.generator.model.TableItem; 14 | import pg.laziji.generator.model.TemplateContext; 15 | import pg.laziji.generator.service.GeneratorService; 16 | import pg.laziji.generator.service.TableService; 17 | import pg.laziji.generator.util.SpringContextUtils; 18 | 19 | import java.io.FileOutputStream; 20 | import java.io.IOException; 21 | import java.io.StringWriter; 22 | import java.util.HashMap; 23 | import java.util.Map; 24 | import java.util.Properties; 25 | import java.util.zip.ZipEntry; 26 | import java.util.zip.ZipOutputStream; 27 | 28 | @Service 29 | public class CodeGeneratorServiceImpl implements GeneratorService { 30 | 31 | static { 32 | Properties prop = new Properties(); 33 | prop.put("file.resource.loader.class", ClasspathResourceLoader.class.getName()); 34 | Velocity.init(prop); 35 | } 36 | 37 | private static final Logger log = LoggerFactory.getLogger(CodeGeneratorServiceImpl.class); 38 | 39 | @Value("${generator.template.base-path:}") 40 | private String templateBasePath; 41 | 42 | @Value("${generator.template.output-paths:}") 43 | private String templateOutputPaths; 44 | 45 | @Value("${generator.datasource.type:mysql}") 46 | private String datasourceType; 47 | 48 | public void generate(String[] tableNames, String outputPath) { 49 | TableItem[] tableItems = new TableItem[tableNames.length]; 50 | for (int i = 0; i < tableNames.length; i++) { 51 | tableItems[i] = new TableItem(tableNames[i]); 52 | } 53 | generate(tableItems, outputPath); 54 | } 55 | 56 | public void generate(TableItem[] tableItems, String outputPath) { 57 | TableService tableService = SpringContextUtils.getBean(datasourceType, TableService.class); 58 | try (FileOutputStream fos = new FileOutputStream(outputPath)) { 59 | try (ZipOutputStream zos = new ZipOutputStream(fos)) { 60 | for (TableItem item : tableItems) { 61 | Table table = tableService.getTable(item.getTableName()); 62 | if (table == null) { 63 | log.warn("表[{}] 信息查询失败", item.getTableName()); 64 | continue; 65 | } 66 | generatorCode(TemplateContext.newBuilder() 67 | .templateVariables(item.getTemplateVariables()) 68 | .table(table) 69 | .dynamicPathVariables(item.getDynamicPathVariables()) 70 | .build(), zos); 71 | } 72 | } 73 | } catch (Exception e) { 74 | e.printStackTrace(); 75 | } 76 | } 77 | 78 | private static String replace(String pattern, Map context) throws Exception { 79 | char[] patternChars = pattern.toCharArray(); 80 | StringBuilder valueBuffer = new StringBuilder(); 81 | StringBuilder variableNameBuffer = null; 82 | boolean inVariable = false; 83 | for (int i = 0; i < patternChars.length; ++i) { 84 | if (!inVariable && patternChars[i] == '{') { 85 | inVariable = true; 86 | variableNameBuffer = new StringBuilder(); 87 | continue; 88 | } 89 | if (inVariable && patternChars[i] == '}') { 90 | inVariable = false; 91 | String variable = context.get(variableNameBuffer.toString()); 92 | valueBuffer.append(variable == null ? "null" : variable); 93 | variableNameBuffer = null; 94 | continue; 95 | } 96 | if (patternChars[i] == '\\' && ++i == patternChars.length) { 97 | throw new Exception("转义符 '\\' 后缺少字符"); 98 | } 99 | StringBuilder activeBuffer = inVariable ? variableNameBuffer : valueBuffer; 100 | activeBuffer.append(patternChars[i]); 101 | } 102 | if (variableNameBuffer != null) { 103 | throw new Exception("结尾缺少 '}' "); 104 | } 105 | return valueBuffer.toString(); 106 | } 107 | 108 | 109 | private void generatorCode(TemplateContext context, ZipOutputStream zos) { 110 | VelocityContext velocityContext = new VelocityContext(context.toMap()); 111 | Map outputPathMap = parseTemplateOutputPaths(context); 112 | for (Map.Entry entry : outputPathMap.entrySet()) { 113 | Template template = Velocity.getTemplate(entry.getKey(), "UTF-8"); 114 | try (StringWriter writer = new StringWriter()) { 115 | template.merge(velocityContext, writer); 116 | zos.putNextEntry(new ZipEntry(entry.getValue())); 117 | IOUtils.write(writer.toString(), zos, "UTF-8"); 118 | zos.closeEntry(); 119 | } catch (IOException e) { 120 | e.printStackTrace(); 121 | } 122 | } 123 | } 124 | 125 | private Map parseTemplateOutputPaths(TemplateContext context) { 126 | String[] rows = templateOutputPaths.split("\n"); 127 | Map outputPathMap = new HashMap<>(); 128 | for (String row : rows) { 129 | int index = row.indexOf(":"); 130 | if (index == -1) { 131 | continue; 132 | } 133 | String fileName = row.substring(0, index).trim(); 134 | try { 135 | String path = replace(row.substring(index + 1).trim(), context.getDynamicPathVariables()); 136 | outputPathMap.put(templateBasePath + "/" + fileName, path); 137 | } catch (Exception e) { 138 | e.printStackTrace(); 139 | } 140 | } 141 | return outputPathMap; 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /src/main/java/pg/laziji/generator/service/impl/gen/DocGeneratorServiceImpl.java: -------------------------------------------------------------------------------- 1 | package pg.laziji.generator.service.impl.gen; 2 | 3 | import org.springframework.stereotype.Service; 4 | import pg.laziji.generator.service.GeneratorService; 5 | 6 | @Service 7 | public class DocGeneratorServiceImpl implements GeneratorService { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/pg/laziji/generator/service/impl/table/BaseTableService.java: -------------------------------------------------------------------------------- 1 | package pg.laziji.generator.service.impl.table; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import pg.laziji.generator.model.Column; 5 | import pg.laziji.generator.model.Table; 6 | import pg.laziji.generator.service.TableService; 7 | 8 | import java.sql.*; 9 | import java.util.*; 10 | 11 | public abstract class BaseTableService implements TableService { 12 | 13 | private final Map typeHandlerMap = new HashMap<>(); 14 | 15 | @Value("${generator.datasource.url}") 16 | private String url; 17 | 18 | @Value("${generator.datasource.username}") 19 | private String username; 20 | 21 | @Value("${generator.datasource.password}") 22 | private String password; 23 | 24 | protected abstract Class getDriverClass(); 25 | 26 | @Override 27 | public Table getTable(String tableName) throws Exception { 28 | try (Connection connection = getConnection()) { 29 | Table table = getMetaDataTable(connection, tableName); 30 | if (table == null) { 31 | return null; 32 | } 33 | List columns = listMetaDataColumn(connection, tableName); 34 | table.setColumns(columns); 35 | return table; 36 | } 37 | } 38 | 39 | protected String analysisDataType(Column column) { 40 | if (column == null || column.getDataType() == null) { 41 | return Object.class.getSimpleName(); 42 | } 43 | return getTypeMappingOrDefault( 44 | column.getDataType().toLowerCase().replace("unsigned", "").trim(), 45 | column, 46 | Object.class 47 | ).getSimpleName(); 48 | } 49 | 50 | protected void addTypeHandler(String dataType, Class clazz) { 51 | this.typeHandlerMap.put(dataType, column -> clazz); 52 | } 53 | 54 | protected void addTypeHandler(String dataType, TypeHandler handler) { 55 | this.typeHandlerMap.put(dataType, handler); 56 | } 57 | 58 | protected Class getTypeMappingOrDefault(String dataType, Column column, Class clazz) { 59 | TypeHandler handler = this.typeHandlerMap.get(dataType); 60 | if (handler == null) { 61 | return clazz; 62 | } 63 | return handler.handle(column); 64 | } 65 | 66 | 67 | private Table getMetaDataTable(Connection connection, String tableName) throws SQLException { 68 | ResultSet resultSet = connection.getMetaData().getTables( 69 | connection.getCatalog(), 70 | connection.getSchema(), 71 | tableName, 72 | null); 73 | if (resultSet.next()) { 74 | if (!Objects.equals(tableName, resultSet.getString("TABLE_NAME"))) { 75 | return null; 76 | } 77 | Table table = new Table(); 78 | table.setTableName(tableName); 79 | table.setTableType(resultSet.getString("TABLE_TYPE")); 80 | table.setTableComment(resultSet.getString("REMARKS")); 81 | return table; 82 | } 83 | return null; 84 | } 85 | 86 | private List listMetaDataColumn(Connection connection, String tableName) throws SQLException { 87 | ResultSet resultSet = connection.getMetaData().getColumns(connection.getCatalog(), 88 | connection.getSchema(), 89 | tableName, 90 | null); 91 | List columns = new ArrayList<>(); 92 | while (resultSet.next()) { 93 | if (!Objects.equals(tableName, resultSet.getString("TABLE_NAME"))) { 94 | continue; 95 | } 96 | 97 | Column column = new Column(); 98 | column.setTableName(tableName); 99 | column.setColumnName(resultSet.getString("COLUMN_NAME")); 100 | column.setDataType(resultSet.getString("TYPE_NAME")); 101 | column.setColumnSize(resultSet.getInt("COLUMN_SIZE")); 102 | column.setDecimalDigits(resultSet.getInt("DECIMAL_DIGITS")); 103 | column.setColumnComment(resultSet.getString("REMARKS")); 104 | 105 | String nullAble = resultSet.getString("IS_NULLABLE"); 106 | if (nullAble != null) { 107 | column.setNullAble("YES".equals(nullAble)); 108 | } 109 | String autoIncrement = resultSet.getString("IS_AUTOINCREMENT"); 110 | if (autoIncrement != null) { 111 | column.setAutoIncrement("YES".equals(autoIncrement)); 112 | } 113 | 114 | column.setAttributeType(analysisDataType(column)); 115 | 116 | columns.add(column); 117 | } 118 | return columns; 119 | } 120 | 121 | private Connection getConnection() throws SQLException { 122 | return DriverManager.getConnection(url, username, password); 123 | } 124 | 125 | @FunctionalInterface 126 | protected interface TypeHandler { 127 | Class handle(Column column); 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /src/main/java/pg/laziji/generator/service/impl/table/MySQLTableServiceImpl.java: -------------------------------------------------------------------------------- 1 | package pg.laziji.generator.service.impl.table; 2 | 3 | 4 | import com.mysql.jdbc.Driver; 5 | import org.springframework.stereotype.Service; 6 | 7 | import java.util.Date; 8 | 9 | @Service("mysql") 10 | public class MySQLTableServiceImpl extends BaseTableService { 11 | 12 | { 13 | addTypeHandler("bit", Boolean.class); 14 | addTypeHandler("tinyint", Integer.class); 15 | addTypeHandler("smallint", Integer.class); 16 | addTypeHandler("mediumint", Integer.class); 17 | addTypeHandler("int", Integer.class); 18 | addTypeHandler("integer", Integer.class); 19 | addTypeHandler("bigint", Long.class); 20 | addTypeHandler("decimal", Long.class); 21 | addTypeHandler("float", Float.class); 22 | addTypeHandler("double", Double.class); 23 | addTypeHandler("date", Date.class); 24 | addTypeHandler("time", Date.class); 25 | addTypeHandler("year", Date.class); 26 | addTypeHandler("datetime", Date.class); 27 | addTypeHandler("timestamp", Date.class); 28 | addTypeHandler("char", String.class); 29 | addTypeHandler("varchar", String.class); 30 | addTypeHandler("tinyblob", String.class); 31 | addTypeHandler("tinytext", String.class); 32 | addTypeHandler("blob", String.class); 33 | addTypeHandler("text", String.class); 34 | addTypeHandler("mediumblob", String.class); 35 | addTypeHandler("mediumtext", String.class); 36 | addTypeHandler("longblob", String.class); 37 | addTypeHandler("longtext", String.class); 38 | } 39 | 40 | @Override 41 | protected Class getDriverClass() { 42 | return Driver.class; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/pg/laziji/generator/service/impl/table/OracleTableServiceImpl.java: -------------------------------------------------------------------------------- 1 | package pg.laziji.generator.service.impl.table; 2 | 3 | import oracle.jdbc.driver.OracleDriver; 4 | import org.springframework.stereotype.Service; 5 | 6 | import java.util.Date; 7 | 8 | @Service("oracle") 9 | public class OracleTableServiceImpl extends BaseTableService { 10 | 11 | { 12 | addTypeHandler("integer", Integer.class); 13 | addTypeHandler("float", Double.class); 14 | addTypeHandler("date", Date.class); 15 | addTypeHandler("timestamp", Date.class); 16 | addTypeHandler("char", String.class); 17 | addTypeHandler("varchar2", String.class); 18 | addTypeHandler("nvarchar2", String.class); 19 | addTypeHandler("long", String.class); 20 | addTypeHandler("clob", String.class); 21 | addTypeHandler("nclob", String.class); 22 | addTypeHandler("number", column -> column.getDecimalDigits() == null || column.getDecimalDigits() == 0 ? Long.class : Double.class); 23 | } 24 | 25 | @Override 26 | protected Class getDriverClass() { 27 | return OracleDriver.class; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/pg/laziji/generator/service/impl/table/PostgreSQLTableServiceImpl.java: -------------------------------------------------------------------------------- 1 | package pg.laziji.generator.service.impl.table; 2 | 3 | import org.postgresql.Driver; 4 | import org.springframework.stereotype.Service; 5 | 6 | import java.util.Date; 7 | 8 | @Service("postgresql") 9 | public class PostgreSQLTableServiceImpl extends BaseTableService { 10 | 11 | { 12 | addTypeHandler("int2", Short.class); 13 | addTypeHandler("int", Integer.class); 14 | addTypeHandler("int4", Integer.class); 15 | addTypeHandler("int8", Long.class); 16 | addTypeHandler("numeric", Long.class); 17 | addTypeHandler("bigserial", Long.class); 18 | addTypeHandler("float", Double.class); 19 | addTypeHandler("date", Date.class); 20 | addTypeHandler("timestamp", Date.class); 21 | addTypeHandler("varchar", String.class); 22 | addTypeHandler("text", String.class); 23 | } 24 | 25 | @Override 26 | protected Class getDriverClass() { 27 | return Driver.class; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/pg/laziji/generator/util/ConfigUtils.java: -------------------------------------------------------------------------------- 1 | package pg.laziji.generator.util; 2 | 3 | import org.springframework.core.env.Environment; 4 | 5 | public class ConfigUtils { 6 | 7 | public static String get(String key) { 8 | Environment environment = SpringContextUtils.getBean(Environment.class); 9 | return environment.getProperty(key); 10 | } 11 | 12 | public static String getOrDefault(String key, String defaultValue) { 13 | Environment environment = SpringContextUtils.getBean(Environment.class); 14 | return environment.getProperty(key, defaultValue); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/pg/laziji/generator/util/SpringContextUtils.java: -------------------------------------------------------------------------------- 1 | package pg.laziji.generator.util; 2 | 3 | import org.springframework.beans.BeansException; 4 | import org.springframework.context.ApplicationContext; 5 | import org.springframework.context.ApplicationContextAware; 6 | import org.springframework.stereotype.Component; 7 | 8 | @Component 9 | public class SpringContextUtils implements ApplicationContextAware { 10 | 11 | private static ApplicationContext context; 12 | 13 | @Override 14 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 15 | context = applicationContext; 16 | } 17 | 18 | public static Object getBean(String name) { 19 | return context.getBean(name); 20 | } 21 | 22 | public static T getBean(Class clazz) { 23 | return context.getBean(clazz); 24 | } 25 | 26 | public static T getBean(String name, Class clazz) { 27 | return context.getBean(name, clazz); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/pg/laziji/generator/util/TemplateUtils.java: -------------------------------------------------------------------------------- 1 | package pg.laziji.generator.util; 2 | 3 | import java.text.DateFormat; 4 | import java.text.SimpleDateFormat; 5 | import java.util.Date; 6 | 7 | public class TemplateUtils { 8 | 9 | public static String time(String format){ 10 | return new SimpleDateFormat(format).format(new Date()); 11 | } 12 | 13 | public static String time(){ 14 | return DateFormat.getDateTimeInstance().format(new Date()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/resources/application-example.yml: -------------------------------------------------------------------------------- 1 | 2 | generator: 3 | # code generator 4 | datasource: 5 | # mysql or oracle, default mysql 6 | type: mysql 7 | url: jdbc:mysql://127.0.0.1:3306/example?characterEncoding=utf-8 8 | username: root 9 | password: root 10 | package: com.g.example 11 | template: 12 | base-path: mybatis-plus 13 | output-paths: | 14 | Entity.java.vm: src/main/java/{packagePath}/entity/{className}.java 15 | Mapper.java.vm: src/main/java/{packagePath}/mapper/{className}Mapper.java 16 | Service.java.vm: src/main/java/{packagePath}/service/{className}Service.java 17 | ServiceImpl.java.vm: src/main/java/{packagePath}/service/impl/{className}ServiceImpl.java 18 | 19 | # doc generator 20 | # ... -------------------------------------------------------------------------------- /src/main/resources/mybatis-default/Dao.java.vm: -------------------------------------------------------------------------------- 1 | #set ( $sys = ${system} ) 2 | #set ( $dp = ${dynamicPath} ) 3 | #set ( $tpl = ${template} ) 4 | #set ( $package = ${sys.config.get("generator.package")} ) 5 | #set ( $className = ${dp.className} ) 6 | package ${package}.database.dao; 7 | 8 | import ${package}.database.model.${className}; 9 | import ${package}.database.query.${className}Query; 10 | 11 | import org.apache.ibatis.annotations.Mapper; 12 | import java.util.List; 13 | 14 | @Mapper 15 | public interface ${className}Dao { 16 | List<${className}> select(${className}Query query); 17 | Integer selectCount(${className}Query query); 18 | ${className} selectById(Integer id); 19 | Integer insert(${className} bean); 20 | Integer update(${className} bean); 21 | Integer delete(Integer id); 22 | } 23 | -------------------------------------------------------------------------------- /src/main/resources/mybatis-default/Mapper.xml.vm: -------------------------------------------------------------------------------- 1 | #set ( $sys = ${system} ) 2 | #set ( $dp = ${dynamicPath} ) 3 | #set ( $tpl = ${template} ) 4 | #set ( $package = ${sys.config.get("generator.package")} ) 5 | #set ( $className = ${dp.className} ) 6 | 7 | 8 | 9 | 10 | 11 | #set ( $sort = "" ) 12 | #set ( $order = "" ) 13 | 14 | 15 | `${tableName}` 16 | #foreach($column in $table.columns)`$column.columnName`,#end 17 | 18 | 19 | 20 | #foreach($column in $table.columns) 21 | and `$column.columnName` = #{$column.attributeName} 22 | #end 23 | #foreach($column in $table.columns) 24 | #if($column.attributeType=='String') 25 | and `$column.columnName` like 26 | CONCAT('%', #{${column.attributeName}Like}, '%') 27 | 28 | #end 29 | #end 30 | 31 | 32 | 33 | 34 | 35 | #foreach($column in $table.columns) 36 | `$column.columnName`, 37 | #end 38 | 39 | 40 | 41 | 42 | #foreach($column in $table.columns) 43 | #{$column.attributeName}, 44 | #end 45 | 46 | 47 | 48 | 49 | #foreach($column in $table.columns) 50 | #if($column.columnName != "id") 51 | `$column.columnName` = #{$column.attributeName}, 52 | #end 53 | #end 54 | 55 | 56 | 57 | 58 | 68 | 69 | 73 | 74 | 77 | 78 | 79 | insert into 80 | () 81 | values () 82 | 83 | 84 | 85 | update 86 | 87 | where `id` = #{id} 88 | 89 | 90 | 91 | delete from where `id` = #{value} 92 | 93 | -------------------------------------------------------------------------------- /src/main/resources/mybatis-default/Model.java.vm: -------------------------------------------------------------------------------- 1 | #set ( $sys = ${system} ) 2 | #set ( $dp = ${dynamicPath} ) 3 | #set ( $tpl = ${template} ) 4 | #set ( $package = ${sys.config.get("generator.package")} ) 5 | #set ( $className = ${dp.className} ) 6 | package ${package}.database.model; 7 | 8 | import java.util.Date; 9 | import com.alibaba.fastjson.JSON; 10 | 11 | public class ${className}{ 12 | 13 | #foreach ($column in $table.columns) 14 | private $column.attributeType $column.attributeName; 15 | #end 16 | 17 | #foreach ($column in $table.columns) 18 | public void set${column.uppercaseAttributeName}($column.attributeType $column.attributeName) { 19 | this.$column.attributeName = $column.attributeName; 20 | } 21 | public $column.attributeType get${column.uppercaseAttributeName}() { 22 | return $column.attributeName; 23 | } 24 | #end 25 | 26 | @Override 27 | public String toString() { 28 | return JSON.toJSONString(this); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/resources/mybatis-default/Query.java.vm: -------------------------------------------------------------------------------- 1 | #set ( $sys = ${system} ) 2 | #set ( $dp = ${dynamicPath} ) 3 | #set ( $tpl = ${template} ) 4 | #set ( $package = ${sys.config.get("generator.package")} ) 5 | #set ( $className = ${dp.className} ) 6 | package ${package}.database.query; 7 | 8 | import java.util.Date; 9 | import com.alibaba.fastjson.JSON; 10 | 11 | public class ${className}Query { 12 | 13 | private Integer limit = 1; 14 | private Integer page = 0; 15 | private Integer offset = 0; 16 | private String sort; 17 | private String order; 18 | 19 | #foreach ($column in $table.columns) 20 | private $column.attributeType $column.attributeName; 21 | #if($column.attributeType=='String') 22 | private $column.attributeType ${column.attributeName}Like; 23 | #end 24 | #end 25 | 26 | #foreach ($column in $table.columns) 27 | public void set${column.uppercaseAttributeName}($column.attributeType $column.attributeName) { 28 | this.$column.attributeName = $column.attributeName; 29 | } 30 | public $column.attributeType get${column.uppercaseAttributeName}() { 31 | return $column.attributeName; 32 | } 33 | #if($column.attributeType=='String') 34 | public void set${column.uppercaseAttributeName}Like($column.attributeType ${column.attributeName}Like) { 35 | this.${column.attributeName}Like = ${column.attributeName}Like; 36 | } 37 | public $column.attributeType get${column.uppercaseAttributeName}Like() { 38 | return ${column.attributeName}Like; 39 | } 40 | #end 41 | #end 42 | 43 | public Integer getLimit() { 44 | return limit; 45 | } 46 | 47 | public void setLimit(Integer limit) { 48 | this.limit = limit > 0 ? limit : 1; 49 | this.offset = this.page * this.limit; 50 | } 51 | 52 | public Integer getPage() { 53 | return page; 54 | } 55 | 56 | public void setPage(Integer page) { 57 | if (page == null) { 58 | page = 0; 59 | } 60 | this.page = page >= 0 ? page : 0; 61 | this.offset = this.page * this.limit; 62 | } 63 | 64 | public Integer getOffset() { 65 | return offset; 66 | } 67 | 68 | public void setOffset(Integer offset) { 69 | this.offset = offset; 70 | } 71 | 72 | public String getSort() { 73 | return sort; 74 | } 75 | 76 | public void setSort(String sort) { 77 | this.sort = sort; 78 | } 79 | 80 | public String getOrder() { 81 | return order; 82 | } 83 | 84 | public void setOrder(String order) { 85 | if (order == null) { 86 | this.order = null; 87 | return; 88 | } 89 | String t = order.toLowerCase(); 90 | if (!"asc".equals(t) && !"desc".equals(t)) { 91 | return; 92 | } 93 | this.order = order; 94 | } 95 | 96 | @Override 97 | public String toString() { 98 | return JSON.toJSONString(this); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/main/resources/mybatis-plus/Entity.java.vm: -------------------------------------------------------------------------------- 1 | #set ( $sys = ${system} ) 2 | #set ( $dp = ${dynamicPath} ) 3 | #set ( $tpl = ${template} ) 4 | #set ( $package = ${sys.config.get("generator.package")} ) 5 | #set ( $className = ${dp.className} ) 6 | package ${package}.entity; 7 | 8 | import java.util.Date; 9 | import com.alibaba.fastjson.JSON; 10 | import com.baomidou.mybatisplus.annotation.IdType; 11 | import com.baomidou.mybatisplus.annotation.TableId; 12 | import com.baomidou.mybatisplus.annotation.TableLogic; 13 | 14 | public class ${className}{ 15 | 16 | #foreach (${column} in ${table.columns}) 17 | #if (${column.attributeName} == "id") 18 | @TableId(type = IdType.AUTO) 19 | #elseif(${column.attributeName} == "delFlag") 20 | @TableLogic 21 | #end 22 | private ${column.attributeType} ${column.attributeName}; 23 | #end 24 | 25 | #foreach (${column} in ${table.columns}) 26 | public void set${column.uppercaseAttributeName}(${column.attributeType} ${column.attributeName}) { 27 | this.${column.attributeName} = ${column.attributeName}; 28 | } 29 | public ${column.attributeType} get${column.uppercaseAttributeName}() { 30 | return ${column.attributeName}; 31 | } 32 | #end 33 | 34 | @Override 35 | public String toString() { 36 | return JSON.toJSONString(this); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/resources/mybatis-plus/Mapper.java.vm: -------------------------------------------------------------------------------- 1 | #set ( $sys = ${system} ) 2 | #set ( $dp = ${dynamicPath} ) 3 | #set ( $tpl = ${template} ) 4 | #set ( $package = ${sys.config.get("generator.package")} ) 5 | #set ( $className = ${dp.className} ) 6 | package ${package}.mapper; 7 | 8 | import ${package}.entity.${className}; 9 | 10 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; 11 | import org.apache.ibatis.annotations.Mapper; 12 | 13 | @Mapper 14 | public interface ${className}Mapper extends BaseMapper<${className}> { 15 | 16 | } -------------------------------------------------------------------------------- /src/main/resources/mybatis-plus/Service.java.vm: -------------------------------------------------------------------------------- 1 | #set ( $sys = ${system} ) 2 | #set ( $dp = ${dynamicPath} ) 3 | #set ( $tpl = ${template} ) 4 | #set ( $package = ${sys.config.get("generator.package")} ) 5 | #set ( $className = ${dp.className} ) 6 | package ${package}.service; 7 | 8 | import ${package}.entity.${className}; 9 | import com.baomidou.mybatisplus.extension.service.IService; 10 | 11 | public interface ${className}Service extends IService<${className}> { 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/resources/mybatis-plus/ServiceImpl.java.vm: -------------------------------------------------------------------------------- 1 | #set ( $sys = ${system} ) 2 | #set ( $dp = ${dynamicPath} ) 3 | #set ( $tpl = ${template} ) 4 | #set ( $package = ${sys.config.get("generator.package")} ) 5 | #set ( $className = ${dp.className} ) 6 | package ${package}.service.impl; 7 | 8 | import ${package}.entity.${className}; 9 | import ${package}.mapper.${className}Mapper; 10 | import ${package}.service.${className}Service; 11 | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; 12 | import org.springframework.stereotype.Service; 13 | 14 | @Service 15 | public class ${className}ServiceImpl extends ServiceImpl<${className}Mapper, ${className}> implements ${className}Service { 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/resources/mybatis2/Dao.java.vm: -------------------------------------------------------------------------------- 1 | #set ( $sys = ${system} ) 2 | #set ( $dp = ${dynamicPath} ) 3 | #set ( $tpl = ${template} ) 4 | #set ( $suffix = "DO" ) 5 | #if(${table.tableType}=="VIEW") 6 | #set ( $suffix = "VO" ) 7 | #end 8 | #set ( $package = ${sys.config.get("generator.package")} ) 9 | #set ( $className = ${dp.className} ) 10 | package ${package}.database.dao; 11 | 12 | import ${package}.database.model.${className}; 13 | 14 | import org.apache.ibatis.annotations.Mapper; 15 | import org.laziji.commons.mybatis.dao.${suffix}Dao; 16 | 17 | @Mapper 18 | public interface ${className}Dao extends ${suffix}Dao<${className}> { 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/resources/mybatis2/Model.java.vm: -------------------------------------------------------------------------------- 1 | #set ( $sys = ${system} ) 2 | #set ( $dp = ${dynamicPath} ) 3 | #set ( $tpl = ${template} ) 4 | #set ( $suffix = "DO" ) 5 | #if(${table.tableType}=="VIEW") 6 | #set ( $suffix = "VO" ) 7 | #end 8 | #set ( $package = ${sys.config.get("generator.package")} ) 9 | #set ( $className = ${dp.className} ) 10 | package ${package}.database.model; 11 | 12 | import org.laziji.commons.mybatis.model.Base${suffix}; 13 | import org.laziji.commons.mybatis.dao.annotations.Table; 14 | 15 | import java.util.Date; 16 | 17 | #if (${className}!=${table.className}) 18 | @Table(name="${table.tableName}") 19 | #end 20 | public class ${className} extends Base${suffix}{ 21 | 22 | #foreach (${column} in ${table.columns}) 23 | #if (${suffix}=="VO"||${column.attributeName}!="id"&&${column.attributeName}!="gmtCreate"&&${column.attributeName}!="gmtModified") 24 | private ${column.attributeType} ${column.attributeName}; 25 | #end 26 | #end 27 | 28 | #foreach (${column} in ${table.columns}) 29 | #if (${suffix}=="VO"||${column.attributeName}!="id"&&${column.attributeName}!="gmtCreate"&&${column.attributeName}!="gmtModified") 30 | public void set${column.uppercaseAttributeName}(${column.attributeType} ${column.attributeName}) { 31 | this.${column.attributeName} = ${column.attributeName}; 32 | } 33 | public ${column.attributeType} get${column.uppercaseAttributeName}() { 34 | return ${column.attributeName}; 35 | } 36 | #end 37 | #end 38 | } 39 | -------------------------------------------------------------------------------- /src/main/resources/mybatis2/Query.java.vm: -------------------------------------------------------------------------------- 1 | #set ( $sys = ${system} ) 2 | #set ( $dp = ${dynamicPath} ) 3 | #set ( $tpl = ${template} ) 4 | #set ( $suffix = "DO" ) 5 | #if(${table.tableType}=="VIEW") 6 | #set ( $suffix = "VO" ) 7 | #end 8 | #set ( $package = ${sys.config.get("generator.package")} ) 9 | #set ( $className = ${dp.className} ) 10 | package ${package}.database.query; 11 | 12 | import ${package}.database.model.${className}; 13 | 14 | import java.util.Date; 15 | import org.laziji.commons.mybatis.query.BaseQuery; 16 | 17 | 18 | public class ${className}Query extends BaseQuery<${className}> { 19 | 20 | #foreach (${column} in ${table.columns}) 21 | private ${column.attributeType} ${column.attributeName}; 22 | #if(${column.attributeType}=='String') 23 | private ${column.attributeType} ${column.attributeName}Like; 24 | #end 25 | #end 26 | 27 | #foreach (${column} in $table.columns) 28 | public void set${column.uppercaseAttributeName}(${column.attributeType} ${column.attributeName}) { 29 | this.${column.attributeName} = ${column.attributeName}; 30 | } 31 | public ${column.attributeType} get${column.uppercaseAttributeName}() { 32 | return ${column.attributeName}; 33 | } 34 | #if(${column.attributeType}=='String') 35 | public void set${column.uppercaseAttributeName}Like(${column.attributeType} ${column.attributeName}Like) { 36 | this.${column.attributeName}Like = ${column.attributeName}Like; 37 | } 38 | public ${column.attributeType} get${column.uppercaseAttributeName}Like() { 39 | return ${column.attributeName}Like; 40 | } 41 | #end 42 | #end 43 | } 44 | -------------------------------------------------------------------------------- /src/main/resources/mybatis2/Service.java.vm: -------------------------------------------------------------------------------- 1 | #set ( $sys = ${system} ) 2 | #set ( $dp = ${dynamicPath} ) 3 | #set ( $tpl = ${template} ) 4 | #set ( $suffix = "DO" ) 5 | #if(${table.tableType}=="VIEW") 6 | #set ( $suffix = "VO" ) 7 | #end 8 | #set ( $package = ${sys.config.get("generator.package")} ) 9 | #set ( $className = ${dp.className} ) 10 | package ${package}.database.service; 11 | 12 | import org.springframework.stereotype.Service; 13 | import org.laziji.commons.mybatis.service.Base${suffix}Service; 14 | 15 | import ${package}.database.model.${className}; 16 | 17 | @Service 18 | public class ${className}Service extends Base${suffix}Service<${className}> { 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/pg/laziji/generator/ExampleTest.java: -------------------------------------------------------------------------------- 1 | package pg.laziji.generator; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.ActiveProfiles; 7 | import org.springframework.test.context.junit4.SpringRunner; 8 | import pg.laziji.generator.constant.KeyConsts; 9 | import pg.laziji.generator.model.TableItem; 10 | import pg.laziji.generator.service.impl.gen.CodeGeneratorServiceImpl; 11 | 12 | import javax.annotation.Resource; 13 | 14 | @ActiveProfiles("example") 15 | @RunWith(SpringRunner.class) 16 | @SpringBootTest 17 | public class ExampleTest { 18 | 19 | @Resource 20 | private CodeGeneratorServiceImpl codeGeneratorService; 21 | 22 | @Test 23 | public void test() { 24 | codeGeneratorService.generate(new TableItem[]{ 25 | TableItem.newBuilder() 26 | .tableName("table1") 27 | .dynamicPathVariable(KeyConsts.CLASS_NAME, "TableA") 28 | .build(), 29 | new TableItem("table2") 30 | }, "./entity.zip"); 31 | } 32 | } 33 | --------------------------------------------------------------------------------