Merge branch 'mybatis-flex:main' into main

This commit is contained in:
Jiabin Zhuang 2023-05-26 11:03:27 +08:00 committed by GitHub
commit 9ab8eb08ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
42 changed files with 4120 additions and 1024 deletions

View File

@ -6,6 +6,7 @@
在使用前先添加 `mybatis-flex-codegen` 的 Maven 依赖:
```xml
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-codegen</artifactId>
@ -16,6 +17,7 @@
同时需要添加数据源的 Maven 依赖和 jdbc 驱动依赖:
```xml
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
@ -35,40 +37,15 @@
public class Codegen {
public static void main(String[] args) {
//配置数据源
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/your-database?characterEncoding=utf-8");
dataSource.setUsername("root");
dataSource.setPassword("******");
//创建配置内容
GlobalConfig globalConfig = new GlobalConfig();
//设置只生成哪些表
globalConfig.addGenerateTable("account", "account_session");
//设置 entity 的包名
globalConfig.setEntityPackage("com.test.entity");
//设置表前缀
//globalConfig.setTablePrefix("tb_");
//设置 entity 是否使用 Lombok
//globalConfig.setEntityWithLombok(true);
//是否生成 mapper 类,默认为 false
//globalConfig.setMapperGenerateEnable(true);
//设置 mapper 类的包名
globalConfig.setMapperPackage("com.test.mapper");
//可以单独配置某个列
ColumnConfig columnConfig = new ColumnConfig();
columnConfig.setColumnName("tenant_id");
columnConfig.setLarge(true);
columnConfig.setVersion(true);
globalConfig.addColumnConfig("account", columnConfig);
//创建配置内容,两种风格都可以。
GlobalConfig globalConfig = createGlobalConfigUseStyle1();
//GlobalConfig globalConfig = createGlobalConfigUseStyle2();
//通过 datasource 和 globalConfig 创建代码生成器
Generator generator = new Generator(dataSource, globalConfig);
@ -76,6 +53,65 @@ public class Codegen {
//生成代码
generator.generate();
}
public static GlobalConfig createGlobalConfigUseStyle1() {
//创建配置内容
GlobalConfig globalConfig = new GlobalConfig();
//设置根包
globalConfig.setBasePackage("com.test");
//设置表前缀和只生成哪些表
globalConfig.setTablePrefix("tb_");
globalConfig.setGenerateTable("account", "account_session");
//设置生成 entity 并启用 Lombok
globalConfig.setEntityGenerateEnable(true);
globalConfig.setWithLombok(true);
//设置生成 mapper
globalConfig.setMapperGenerateEnable(true);
//可以单独配置某个列
ColumnConfig columnConfig = new ColumnConfig();
columnConfig.setColumnName("tenant_id");
columnConfig.setLarge(true);
columnConfig.setVersion(true);
globalConfig.setColumnConfig("account", columnConfig);
return globalConfig;
}
public static GlobalConfig createGlobalConfigUseStyle2() {
//创建配置内容
GlobalConfig globalConfig = new GlobalConfig();
//设置根包
globalConfig.getPackageConfig()
.setBasePackage("com.test");
//设置表前缀和只生成哪些表
globalConfig.getStrategyConfig()
.setTablePrefix("tb_")
.setGenerateTable("account", "account_session");
//设置生成 entity 并启用 Lombok
globalConfig.enableEntity()
.setWithLombok(true);
//设置生成 mapper
globalConfig.enableMapper();
//可以单独配置某个列
ColumnConfig columnConfig = new ColumnConfig();
columnConfig.setColumnName("tenant_id");
columnConfig.setLarge(true);
columnConfig.setVersion(true);
globalConfig.getStrategyConfig()
.setColumnConfig("account", columnConfig);
return globalConfig;
}
}
```
@ -84,158 +120,271 @@ public class Codegen {
关闭 APT 的 Mapper 类文件生成,请参考:[APT 设置章节](../others/apt.md)
## 全局配置 GlobalConfig
## 使用介绍
GlobalConfig 支持更多的配置如下:
在 Mybatis-Flex 的代码生成器中,支持如下 8 种类型的的产物生成:
- Entity 实体类
- Mapper 映射类
- TableDef 表定义辅助类
- Service 服务类
- ServiceImpl 服务实现类
- Controller 控制类
- MapperXml 文件
- package-info.java 文件
启用或关闭某种类型产物的生成,代码如下:
```java
public class GlobalConfig {
//代码生成目录
private String sourceDir;
//根包名
private String basePackage = "com.mybatisflex";
//entity 的包名
private String entityPackage;
//entity 类的前缀
private String entityClassPrefix;
//entity 类的后缀
private String entityClassSuffix;
//entity 类的父类,可以自定义一些 BaseEntity 类
private Class<?> entitySupperClass;
//entity 默认实现的接口
private Class<?>[] entityInterfaces = {Serializable.class};
//entity 是否使用 Lombok
private boolean entityWithLombok = false;
private boolean tableDefGenerateEnable = false;
//tableDef 的包名
private String tableDefPackage;
//tableDef 类的前缀
private String tableDefClassPrefix;
//tableDef 类的后缀
private String tableDefClassSuffix = "Def";
//是否生成 mapper 类
private boolean mapperGenerateEnable = false;
//是否覆盖已经存在的 mapper
private boolean mapperOverwriteEnable = false;
//mapper 类的前缀
private String mapperClassPrefix;
//mapper 类的后缀
private String mapperClassSuffix = "Mapper";
//mapper 的包名
private String mapperPackage;
//自定义 mapper 的父类
private Class<?> mapperSupperClass = BaseMapper.class;
//是否生成 service 类
private boolean serviceGenerateEnable = false;
//是否覆盖已经存在的 service
private boolean serviceOverwriteEnable = false;
//service 类的前缀
private String serviceClassPrefix;
//service 类的后缀
private String serviceClassSuffix = "Service";
//service 的包名
private String servicePackage;
//自定义 service 的父类
private Class<?> serviceSupperClass = IService.class;
//是否生成 serviceImpl 类
private boolean serviceImplGenerateEnable = false;
//是否覆盖已经存在的 serviceImpl
private boolean serviceImplOverwriteEnable = false;
//serviceImpl 类的前缀
private String serviceImplClassPrefix;
//serviceImpl 类的后缀
private String serviceImplClassSuffix = "ServiceImpl";
//serviceImpl 的包名
private String serviceImplPackage;
//自定义 serviceImpl 的父类
private Class<?> serviceImplSupperClass = ServiceImpl.class;
//是否生成 controller 类
private boolean controllerGenerateEnable = false;
//是否覆盖已经存在的 controller
private boolean controllerOverwriteEnable = false;
//controller 类的前缀
private String controllerClassPrefix;
//controller 类的后缀
private String controllerClassSuffix = "Controller";
//controller 的包名
private String controllerPackage;
//自定义 controller 的父类
private Class<?> controllerSupperClass;
//rest 风格的 Controller
private boolean restStyleController = true;
//数据库表前缀,多个前缀用英文逗号(, 隔开
private String tablePrefix;
//逻辑删除的默认字段名称
private String logicDeleteColumn;
//乐观锁的字段名称
private String versionColumn;
//是否生成视图映射
private boolean generateForView = false;
//单独为某张表添加独立的配置
private Map<String, TableConfig> tableConfigMap;
//设置某个列的全局配置
private Map<String, ColumnConfig> defaultColumnConfigMap;
//生成哪些表,白名单
private Set<String> generateTables;
//不生成哪些表,黑名单
private Set<String> unGenerateTables;
//使用哪个模板引擎来生成代码
protected ITemplate templateEngine;
//其他自定义配置
private Map<String, Object> others;
}
// 开启 Entity 的生成
globalConfig.enableEntity();
// 关闭 Entity 的生成
globalConfig.disableEntity();
```
## 表配置 TableConfig
所有方法均支持链式调用配置,代码如下:
```java
// 设置生成 Entity 并启用 Lombok、设置父类
globalConfig.enableEntity()
.setWithLombok(true)
.setSupperClass(BaseEntity.class);
```
## 全局配置 `GlobalConfig`
> 可以像先前一样直接使用 `setXxx()` 进行配置,也可以使用 `getXxxConfig().setXxx()` 进行分类配置。
| 获取配置 | 描述 |
|------------------------|------------------|
| getJavadocConfig() | 注释配置 |
| getPackageConfig() | 包配置 |
| getStrategyConfig() | 策略配置 |
| getTemplateConfig() | 模板配置 |
| getEntityConfig() | Entity 生成配置 |
| getMapperConfig() | Mapper 生成配置 |
| getServiceConfig() | Service 生成配置 |
| getServiceImplConfig() | ServiceImpl 生成配置 |
| getControllerConfig() | Controller 生成配置 |
| getTableDefConfig() | TableDef 生成配置 |
| getMapperXmlConfig() | MapperXml 生成配置 |
```java
globalConfig.getPackageConfig()
.setSourceDir("D://files/java")
.setBasePackage("com.your.domain");
```
| 启用生成 | 描述 |
|---------------------|-------------------|
| enableEntity() | 启用 Entity 生成 |
| enableMapper() | 启用 Mapper 生成 |
| enableService() | 启用 Service 生成 |
| enableServiceImpl() | 启用 ServiceImpl 生成 |
| enableController() | 启用 Controller 生成 |
| enableTableDef() | 启用 TableDef 生成 |
| enableMapperXml() | 启用 MapperXml 生成 |
启用生成之后可以继续链式进行配置,例如:
```java
// 设置生成 Entity 并启用 Lombok、设置父类
globalConfig.enableEntity()
.setWithLombok(true)
.setSupperClass(BaseEntity.class);
```
## 注释配置 `JavadocConfig`
| 配置 | 描述 | 默认值 |
|---------------------------------|-----------------|---------------------------------|
| setAuthor(String) | 作者 | System.getProperty("user.name") |
| setSince(String) | 自 | 日期yyyy-MM-dd |
| setTableCommentFormat(Function) | 表名格式化 | 原表名 |
| setEntityPackage(String) | Entity 包注释 | "实体类层Entity软件包。" |
| setMapperPackage(String) | Mapper 包注释 | "映射层Mapper软件包。" |
| setServicePackage(String) | Service 包注释 | "服务层Service软件包。" |
| setServiceImplPackage(String) | ServiceImpl 包注释 | "服务层实现ServiceImpl软件包。" |
| setControllerPackage(String) | Controller 包注释 | "控制层Controller软件包。" |
| setTableDefPackage(String) | TableDef 包注释 | "表定义层TableDef软件包。" |
```java
globalConfig.getJavadocConfig()
.setAuthor("Your Name")
.setSince("1.0.1");
```
## 包配置 `PackageConfig`
| 配置 | 描述 | 默认值 |
|-------------------------------|----------------|---------------------------------------------------|
| setSourceDir(String) | 文件输出目录 | System.getProperty("user.dir") + "/src/main/java" |
| setBasePackage(String) | 根包名 | "com.mybatisflex" |
| setEntityPackage(String) | Entity 包名 | getBasePackage() + ".entity" | |
| setMapperPackage(String) | Mapper 包名 | getBasePackage() + ".mapper" | |
| setServicePackage(String) | Service 包名 | getBasePackage() + ".service" | |
| setServiceImplPackage(String) | ServiceImpl 包名 | getBasePackage() + ".service.impl" | |
| setControllerPackage(String) | Controller 包名 | getBasePackage() + ".controller" | |
| setTableDefPackage(String) | TableDef 包名 | getEntityPackage() + ".tables" | |
| setMapperXmlPath(String) | MapperXml 路径 | getSourceDir() + "/resources/mapper" | |
```java
globalConfig.getPackageConfig()
.setSourceDir("D://files/java")
.setBasePackage("com.your.domain");
```
## 策略配置 `StrategyConfig`
| 配置 | 描述 | 默认值 |
|--------------------------------|------------------------|-------|
| setTablePrefix(String) | 数据库表前缀,多个前缀用英文逗号(, 隔开 | null |
| setLogicDeleteColumn(String) | 逻辑删除的默认字段名称 | null |
| setVersionColumn(String) | 乐观锁的字段名称 | null |
| setGenerateForView(boolean) | 是否生成视图映射 | false |
| setTableConfig(TableConfig) | 单独为某张表添加独立的配置 | null |
| setColumnConfig(ColumnConfig) | 设置某个列的全局配置 | null |
| setGenerateTables(String...) | 生成哪些表,白名单 | null |
| setUnGenerateTables(String...) | 不生成哪些表,黑名单 | null |
```java
globalConfig.getStrategyConfig()
.setTablePrefix("sys_")
.setGenerateTables("sys_user","sys_dept");
```
## 模板配置 `TemplateConfig`
| 配置 | 描述 | 默认值 |
|------------------------|------------------|------------------------------------|
| setTemplate(ITemplate) | | |
| setEntity(String) | Entity 模板路径 | "/templates/enjoy/entity.tpl" |
| setMapper(String) | Mapper 模板路径 | "/templates/enjoy/mapper.tpl" |
| setService(String) | Service 模板路径 | "/templates/enjoy/service.tpl" |
| setServiceImpl(String) | ServiceImpl 模板路径 | "/templates/enjoy/serviceImpl.tpl" |
| setController(String) | Controller 模板路径 | "/templates/enjoy/controller.tpl" |
| setTableDef(String) | TableDef 模板路径 | "/templates/enjoy/tableDef.tpl" |
| setMapperXml(String) | MapperXml 模板路径 | "/templates/enjoy/mapperXml.tpl" |
```java
globalConfig.getTemplateConfig()
.setTemplate(new FreeMarkerTemplate())
.setEntity("D:\your-template-file\my-entity.tpl");
```
## Entity 生成配置 `EntityConfig`
| 配置 | 描述 | 默认值 |
|-----------------------------|----------------------------------|--------------------|
| setClassPrefix(String) | Entity 类的前缀 | "" |
| setClassSuffix(String) | Entity 类的后缀 | "" |
| setSupperClass(Class) | Entity 类的父类,可以自定义一些 BaseEntity 类 | null |
| setOverwriteEnable(boolean) | 是否覆盖之前生成的文件 | false |
| setImplInterfaces(Class[]) | Entity 默认实现的接口 | Serializable.class |
| setWithLombok(boolean) | Entity 是否使用 Lombok 注解 | false |
```java
globalConfig.getEntityConfig()
.setWithLombok(true)
.setClassPrefix("My")
.setClassSuffix("Entity")
.setSupperClass(BaseEntity.class);
```
## Mapper 生成配置 `MapperConfig`
| 配置 | 描述 | 默认值 |
|-----------------------------|-------------|------------------|
| setClassPrefix(String) | Mapper 类的前缀 | "" |
| setClassSuffix(String) | Mapper 类的后缀 | "Mapper" |
| setSupperClass(Class) | Mapper 类的父类 | BaseMapper.class |
| setOverwriteEnable(boolean) | 是否覆盖之前生成的文件 | false |
```java
globalConfig.getMapperConfig()
.setClassPrefix("My")
.setClassSuffix("Mapper")
.setSuperClass(BaseMapper.class);
```
## Service 生成配置 `ServiceConfig`
| 配置 | 描述 | 默认值 |
|-----------------------------|--------------|----------------|
| setClassPrefix(String) | Service 类的前缀 | "" |
| setClassSuffix(String) | Service 类的后缀 | "Service" |
| setSupperClass(Class) | Service 类的父类 | IService.class |
| setOverwriteEnable(boolean) | 是否覆盖之前生成的文件 | false |
```java
globalConfig.getServiceConfig()
.setClassPrefix("My")
.setClassSuffix("Service")
.setSuperClass(IService.class);
```
## ServiceImpl 生成配置 `ServiceImplConfig`
| 配置 | 描述 | 默认值 |
|-----------------------------|------------------|-------------------|
| setClassPrefix(String) | ServiceImpl 类的前缀 | "" |
| setClassSuffix(String) | ServiceImpl 类的后缀 | "ServiceImpl" |
| setSupperClass(Class) | ServiceImpl 类的父类 | ServiceImpl.class |
| setOverwriteEnable(boolean) | 是否覆盖之前生成的文件 | false |
```java
globalConfig.getServiceImplConfig()
.setClassPrefix("My")
.setClassSuffix("ServiceImpl")
.setSuperClass(ServiceImpl.class);
```
## Controller 生成配置 `ControllerConfig`
| 配置 | 描述 | 默认值 |
|-----------------------------|---------------------|--------------|
| setClassPrefix(String) | Controller 类的前缀 | "" |
| setClassSuffix(String) | Controller 类的后缀 | "Controller" |
| setSupperClass(Class) | Controller 类的父类 | null |
| setOverwriteEnable(boolean) | 是否覆盖之前生成的文件 | false |
| setRestStyle(boolean) | REST 风格的 Controller | true |
```java
globalConfig.getControllerConfig()
.setClassPrefix("My")
.setClassSuffix("Controller")
.setSuperClass(BaseController.class);
```
## TableDef 生成配置 `TableDefConfig`
| 配置 | 描述 | 默认值 |
|-----------------------------|---------------|-------|
| setClassPrefix(String) | TableDef 类的前缀 | "" |
| setClassSuffix(String) | TableDef 类的后缀 | "Def" |
| setOverwriteEnable(boolean) | 是否覆盖之前生成的文件 | false |
```java
globalConfig.getTableDefConfig()
.setClassPrefix("My")
.setClassSuffix("Def");
```
## MapperXml 生成配置 `MapperXmlConfig`
| 配置 | 描述 | 默认值 |
|-----------------------------|-----------------|----------|
| setFilePrefix(String) | MapperXml 文件的前缀 | "" |
| setFileSuffix(String) | MapperXml 文件的后缀 | "Mapper" |
| setOverwriteEnable(boolean) | 是否覆盖之前生成的文件 | false |
```java
globalConfig.getMapperXmlConfig()
.setFilePrefix("My")
.setFileSuffix("Mapper");
```
## 表配置 `TableConfig`
TableConfig 支持的配置如下:
@ -265,7 +414,7 @@ public class TableConfig {
}
```
## 列配置 ColumnConfig
## 列配置 `ColumnConfig`
ColumnConfig 支持的配置如下:
@ -302,11 +451,13 @@ MyBatis-Flex 内置了一个名为:`JdbcTypeMapping` 的 java 类,我们可
```java
JdbcTypeMapping.registerMapping(LocalDateTime.class,Date.class);
```
那么,当我们生成代码的时候,发现 JDBC 驱动的数据类型为 `LocalDateTime`,则 Entity 对应的属性类型为 `Date`
## 自定义代码模板
通过 `GlobalConfig`(全局配置)的 `setTemplateEngine()` 方法,可以配置自己的模板引擎以及模板,以下是内置的 `EnjoyTemplate` 的代码示例:
通过 `GlobalConfig`(全局配置)的 `setTemplateEngine()` 方法,可以配置自己的模板引擎以及模板,以下是内置的 `EnjoyTemplate`
的代码示例:
```java
public class EnjoyTemplate implements ITemplate {
@ -354,16 +505,7 @@ public class EnjoyTemplate implements ITemplate {
## 添加其他产物的生成
在 MyBatis-Flex 的代码生成器中,支持如下 6 种类型的的产物生成
- 1、Entity 实体类
- 2、Mapper 类(默认关闭)
- 3、TableDef 表定义辅助类(默认关闭)
- 4、Service 类(默认关闭)
- 5、ServiceImpl 类(默认关闭)
- 6、Controller 类(默认关闭)
这 6 种产物,都是通过实现 `IGenerator` 来实现的,比如 Entity 实体类的代码如下:
通过实现 `IGenerator` 来实现,比如 Entity 实体类的代码如下:
```java
public class EntityGenerator implements IGenerator {
@ -373,15 +515,30 @@ public class EntityGenerator implements IGenerator {
@Override
public void generate(Table table, GlobalConfig globalConfig) {
String entityPackagePath = globalConfig.getEntityPackage().replace(".", "/");
File entityJavaFile = new File(globalConfig.getSourceDir(), entityPackagePath + "/" +
if (!globalConfig.isEntityGenerateEnable()) {
return;
}
PackageConfig packageConfig = globalConfig.getPackageConfig();
EntityConfig entityConfig = globalConfig.getEntityConfig();
String entityPackagePath = packageConfig.getEntityPackage().replace(".", "/");
File entityJavaFile = new File(packageConfig.getSourceDir(), entityPackagePath + "/" +
table.buildEntityClassName() + ".java");
Map<String, Object> params = new HashMap<>();
params.put("table", table);
params.put("globalConfig", globalConfig);
globalConfig.getTemplateEngine().generate(params, templatePath, entityJavaFile);
if (entityJavaFile.exists() && !entityConfig.isOverwriteEnable()) {
return;
}
Map<String, Object> params = new HashMap<>(4);
params.put("table", table);
params.put("entityConfig", entityConfig);
params.put("packageConfig", packageConfig);
params.put("javadocConfig", globalConfig.getJavadocConfig());
globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, entityJavaFile);
}
}
```
@ -397,6 +554,7 @@ public class HtmlGenerator implements IGenerator {
}
}
```
最后,通过 `GeneratorFactory` 来注册 `HtmlGenerator` 即可:
```java

View File

@ -82,6 +82,13 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -16,6 +16,7 @@
package com.mybatisflex.codegen;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.StrategyConfig;
import com.mybatisflex.codegen.dialect.IDialect;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.codegen.generator.GeneratorFactory;
@ -83,22 +84,23 @@ public class Generator {
private List<Table> buildTables() throws SQLException {
StrategyConfig strategyConfig = globalConfig.getStrategyConfig();
List<Table> tables = new ArrayList<>();
try (ResultSet rs = getTablesResultSet()) {
while (rs.next()) {
String tableName = rs.getString("TABLE_NAME");
if (!globalConfig.isSupportGenerate(tableName)) {
if (!strategyConfig.isSupportGenerate(tableName)) {
continue;
}
Table table = new Table();
table.setGlobalConfig(globalConfig);
table.setTableConfig(globalConfig.getTableConfig(tableName));
table.setTableConfig(strategyConfig.getTableConfig(tableName));
table.setName(tableName);
String remarks = rs.getString("REMARKS");
table.setRemarks(remarks);
table.setComment(remarks);
buildPrimaryKey(table);
@ -113,7 +115,7 @@ public class Generator {
protected ResultSet getTablesResultSet() throws SQLException {
if (globalConfig.isGenerateForView()) {
if (globalConfig.getStrategyConfig().isGenerateForView()) {
return dialect.getTablesResultSet(dbMeta, conn, new String[]{"TABLE", "VIEW"});
} else {
return dialect.getTablesResultSet(dbMeta, conn, new String[]{"TABLE"});

View File

@ -0,0 +1,135 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.codegen.config;
/**
* 生成 Controller 的配置
*
* @author 王帅
* @since 2023-05-15
*/
@SuppressWarnings("unused")
public class ControllerConfig {
/**
* Controller 类的前缀
*/
private String classPrefix = "";
/**
* Controller 类的后缀
*/
private String classSuffix = "Controller";
/**
* 自定义 Controller 的父类
*/
private Class<?> supperClass;
/**
* 是否覆盖之前生成的文件
*/
private boolean overwriteEnable;
/**
* 生成 REST 风格的 Controller
*/
private boolean restStyle = true;
public String buildSuperClassImport() {
return supperClass.getName();
}
public String buildSuperClassName() {
return supperClass.getSimpleName();
}
/**
* 获取类前缀
*/
public String getClassPrefix() {
return classPrefix;
}
/**
* 设置类前缀
*/
public ControllerConfig setClassPrefix(String classPrefix) {
this.classPrefix = classPrefix;
return this;
}
/**
* 获取类后缀
*/
public String getClassSuffix() {
return classSuffix;
}
/**
* 设置类后缀
*/
public ControllerConfig setClassSuffix(String classSuffix) {
this.classSuffix = classSuffix;
return this;
}
/**
* 获取父类
*/
public Class<?> getSupperClass() {
return supperClass;
}
/**
* 设置父类
*/
public ControllerConfig setSupperClass(Class<?> supperClass) {
this.supperClass = supperClass;
return this;
}
/**
* 是否覆盖原有文件
*/
public boolean isOverwriteEnable() {
return overwriteEnable;
}
/**
* 设置是否覆盖原有文件
*/
public ControllerConfig setOverwriteEnable(boolean overwriteEnable) {
this.overwriteEnable = overwriteEnable;
return this;
}
/**
* 是否 REST 风格
*/
public boolean isRestStyle() {
return restStyle;
}
/**
* 设置 REST 风格
*/
public ControllerConfig setRestStyle(boolean restStyle) {
this.restStyle = restStyle;
return this;
}
}

View File

@ -0,0 +1,149 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.codegen.config;
import java.io.Serializable;
/**
* 生成 Entity 的配置
*
* @author 王帅
* @since 2023-05-15
*/
@SuppressWarnings("unused")
public class EntityConfig {
/**
* Entity 类的前缀
*/
private String classPrefix = "";
/**
* Entity 类的后缀
*/
private String classSuffix = "";
/**
* Entity 类的父类可以自定义一些 BaseEntity
*/
private Class<?> supperClass;
/**
* 是否覆盖之前生成的文件
*/
private boolean overwriteEnable;
/**
* Entity 默认实现的接口
*/
private Class<?>[] implInterfaces = {Serializable.class};
/**
* Entity 是否使用 Lombok 注解
*/
private boolean withLombok;
/**
* 获取类前缀
*/
public String getClassPrefix() {
return classPrefix;
}
/**
* 设置类前缀
*/
public EntityConfig setClassPrefix(String classPrefix) {
this.classPrefix = classPrefix;
return this;
}
/**
* 获取类后缀
*/
public String getClassSuffix() {
return classSuffix;
}
/**
* 设置类后缀
*/
public EntityConfig setClassSuffix(String classSuffix) {
this.classSuffix = classSuffix;
return this;
}
/**
* 获取父类
*/
public Class<?> getSupperClass() {
return supperClass;
}
/**
* 设置父类
*/
public EntityConfig setSupperClass(Class<?> supperClass) {
this.supperClass = supperClass;
return this;
}
/**
* 是否覆盖原有文件
*/
public boolean isOverwriteEnable() {
return overwriteEnable;
}
/**
* 设置是否覆盖原有文件
*/
public EntityConfig setOverwriteEnable(boolean overwriteEnable) {
this.overwriteEnable = overwriteEnable;
return this;
}
/**
* 获取实现接口
*/
public Class<?>[] getImplInterfaces() {
return implInterfaces;
}
/**
* 设置实现接口
*/
public EntityConfig setImplInterfaces(Class<?>... implInterfaces) {
this.implInterfaces = implInterfaces;
return this;
}
/**
* 是否使用 Lombok
*/
public boolean isWithLombok() {
return withLombok;
}
/**
* 设置是否使用 Lombok
*/
public EntityConfig setWithLombok(boolean withLombok) {
this.withLombok = withLombok;
return this;
}
}

View File

@ -0,0 +1,228 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.codegen.config;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
/**
* 注释配置类
*
* @author 王帅
* @since 2023-05-17
*/
@SuppressWarnings("unused")
public class JavadocConfig {
/**
* 作者
*/
private String author = System.getProperty("user.name");
/**
*
*/
private Supplier<String> since = () -> DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDate.now());
/**
* 表名格式化
*/
private UnaryOperator<String> tableCommentFormat = UnaryOperator.identity();
/**
* Entity 包注释
*/
private String entityPackage = "实体类层Entity软件包。";
/**
* Mapper 包注释
*/
private String mapperPackage = "映射层Mapper软件包。";
/**
* Service 包注释
*/
private String servicePackage = "服务层Service软件包。";
/**
* ServiceImpl 包注释
*/
private String serviceImplPackage = "服务层实现ServiceImpl软件包。";
/**
* Controller 包注释
*/
private String controllerPackage = "控制层Controller软件包。";
/**
* TableDef 包注释
*/
private String tableDefPackage = "表定义层TableDef软件包。";
/**
* 获取作者
*/
public String getAuthor() {
return author;
}
/**
* 设置作者
*/
public JavadocConfig setAuthor(String author) {
this.author = author;
return this;
}
/**
* 获取自
*/
public String getSince() {
return since.get();
}
/**
* 设置自
*/
public JavadocConfig setSince(String since) {
this.since = () -> since;
return this;
}
/**
* 设置自
*/
public JavadocConfig setSince(Supplier<String> since) {
this.since = since;
return this;
}
public String formatTableComment(String comment) {
return tableCommentFormat.apply(comment);
}
/**
* 获取表注释格式化
*/
public Function<String, String> getTableCommentFormat() {
return tableCommentFormat;
}
/**
* 设置表注释格式化方案用来生成实体类注释
*/
public JavadocConfig setTableCommentFormat(UnaryOperator<String> tableCommentFormat) {
this.tableCommentFormat = tableCommentFormat;
return this;
}
/**
* 获取实体类层包注释
*/
public String getEntityPackage() {
return entityPackage;
}
/**
* 设置实体类层包注释
*/
public JavadocConfig setEntityPackage(String entityPackage) {
this.entityPackage = entityPackage;
return this;
}
/**
* 获取映射层包注释
*/
public String getMapperPackage() {
return mapperPackage;
}
/**
* 设置映射层包注释
*/
public JavadocConfig setMapperPackage(String mapperPackage) {
this.mapperPackage = mapperPackage;
return this;
}
/**
* 获取服务层包注释
*/
public String getServicePackage() {
return servicePackage;
}
/**
* 设置服务层包注释
*/
public JavadocConfig setServicePackage(String servicePackage) {
this.servicePackage = servicePackage;
return this;
}
/**
* 获取服务层实现包注释
*
* @return {@link String}
*/
public String getServiceImplPackage() {
return serviceImplPackage;
}
/**
* 设置服务层实现包注释
*/
public JavadocConfig setServiceImplPackage(String serviceImplPackage) {
this.serviceImplPackage = serviceImplPackage;
return this;
}
/**
* 获取控制层包注释
*/
public String getControllerPackage() {
return controllerPackage;
}
/**
* 设置控制层包注释
*/
public JavadocConfig setControllerPackage(String controllerPackage) {
this.controllerPackage = controllerPackage;
return this;
}
/**
* 获取表定义层包注释
*/
public String getTableDefPackage() {
return tableDefPackage;
}
/**
* 设置表定义层包注释
*/
public JavadocConfig setTableDefPackage(String tableDefPackage) {
this.tableDefPackage = tableDefPackage;
return this;
}
}

View File

@ -0,0 +1,117 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.codegen.config;
import com.mybatisflex.core.BaseMapper;
/**
* 生成 Mapper 的配置
*
* @author 王帅
* @since 2023-05-15
*/
@SuppressWarnings("unused")
public class MapperConfig {
/**
* Mapper 类的前缀
*/
private String classPrefix = "";
/**
* Mapper 类的后缀
*/
private String classSuffix = "Mapper";
/**
* 自定义 Mapper 的父类
*/
private Class<?> supperClass = BaseMapper.class;
/**
* 是否覆盖之前生成的文件
*/
private boolean overwriteEnable;
public String buildSuperClassImport() {
return supperClass.getName();
}
public String buildSuperClassName() {
return supperClass.getSimpleName();
}
/**
* 获取类前缀
*/
public String getClassPrefix() {
return classPrefix;
}
/**
* 设置类前缀
*/
public MapperConfig setClassPrefix(String classPrefix) {
this.classPrefix = classPrefix;
return this;
}
/**
* 获取类后缀
*/
public String getClassSuffix() {
return classSuffix;
}
/**
* 设置类后缀
*/
public MapperConfig setClassSuffix(String classSuffix) {
this.classSuffix = classSuffix;
return this;
}
/**
* 获取父类
*/
public Class<?> getSupperClass() {
return supperClass;
}
/**
* 设置父类
*/
public MapperConfig setSupperClass(Class<?> supperClass) {
this.supperClass = supperClass;
return this;
}
/**
* 是否覆盖原有文件
*/
public boolean isOverwriteEnable() {
return overwriteEnable;
}
/**
* 设置是否覆盖原有文件
*/
public MapperConfig setOverwriteEnable(boolean overwriteEnable) {
this.overwriteEnable = overwriteEnable;
return this;
}
}

View File

@ -0,0 +1,88 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.codegen.config;
/**
* 生成 MapperXml 的配置
*
* @author 王帅
* @since 2023-05-17
*/
@SuppressWarnings("unused")
public class MapperXmlConfig {
/**
* MapperXml 文件的前缀
*/
private String filePrefix = "";
/**
* MapperXml 文件的后缀
*/
private String fileSuffix = "Mapper";
/**
* 是否覆盖之前生成的文件
*/
private boolean overwriteEnable;
/**
* 获取文件前缀
*/
public String getFilePrefix() {
return filePrefix;
}
/**
* 设置文件前缀
*/
public MapperXmlConfig setFilePrefix(String filePrefix) {
this.filePrefix = filePrefix;
return this;
}
/**
* 获取文件后缀
*/
public String getFileSuffix() {
return fileSuffix;
}
/**
* 设置文件后缀
*/
public MapperXmlConfig setFileSuffix(String fileSuffix) {
this.fileSuffix = fileSuffix;
return this;
}
/**
* 是否覆盖原有文件
*/
public boolean isOverwriteEnable() {
return overwriteEnable;
}
/**
* 设置是否覆盖原有文件
*/
public MapperXmlConfig setOverwriteEnable(boolean overwriteEnable) {
this.overwriteEnable = overwriteEnable;
return this;
}
}

View File

@ -0,0 +1,234 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.codegen.config;
import com.mybatisflex.core.util.StringUtil;
/**
* 生成软件包的配置
*
* @author 王帅
* @since 2023-05-15
*/
@SuppressWarnings("unused")
public class PackageConfig {
/**
* 代码生成目录
*/
private String sourceDir;
/**
* 根包
*/
private String basePackage = "com.mybatisflex";
/**
* Entity 所在包
*/
private String entityPackage;
/**
* Mapper 所在包
*/
private String mapperPackage;
/**
* Service 所在包
*/
private String servicePackage;
/**
* ServiceImpl 所在包
*/
private String serviceImplPackage;
/**
* Controller 所在包
*/
private String controllerPackage;
/**
* TableDef 所在包
*/
private String tableDefPackage;
/**
* MapperXml 文件所在位置
*/
private String mapperXmlPath;
/**
* 获取生成目录
*/
public String getSourceDir() {
if (StringUtil.isBlank(sourceDir)) {
return System.getProperty("user.dir") + "/src/main/java";
}
return sourceDir;
}
/**
* 设置生成目录
*/
public PackageConfig setSourceDir(String sourceDir) {
this.sourceDir = sourceDir;
return this;
}
/**
* 获得根包路径
*/
public String getBasePackage() {
return basePackage;
}
/**
* 设置根包路径
*/
public PackageConfig setBasePackage(String basePackage) {
this.basePackage = basePackage;
return this;
}
/**
* 获取实体类层包路径
*/
public String getEntityPackage() {
if (StringUtil.isBlank(entityPackage)) {
return basePackage.concat(".entity");
}
return entityPackage;
}
/**
* 设置实体类层包路径
*/
public PackageConfig setEntityPackage(String entityPackage) {
this.entityPackage = entityPackage;
return this;
}
/**
* 获取映射层包路径
*/
public String getMapperPackage() {
if (StringUtil.isBlank(mapperPackage)) {
return basePackage.concat(".mapper");
}
return mapperPackage;
}
/**
* 设置映射层包路径
*/
public PackageConfig setMapperPackage(String mapperPackage) {
this.mapperPackage = mapperPackage;
return this;
}
/**
* 获取服务层包路径
*/
public String getServicePackage() {
if (StringUtil.isBlank(servicePackage)) {
return basePackage.concat(".service");
}
return servicePackage;
}
/**
* 设置服务层包路径
*/
public PackageConfig setServicePackage(String servicePackage) {
this.servicePackage = servicePackage;
return this;
}
/**
* 获取服务层实现包路径
*/
public String getServiceImplPackage() {
if (StringUtil.isBlank(serviceImplPackage)) {
return basePackage.concat(".service.impl");
}
return serviceImplPackage;
}
/**
* 设置服务层实现包路径
*/
public PackageConfig setServiceImplPackage(String serviceImplPackage) {
this.serviceImplPackage = serviceImplPackage;
return this;
}
/**
* 获取控制层包路径
*/
public String getControllerPackage() {
if (StringUtil.isBlank(controllerPackage)) {
return basePackage.concat(".controller");
}
return controllerPackage;
}
/**
* 设置控制层包路径
*/
public PackageConfig setControllerPackage(String controllerPackage) {
this.controllerPackage = controllerPackage;
return this;
}
/**
* 获取表定义层包路径
*/
public String getTableDefPackage() {
if (StringUtil.isBlank(tableDefPackage)) {
return getEntityPackage().concat(".tables");
}
return tableDefPackage;
}
/**
* 设置表定义层包路径
*/
public PackageConfig setTableDefPackage(String tableDefPackage) {
this.tableDefPackage = tableDefPackage;
return this;
}
/**
* 获取 Mapper XML 文件路径
*/
public String getMapperXmlPath() {
if (StringUtil.isBlank(mapperXmlPath)) {
return getSourceDir().concat("/resources/mapper");
}
return mapperXmlPath;
}
/**
* 设置 Mapper XML 文件路径
*/
public PackageConfig setMapperXmlPath(String mapperXmlPath) {
this.mapperXmlPath = mapperXmlPath;
return this;
}
}

View File

@ -0,0 +1,118 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.codegen.config;
import com.mybatisflex.core.service.IService;
/**
* 生成 Service 的配置
*
* @author 王帅
* @since 2023-05-15
*/
@SuppressWarnings("unused")
public class ServiceConfig {
/**
* Service 类的前缀
*/
private String classPrefix = "";
/**
* Service 类的后缀
*/
private String classSuffix = "Service";
/**
* 自定义 Service 的父类
*/
private Class<?> supperClass = IService.class;
/**
* 是否覆盖之前生成的文件
*/
private boolean overwriteEnable;
public String buildSuperClassImport() {
return supperClass.getName();
}
public String buildSuperClassName() {
return supperClass.getSimpleName();
}
/**
* 获取类前缀
*/
public String getClassPrefix() {
return classPrefix;
}
/**
* 设置类前缀
*/
public ServiceConfig setClassPrefix(String classPrefix) {
this.classPrefix = classPrefix;
return this;
}
/**
* 获取类后缀
*/
public String getClassSuffix() {
return classSuffix;
}
/**
* 设置类后缀
*/
public ServiceConfig setClassSuffix(String classSuffix) {
this.classSuffix = classSuffix;
return this;
}
/**
* 获取父类
*/
public Class<?> getSupperClass() {
return supperClass;
}
/**
* 设置父类
*/
public ServiceConfig setSupperClass(Class<?> supperClass) {
this.supperClass = supperClass;
return this;
}
/**
* 是否覆盖原有文件
*/
public boolean isOverwriteEnable() {
return overwriteEnable;
}
/**
* 设置是否覆盖原有文件
*/
public ServiceConfig setOverwriteEnable(boolean overwriteEnable) {
this.overwriteEnable = overwriteEnable;
return this;
}
}

View File

@ -0,0 +1,117 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.codegen.config;
import com.mybatisflex.spring.service.impl.ServiceImpl;
/**
* 生成 ServiceImpl 的配置
*
* @author 王帅
* @since 2023-05-15
*/
@SuppressWarnings("unused")
public class ServiceImplConfig {
/**
* ServiceImpl 类的前缀
*/
private String classPrefix = "";
/**
* ServiceImpl 类的后缀
*/
private String classSuffix = "ServiceImpl";
/**
* 自定义 ServiceImpl 的父类
*/
private Class<?> supperClass = ServiceImpl.class;
/**
* 是否覆盖之前生成的文件
*/
private boolean overwriteEnable;
public String buildSuperClassImport() {
return supperClass.getName();
}
public String buildSuperClassName() {
return supperClass.getSimpleName();
}
/**
* 获取类前缀
*/
public String getClassPrefix() {
return classPrefix;
}
/**
* 设置类前缀
*/
public ServiceImplConfig setClassPrefix(String classPrefix) {
this.classPrefix = classPrefix;
return this;
}
/**
* 获取类后缀
*/
public String getClassSuffix() {
return classSuffix;
}
/**
* 设置类后缀
*/
public ServiceImplConfig setClassSuffix(String classSuffix) {
this.classSuffix = classSuffix;
return this;
}
/**
* 获取父类
*/
public Class<?> getSupperClass() {
return supperClass;
}
/**
* 设置父类
*/
public ServiceImplConfig setSupperClass(Class<?> supperClass) {
this.supperClass = supperClass;
return this;
}
/**
* 是否覆盖原有文件
*/
public boolean isOverwriteEnable() {
return overwriteEnable;
}
/**
* 设置是否覆盖原有文件
*/
public ServiceImplConfig setOverwriteEnable(boolean overwriteEnable) {
this.overwriteEnable = overwriteEnable;
return this;
}
}

View File

@ -0,0 +1,316 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.codegen.config;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* 表策略配置
*
* @author 王帅
* @since 2023-05-14
*/
@SuppressWarnings("unused")
public class StrategyConfig {
/**
* 数据库表前缀多个前缀用英文逗号, 隔开
*/
private String tablePrefix;
/**
* 逻辑删除的默认字段名称
*/
private String logicDeleteColumn;
/**
* 乐观锁的字段名称
*/
private String versionColumn;
/**
* 是否生成视图映射
*/
private boolean generateForView;
/**
* 单独为某张表添加独立的配置
*/
private Map<String, TableConfig> tableConfigMap;
/**
* 设置某个列的全局配置
*/
private Map<String, ColumnConfig> columnConfigMap;
/**
* 生成哪些表白名单
*/
private Set<String> generateTables;
/**
* 不生成哪些表黑名单
*/
private Set<String> unGenerateTables;
/**
* 获取表配置
*/
public TableConfig getTableConfig(String tableName) {
return tableConfigMap == null ? null : tableConfigMap.get(tableName);
}
/**
* 设置表配置
*/
public void setTableConfig(TableConfig tableConfig) {
if (tableConfigMap == null) {
tableConfigMap = new HashMap<>();
}
tableConfigMap.put(tableConfig.getTableName(), tableConfig);
}
/**
* 获取列配置
*/
public ColumnConfig getColumnConfig(String tableName, String columnName) {
ColumnConfig columnConfig = null;
TableConfig tableConfig = getTableConfig(tableName);
if (tableConfig != null) {
columnConfig = tableConfig.getColumnConfig(columnName);
}
if (columnConfig == null && columnConfigMap != null) {
columnConfig = columnConfigMap.get(columnName);
}
if (columnConfig == null) {
columnConfig = new ColumnConfig();
}
//全局配置的逻辑删除
if (columnName.equals(logicDeleteColumn) && columnConfig.getLogicDelete() == null) {
columnConfig.setLogicDelete(true);
}
//全部配置的乐观锁版本
if (columnName.equals(versionColumn) && columnConfig.getVersion() == null) {
columnConfig.setVersion(true);
}
return columnConfig;
}
/**
* 设置列配置
*/
public void setColumnConfig(ColumnConfig columnConfig) {
if (columnConfigMap == null) {
columnConfigMap = new HashMap<>();
}
columnConfigMap.put(columnConfig.getColumnName(), columnConfig);
}
/**
* 设置列配置
*/
public void setColumnConfig(String tableName, ColumnConfig columnConfig) {
TableConfig tableConfig = getTableConfig(tableName);
if (tableConfig == null) {
tableConfig = new TableConfig();
tableConfig.setTableName(tableName);
setTableConfig(tableConfig);
}
tableConfig.addColumnConfig(columnConfig);
}
/**
* 设置生成哪些表
*/
public void setGenerateTable(String... tables) {
if (generateTables == null) {
generateTables = new HashSet<>();
}
for (String table : tables) {
if (table != null && table.trim().length() > 0) {
generateTables.add(table.trim());
}
}
}
/**
* 设置不生成哪些表
*/
public void setUnGenerateTable(String... tables) {
if (unGenerateTables == null) {
unGenerateTables = new HashSet<>();
}
for (String table : tables) {
if (table != null && table.trim().length() > 0) {
unGenerateTables.add(table.trim());
}
}
}
public boolean isSupportGenerate(String table) {
if (unGenerateTables != null && unGenerateTables.contains(table)) {
return false;
}
//不配置指定比表名的情况下支持所有表
if (generateTables == null || generateTables.isEmpty()) {
return true;
}
for (String generateTable : generateTables) {
if (generateTable.equals(table)) {
return true;
}
}
return false;
}
/**
* 获取表前缀
*/
public String getTablePrefix() {
return tablePrefix;
}
/**
* 设置表前缀
*/
public StrategyConfig setTablePrefix(String tablePrefix) {
this.tablePrefix = tablePrefix;
return this;
}
/**
* 获取逻辑删除列
*/
public String getLogicDeleteColumn() {
return logicDeleteColumn;
}
/**
* 设置逻辑删除列
*/
public StrategyConfig setLogicDeleteColumn(String logicDeleteColumn) {
this.logicDeleteColumn = logicDeleteColumn;
return this;
}
/**
* 获取版本列
*/
public String getVersionColumn() {
return versionColumn;
}
/**
* 设置版本列
*/
public StrategyConfig setVersionColumn(String versionColumn) {
this.versionColumn = versionColumn;
return this;
}
/**
* 是否生成视图
*/
public boolean isGenerateForView() {
return generateForView;
}
/**
* 设置是否生成视图
*/
public StrategyConfig setGenerateForView(boolean generateForView) {
this.generateForView = generateForView;
return this;
}
/**
* 获取表配置
*/
public Map<String, TableConfig> getTableConfigMap() {
return tableConfigMap;
}
/**
* 设置表配置
*/
public StrategyConfig setTableConfigMap(Map<String, TableConfig> tableConfigMap) {
this.tableConfigMap = tableConfigMap;
return this;
}
/**
* 获取列配置
*/
public Map<String, ColumnConfig> getColumnConfigMap() {
return columnConfigMap;
}
/**
* 设置列配置
*/
public StrategyConfig setColumnConfigMap(Map<String, ColumnConfig> columnConfigMap) {
this.columnConfigMap = columnConfigMap;
return this;
}
/**
* 获取生成哪些表
*/
public Set<String> getGenerateTables() {
return generateTables;
}
/**
* 设置生成哪些表
*/
public StrategyConfig setGenerateTables(Set<String> generateTables) {
this.generateTables = generateTables;
return this;
}
/**
* 获取不生成哪些表
*/
public Set<String> getUnGenerateTables() {
return unGenerateTables;
}
/**
* 设置不生成哪些表
*/
public StrategyConfig setUnGenerateTables(Set<String> unGenerateTables) {
this.unGenerateTables = unGenerateTables;
return this;
}
}

View File

@ -0,0 +1,87 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.codegen.config;
/**
* 生成 TableDef 的配置
*
* @author 王帅
* @since 2023-05-15
*/
@SuppressWarnings("unused")
public class TableDefConfig {
/**
* TableDef 类的前缀
*/
private String classPrefix = "";
/**
* TableDef 类的后缀
*/
private String classSuffix = "Def";
/**
* 是否覆盖之前生成的文件
*/
private boolean overwriteEnable;
/**
* 获取类前缀
*/
public String getClassPrefix() {
return classPrefix;
}
/**
* 设置类前缀
*/
public TableDefConfig setClassPrefix(String classPrefix) {
this.classPrefix = classPrefix;
return this;
}
/**
* 获取类后缀
*/
public String getClassSuffix() {
return classSuffix;
}
/**
* 设置类后缀
*/
public TableDefConfig setClassSuffix(String classSuffix) {
this.classSuffix = classSuffix;
return this;
}
/**
* 是否覆盖原有文件
*/
public boolean isOverwriteEnable() {
return overwriteEnable;
}
/**
* 设置是否覆盖原有文件
*/
public TableDefConfig setOverwriteEnable(boolean overwriteEnable) {
this.overwriteEnable = overwriteEnable;
return this;
}
}

View File

@ -0,0 +1,157 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.codegen.config;
import com.mybatisflex.codegen.constant.GenTypeConst;
import com.mybatisflex.codegen.generator.GeneratorFactory;
import com.mybatisflex.codegen.template.ITemplate;
import com.mybatisflex.codegen.template.impl.EnjoyTemplate;
/**
* 模板配置
*
* @author 王帅
* @since 2023-05-17
*/
@SuppressWarnings("unused")
public class TemplateConfig {
/**
* 生成代码的模板引擎
*/
private ITemplate template = new EnjoyTemplate();
/**
* 获取模板引擎
*/
public ITemplate getTemplate() {
return template;
}
/**
* 设置模板引擎
*/
public TemplateConfig setTemplate(ITemplate template) {
this.template = template;
return this;
}
/**
* 获取生成 Entity 模板文件的位置
*/
public String getEntity() {
return GeneratorFactory.getGenerator(GenTypeConst.ENTITY).getTemplatePath();
}
/**
* 设置生成 Entity 模板文件的位置
*/
public TemplateConfig setEntity(String entity) {
GeneratorFactory.getGenerator(GenTypeConst.ENTITY).setTemplatePath(entity);
return this;
}
/**
* 获取生成 Mapper 模板文件的位置
*/
public String getMapper() {
return GeneratorFactory.getGenerator(GenTypeConst.MAPPER).getTemplatePath();
}
/**
* 设置生成 Mapper 模板文件的位置
*/
public TemplateConfig setMapper(String mapper) {
GeneratorFactory.getGenerator(GenTypeConst.MAPPER).setTemplatePath(mapper);
return this;
}
/**
* 获取生成 Service 模板文件的位置
*/
public String getService() {
return GeneratorFactory.getGenerator(GenTypeConst.SERVICE).getTemplatePath();
}
/**
* 设置生成 Service 模板文件的位置
*/
public TemplateConfig setService(String service) {
GeneratorFactory.getGenerator(GenTypeConst.SERVICE).setTemplatePath(service);
return this;
}
/**
* 获取生成 ServiceImpl 模板文件的位置
*/
public String getServiceImpl() {
return GeneratorFactory.getGenerator(GenTypeConst.SERVICE_IMPL).getTemplatePath();
}
/**
* 设置生成 ServiceImpl 模板文件的位置
*/
public TemplateConfig setServiceImpl(String serviceImpl) {
GeneratorFactory.getGenerator(GenTypeConst.SERVICE_IMPL).setTemplatePath(serviceImpl);
return this;
}
/**
* 获取生成 Controller 模板文件的位置
*/
public String getController() {
return GeneratorFactory.getGenerator(GenTypeConst.CONTROLLER).getTemplatePath();
}
/**
* 设置生成 Controller 模板文件的位置
*/
public TemplateConfig setController(String controller) {
GeneratorFactory.getGenerator(GenTypeConst.CONTROLLER).setTemplatePath(controller);
return this;
}
/**
* 获取生成 TableDef 模板文件的位置
*/
public String getTableDef() {
return GeneratorFactory.getGenerator(GenTypeConst.TABLE_DEF).getTemplatePath();
}
/**
* 设置生成 TableDef 模板文件的位置
*/
public TemplateConfig setTableDef(String tableDef) {
GeneratorFactory.getGenerator(GenTypeConst.TABLE_DEF).setTemplatePath(tableDef);
return this;
}
/**
* 获取生成 MapperXml 模板文件的位置
*/
public String getMapperXml() {
return GeneratorFactory.getGenerator(GenTypeConst.MAPPER_XML).getTemplatePath();
}
/**
* 设置生成 MapperXml 模板文件的位置
*/
public TemplateConfig setMapperXml(String mapperXml) {
GeneratorFactory.getGenerator(GenTypeConst.MAPPER_XML).setTemplatePath(mapperXml);
return this;
}
}

View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.codegen.constant;
/**
* 生成类型常量
*
* @author 王帅
* @since 2023-05-17
*/
public class GenTypeConst {
public static final String ENTITY = "entity";
public static final String MAPPER = "mapper";
public static final String SERVICE = "service";
public static final String SERVICE_IMPL = "serviceImpl";
public static final String CONTROLLER = "controller";
public static final String TABLE_DEF = "tableDef";
public static final String MAPPER_XML = "mapperXml";
public static final String PACKAGE_INFO = "package-info";
private GenTypeConst() {
}
}

View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.codegen.constant;
/**
* 代码生成模板常量池
*
* @author 王帅
* @since 2023-05-16
*/
public final class TemplateConst {
public static final String ENTITY = "/templates/enjoy/entity.tpl";
public static final String MAPPER = "/templates/enjoy/mapper.tpl";
public static final String SERVICE = "/templates/enjoy/service.tpl";
public static final String SERVICE_IMPL = "/templates/enjoy/serviceImpl.tpl";
public static final String CONTROLLER = "/templates/enjoy/controller.tpl";
public static final String TABLE_DEF = "/templates/enjoy/tableDef.tpl";
public static final String MAPPER_XML = "/templates/enjoy/mapperXml.tpl";
public static final String PACKAGE_INFO = "/templates/enjoy/package-info.tpl";
private TemplateConst() {
}
}

View File

@ -45,7 +45,7 @@ public abstract class JdbcDialect implements IDialect {
column.setAutoIncrement(columnMetaData.isAutoIncrement(i));
//注释
column.setRemarks(columnRemarks.get(column.getName()));
column.setComment(columnRemarks.get(column.getName()));
table.addColumn(column);
}

View File

@ -30,7 +30,7 @@ public class Column {
private String property;
private String propertyType;
private String remarks;
private String comment;
private boolean isPrimaryKey = false;
private Boolean isAutoIncrement;
@ -63,12 +63,12 @@ public class Column {
this.propertyType = propertyType;
}
public String getRemarks() {
return remarks;
public String getComment() {
return comment;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
public void setComment(String comment) {
this.comment = comment;
}
public boolean isPrimaryKey() {
@ -104,12 +104,12 @@ public class Column {
}
public String buildRemarks(){
if (StringUtil.isBlank(remarks)){
public String buildComment(){
if (StringUtil.isBlank(comment)){
return "";
}else {
StringBuilder sb = new StringBuilder("/**\n")
.append(" * ").append(remarks).append("\n")
.append(" * ").append(comment).append("\n")
.append(" */");
return sb.toString();
}
@ -280,7 +280,7 @@ public class Column {
return "Column{" +
"name='" + name + '\'' +
", className='" + propertyType + '\'' +
", remarks='" + remarks + '\'' +
", remarks='" + comment + '\'' +
", isAutoIncrement=" + isAutoIncrement +
'}';
}

View File

@ -15,8 +15,7 @@
*/
package com.mybatisflex.codegen.entity;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.TableConfig;
import com.mybatisflex.codegen.config.*;
import com.mybatisflex.core.util.StringUtil;
import java.math.BigInteger;
@ -26,7 +25,7 @@ import java.util.stream.Collectors;
public class Table {
private String name;
private String remarks;
private String comment;
private Set<String> primaryKeys;
private List<Column> columns = new ArrayList<>();
@ -41,12 +40,12 @@ public class Table {
this.name = name;
}
public String getRemarks() {
return remarks;
public String getComment() {
return globalConfig.getJavadocConfig().formatTableComment(comment);
}
public void setRemarks(String remarks) {
this.remarks = remarks;
public void setComment(String comment) {
this.comment = comment;
}
@ -89,7 +88,7 @@ public class Table {
column.setAutoIncrement(false);
}
column.setColumnConfig(globalConfig.getColumnConfig(name, column.getName()));
column.setColumnConfig(globalConfig.getStrategyConfig().getColumnConfig(name, column.getName()));
columns.add(column);
}
@ -118,8 +117,10 @@ public class Table {
imports.addAll(column.getImportClasses());
}
EntityConfig entityConfig = globalConfig.getEntityConfig();
//开启 lombok
if (globalConfig.isEntityWithLombok()) {
if (entityConfig.isWithLombok()) {
//import lombok.AllArgsConstructor;
//import lombok.Builder;
//import lombok.Data;
@ -130,12 +131,12 @@ public class Table {
imports.add("lombok.NoArgsConstructor");
}
if (globalConfig.getEntitySupperClass() != null) {
imports.add(globalConfig.getEntitySupperClass().getName());
if (entityConfig.getSupperClass() != null) {
imports.add(entityConfig.getSupperClass().getName());
}
if (globalConfig.getEntityInterfaces() != null) {
for (Class<?> entityInterface : globalConfig.getEntityInterfaces()) {
if (entityConfig.getImplInterfaces() != null) {
for (Class<?> entityInterface : entityConfig.getImplInterfaces()) {
imports.add(entityInterface.getName());
}
}
@ -155,20 +156,9 @@ public class Table {
return imports.stream().sorted(Comparator.naturalOrder()).collect(Collectors.toList());
}
public String buildRemarks(){
if (StringUtil.isBlank(remarks)){
return "";
}else {
StringBuilder sb = new StringBuilder("/**\n")
.append(" * ").append(remarks).append("\n")
.append(" */");
return sb.toString();
}
}
public String getEntityJavaFileName() {
String entityJavaFileName = name;
String tablePrefix = globalConfig.getTablePrefix();
String tablePrefix = globalConfig.getStrategyConfig().getTablePrefix();
if (tablePrefix != null) {
String[] tablePrefixes = tablePrefix.split(",");
for (String prefix : tablePrefixes) {
@ -189,9 +179,10 @@ public class Table {
*/
public String buildEntityClassName() {
String entityJavaFileName = getEntityJavaFileName();
return globalConfig.getEntityClassPrefix()
EntityConfig entityConfig = globalConfig.getEntityConfig();
return entityConfig.getClassPrefix()
+ entityJavaFileName
+ globalConfig.getEntityClassSuffix();
+ entityConfig.getClassSuffix();
}
/**
@ -201,21 +192,36 @@ public class Table {
*/
public String buildTableDefClassName() {
String tableDefJavaFileName = getEntityJavaFileName();
return globalConfig.getTableDefClassPrefix()
TableDefConfig tableDefConfig = globalConfig.getTableDefConfig();
return tableDefConfig.getClassPrefix()
+ tableDefJavaFileName
+ globalConfig.getTableDefClassSuffix();
+ tableDefConfig.getClassSuffix();
}
/**
* 构建 MapperXml 的文件名称
*
* @return fileName
*/
public String buildMapperXmlFileName() {
String tableDefJavaFileName = getEntityJavaFileName();
MapperXmlConfig mapperXmlConfig = globalConfig.getMapperXmlConfig();
return mapperXmlConfig.getFilePrefix()
+ tableDefJavaFileName
+ mapperXmlConfig.getFileSuffix();
}
public String buildExtends() {
if (globalConfig.getEntitySupperClass() != null) {
return " extends " + globalConfig.getEntitySupperClass().getSimpleName();
EntityConfig entityConfig = globalConfig.getEntityConfig();
if (entityConfig.getSupperClass() != null) {
return " extends " + entityConfig.getSupperClass().getSimpleName();
} else {
return "";
}
}
public String buildImplements() {
Class<?>[] entityInterfaces = globalConfig.getEntityInterfaces();
Class<?>[] entityInterfaces = globalConfig.getEntityConfig().getImplInterfaces();
if (entityInterfaces != null && entityInterfaces.length > 0) {
return " implements " + StringUtil.join(", ", Arrays.stream(entityInterfaces)
.map(Class::getSimpleName).collect(Collectors.toList()));
@ -227,30 +233,34 @@ public class Table {
public String buildMapperClassName() {
String entityJavaFileName = getEntityJavaFileName();
return globalConfig.getMapperClassPrefix()
MapperConfig mapperConfig = globalConfig.getMapperConfig();
return mapperConfig.getClassPrefix()
+ entityJavaFileName
+ globalConfig.getMapperClassSuffix();
+ mapperConfig.getClassSuffix();
}
public String buildServiceClassName() {
String entityJavaFileName = getEntityJavaFileName();
return globalConfig.getServiceClassPrefix()
ServiceConfig serviceConfig = globalConfig.getServiceConfig();
return serviceConfig.getClassPrefix()
+ entityJavaFileName
+ globalConfig.getServiceClassSuffix();
+ serviceConfig.getClassSuffix();
}
public String buildServiceImplClassName() {
String entityJavaFileName = getEntityJavaFileName();
return globalConfig.getServiceImplClassPrefix()
ServiceImplConfig serviceImplConfig = globalConfig.getServiceImplConfig();
return serviceImplConfig.getClassPrefix()
+ entityJavaFileName
+ globalConfig.getServiceImplClassSuffix();
+ serviceImplConfig.getClassSuffix();
}
public String buildControllerClassName() {
String entityJavaFileName = getEntityJavaFileName();
return globalConfig.getControllerClassPrefix()
ControllerConfig controllerConfig = globalConfig.getControllerConfig();
return controllerConfig.getClassPrefix()
+ entityJavaFileName
+ globalConfig.getControllerClassSuffix();
+ controllerConfig.getClassSuffix();
}
/**
@ -258,7 +268,7 @@ public class Table {
*/
public String buildTableAnnotation() {
StringBuilder tableAnnotation = new StringBuilder();
if (globalConfig.isEntityWithLombok()) {
if (globalConfig.getEntityConfig().isWithLombok()) {
//@Data
//@Builder
//@NoArgsConstructor
@ -273,19 +283,19 @@ public class Table {
if (tableConfig != null) {
if (tableConfig.getSchema() != null) {
tableAnnotation.append(", schema = \"" + tableConfig.getSchema() + "\"");
tableAnnotation.append(", schema = \"").append(tableConfig.getSchema()).append("\"");
}
if (tableConfig.getCamelToUnderline() != null) {
tableAnnotation.append(", camelToUnderline = \"" + tableConfig.getCamelToUnderline() + "\"");
tableAnnotation.append(", camelToUnderline = \"").append(tableConfig.getCamelToUnderline()).append("\"");
}
if (tableConfig.getInsertListenerClass() != null) {
tableAnnotation.append(", onInsert = " + tableConfig.getInsertListenerClass().getSimpleName() + ".class");
tableAnnotation.append(", onInsert = ").append(tableConfig.getInsertListenerClass().getSimpleName()).append(".class");
}
if (tableConfig.getUpdateListenerClass() != null) {
tableAnnotation.append(", onUpdate = " + tableConfig.getUpdateListenerClass().getSimpleName() + ".class");
tableAnnotation.append(", onUpdate = ").append(tableConfig.getUpdateListenerClass().getSimpleName()).append(".class");
}
if (tableConfig.getSetListenerClass() != null) {
tableAnnotation.append(", onSet = " + tableConfig.getUpdateListenerClass().getSimpleName() + ".class");
tableAnnotation.append(", onSet = ").append(tableConfig.getUpdateListenerClass().getSimpleName()).append(".class");
}
if (Boolean.FALSE.equals(tableConfig.getMapperGenerateEnable())) {
tableAnnotation.append(", mapperGenerateEnable = false");
@ -294,40 +304,11 @@ public class Table {
return tableAnnotation.append(")").toString();
}
public String buildMapperImport() {
return globalConfig.getMapperSupperClass().getName();
}
public String buildServiceImport() {
return globalConfig.getServiceSupperClass().getName();
}
public String buildServiceImplImport() {
return globalConfig.getServiceImplSupperClass().getName();
}
public String buildMapperName() {
return globalConfig.getMapperSupperClass().getSimpleName();
}
public String buildServiceName() {
return globalConfig.getServiceSupperClass().getSimpleName();
}
public String buildServiceImplName() {
return globalConfig.getServiceImplSupperClass().getSimpleName();
}
public String buildControllerName() {
return globalConfig.getControllerSupperClass().getSimpleName();
}
@Override
public String toString() {
return "Table{" +
"name='" + name + '\'' +
", remarks='" + remarks + '\'' +
", remarks='" + comment + '\'' +
", primaryKeys='" + primaryKeys + '\'' +
", columns=" + columns +
'}';

View File

@ -1,4 +1,4 @@
/**
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
@ -15,6 +15,7 @@
*/
package com.mybatisflex.codegen.generator;
import com.mybatisflex.codegen.constant.GenTypeConst;
import com.mybatisflex.codegen.generator.impl.*;
import java.util.Collection;
@ -26,12 +27,18 @@ public class GeneratorFactory {
private static final Map<String, IGenerator> generators = new HashMap<>();
static {
registerGenerator("entity", new EntityGenerator());
registerGenerator("mapper", new MapperGenerator());
registerGenerator("service", new ServiceGenerator());
registerGenerator("serviceImpl", new ServiceImplGenerator());
registerGenerator("controller", new ControllerGenerator());
registerGenerator("tableDef", new TableDefGenerator());
registerGenerator(GenTypeConst.ENTITY, new EntityGenerator());
registerGenerator(GenTypeConst.MAPPER, new MapperGenerator());
registerGenerator(GenTypeConst.SERVICE, new ServiceGenerator());
registerGenerator(GenTypeConst.SERVICE_IMPL, new ServiceImplGenerator());
registerGenerator(GenTypeConst.CONTROLLER, new ControllerGenerator());
registerGenerator(GenTypeConst.TABLE_DEF, new TableDefGenerator());
registerGenerator(GenTypeConst.MAPPER_XML, new MapperXmlGenerator());
registerGenerator(GenTypeConst.PACKAGE_INFO, new PackageInfoGenerator());
}
public static IGenerator getGenerator(String genType) {
return generators.get(genType);
}

View File

@ -1,4 +1,4 @@
/**
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
@ -18,6 +18,34 @@ package com.mybatisflex.codegen.generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.entity.Table;
/**
* 文件生成器接口
*
* @author Michael Yang
* @author 王帅
*/
public interface IGenerator {
/**
* 获取模板文件位置
*
* @return 路径
*/
String getTemplatePath();
/**
* 设置模板文件位置
*
* @param templatePath
*/
void setTemplatePath(String templatePath);
/**
* 根据模板生成文件
*
* @param table 表内容
* @param globalConfig 全局配置
*/
void generate(Table table, GlobalConfig globalConfig);
}

View File

@ -1,4 +1,4 @@
/**
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
@ -15,7 +15,10 @@
*/
package com.mybatisflex.codegen.generator.impl;
import com.mybatisflex.codegen.config.ControllerConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.PackageConfig;
import com.mybatisflex.codegen.constant.TemplateConst;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.codegen.generator.IGenerator;
@ -29,12 +32,12 @@ import java.util.Map;
* @author 王帅
* @since 2023-05-14
*/
@SuppressWarnings("unused")
public class ControllerGenerator implements IGenerator {
private String templatePath = "/templates/enjoy/controller.tpl";
private String templatePath;
public ControllerGenerator() {
this(TemplateConst.CONTROLLER);
}
public ControllerGenerator(String templatePath) {
@ -48,20 +51,34 @@ public class ControllerGenerator implements IGenerator {
return;
}
String controllerPackagePath = globalConfig.getControllerPackage().replace(".", "/");
File controllerJavaFile = new File(globalConfig.getSourceDir(), controllerPackagePath + "/" +
PackageConfig packageConfig = globalConfig.getPackageConfig();
ControllerConfig controllerConfig = globalConfig.getControllerConfig();
String controllerPackagePath = packageConfig.getControllerPackage().replace(".", "/");
File controllerJavaFile = new File(packageConfig.getSourceDir(), controllerPackagePath + "/" +
table.buildControllerClassName() + ".java");
if (controllerJavaFile.exists() && !globalConfig.isControllerOverwriteEnable()) {
if (controllerJavaFile.exists() && controllerConfig.isOverwriteEnable()) {
return;
}
Map<String, Object> params = new HashMap<>(2);
Map<String, Object> params = new HashMap<>(4);
params.put("table", table);
params.put("globalConfig", globalConfig);
params.put("packageConfig", packageConfig);
params.put("controllerConfig", controllerConfig);
params.put("javadocConfig", globalConfig.getJavadocConfig());
globalConfig.getTemplateEngine().generate(params, templatePath, controllerJavaFile);
globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, controllerJavaFile);
}
public String getTemplatePath() {
return templatePath;
}
public void setTemplatePath(String templatePath) {
this.templatePath = templatePath;
}
}

View File

@ -1,4 +1,4 @@
/**
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
@ -15,7 +15,10 @@
*/
package com.mybatisflex.codegen.generator.impl;
import com.mybatisflex.codegen.config.EntityConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.PackageConfig;
import com.mybatisflex.codegen.constant.TemplateConst;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.codegen.generator.IGenerator;
@ -23,11 +26,18 @@ import java.io.File;
import java.util.HashMap;
import java.util.Map;
/**
* Entity 生成器
*
* @author Michael Yang
* @author 王帅
*/
public class EntityGenerator implements IGenerator {
private String templatePath = "/templates/enjoy/entity.tpl";
private String templatePath;
public EntityGenerator() {
this(TemplateConst.ENTITY);
}
public EntityGenerator(String templatePath) {
@ -37,14 +47,38 @@ public class EntityGenerator implements IGenerator {
@Override
public void generate(Table table, GlobalConfig globalConfig) {
String entityPackagePath = globalConfig.getEntityPackage().replace(".", "/");
File entityJavaFile = new File(globalConfig.getSourceDir(), entityPackagePath + "/" +
if (!globalConfig.isEntityGenerateEnable()) {
return;
}
PackageConfig packageConfig = globalConfig.getPackageConfig();
EntityConfig entityConfig = globalConfig.getEntityConfig();
String entityPackagePath = packageConfig.getEntityPackage().replace(".", "/");
File entityJavaFile = new File(packageConfig.getSourceDir(), entityPackagePath + "/" +
table.buildEntityClassName() + ".java");
Map<String, Object> params = new HashMap<>();
params.put("table", table);
params.put("globalConfig", globalConfig);
globalConfig.getTemplateEngine().generate(params, templatePath, entityJavaFile);
if (entityJavaFile.exists() && !entityConfig.isOverwriteEnable()) {
return;
}
Map<String, Object> params = new HashMap<>(4);
params.put("table", table);
params.put("entityConfig", entityConfig);
params.put("packageConfig", packageConfig);
params.put("javadocConfig", globalConfig.getJavadocConfig());
globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, entityJavaFile);
}
public String getTemplatePath() {
return templatePath;
}
public void setTemplatePath(String templatePath) {
this.templatePath = templatePath;
}
}

View File

@ -1,4 +1,4 @@
/**
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
@ -16,6 +16,9 @@
package com.mybatisflex.codegen.generator.impl;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.MapperConfig;
import com.mybatisflex.codegen.config.PackageConfig;
import com.mybatisflex.codegen.constant.TemplateConst;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.codegen.generator.IGenerator;
@ -23,11 +26,18 @@ import java.io.File;
import java.util.HashMap;
import java.util.Map;
/**
* Mapper 生成器
*
* @author Michael Yang
* @author 王帅
*/
public class MapperGenerator implements IGenerator {
private String templatePath = "/templates/enjoy/mapper.tpl";
private String templatePath;
public MapperGenerator() {
this(TemplateConst.MAPPER);
}
public MapperGenerator(String templatePath) {
@ -41,20 +51,34 @@ public class MapperGenerator implements IGenerator {
return;
}
String mapperPackagePath = globalConfig.getMapperPackage().replace(".", "/");
File mapperJavaFile = new File(globalConfig.getSourceDir(), mapperPackagePath + "/" +
PackageConfig packageConfig = globalConfig.getPackageConfig();
MapperConfig mapperConfig = globalConfig.getMapperConfig();
String mapperPackagePath = packageConfig.getMapperPackage().replace(".", "/");
File mapperJavaFile = new File(packageConfig.getSourceDir(), mapperPackagePath + "/" +
table.buildMapperClassName() + ".java");
if (mapperJavaFile.exists() && !globalConfig.isMapperOverwriteEnable()) {
return;//ignore
if (mapperJavaFile.exists() && !mapperConfig.isOverwriteEnable()) {
return;
}
Map<String, Object> params = new HashMap<>();
Map<String, Object> params = new HashMap<>(4);
params.put("table", table);
params.put("globalConfig", globalConfig);
params.put("mapperConfig", mapperConfig);
params.put("packageConfig", packageConfig);
params.put("javadocConfig", globalConfig.getJavadocConfig());
globalConfig.getTemplateEngine().generate(params, templatePath, mapperJavaFile);
globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, mapperJavaFile);
}
public String getTemplatePath() {
return templatePath;
}
public void setTemplatePath(String templatePath) {
this.templatePath = templatePath;
}
}

View File

@ -0,0 +1,81 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.codegen.generator.impl;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.MapperXmlConfig;
import com.mybatisflex.codegen.config.PackageConfig;
import com.mybatisflex.codegen.constant.TemplateConst;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.codegen.generator.IGenerator;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
/**
* MapperXml 生成器
*
* @author 王帅
* @since 2023-05-17
*/
public class MapperXmlGenerator implements IGenerator {
private String templatePath;
public MapperXmlGenerator() {
this(TemplateConst.MAPPER_XML);
}
public MapperXmlGenerator(String templatePath) {
this.templatePath = templatePath;
}
@Override
public void generate(Table table, GlobalConfig globalConfig) {
if (!globalConfig.isMapperXmlGenerateEnable()) {
return;
}
PackageConfig packageConfig = globalConfig.getPackageConfig();
MapperXmlConfig mapperXmlConfig = globalConfig.getMapperXmlConfig();
File mapperXmlFile = new File(packageConfig.getMapperXmlPath() + "/" +
table.buildMapperXmlFileName() + ".xml");
if (mapperXmlFile.exists() && !mapperXmlConfig.isOverwriteEnable()) {
return;
}
Map<String, Object> params = new HashMap<>(2);
params.put("table", table);
params.put("packageConfig", packageConfig);
globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, mapperXmlFile);
}
public String getTemplatePath() {
return templatePath;
}
public void setTemplatePath(String templatePath) {
this.templatePath = templatePath;
}
}

View File

@ -0,0 +1,117 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.codegen.generator.impl;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.JavadocConfig;
import com.mybatisflex.codegen.config.PackageConfig;
import com.mybatisflex.codegen.constant.TemplateConst;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.codegen.generator.IGenerator;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* package-info.java 生成器
*
* @author 王帅
* @since 2023-05-17
*/
public class PackageInfoGenerator implements IGenerator {
private String templatePath;
public PackageInfoGenerator() {
this(TemplateConst.PACKAGE_INFO);
}
public PackageInfoGenerator(String templatePath) {
this.templatePath = templatePath;
}
@Override
public void generate(Table table, GlobalConfig globalConfig) {
if (!globalConfig.isPackageInfoGenerateEnable()) {
return;
}
JavadocConfig javadocConfig = globalConfig.getJavadocConfig();
PackageConfig packageConfig = globalConfig.getPackageConfig();
String sourceDir = packageConfig.getSourceDir();
List<Data> dataList = new ArrayList<>();
if (globalConfig.isEntityGenerateEnable()) {
dataList.add(new Data(sourceDir, packageConfig.getEntityPackage(), javadocConfig.getEntityPackage()));
}
if (globalConfig.isMapperGenerateEnable()) {
dataList.add(new Data(sourceDir, packageConfig.getMapperPackage(), javadocConfig.getMapperPackage()));
}
if (globalConfig.isServiceGenerateEnable()) {
dataList.add(new Data(sourceDir, packageConfig.getServicePackage(), javadocConfig.getServicePackage()));
}
if (globalConfig.isServiceImplGenerateEnable()) {
dataList.add(new Data(sourceDir, packageConfig.getServiceImplPackage(), javadocConfig.getServiceImplPackage()));
}
if (globalConfig.isControllerGenerateEnable()) {
dataList.add(new Data(sourceDir, packageConfig.getControllerPackage(), javadocConfig.getControllerPackage()));
}
if (globalConfig.isTableDefGenerateEnable()) {
dataList.add(new Data(sourceDir, packageConfig.getTableDefPackage(), javadocConfig.getTableDefPackage()));
}
dataList.forEach(data -> {
Map<String, Object> params = new HashMap<>(3);
params.put("packageName", data.packageName);
params.put("packageComment", data.packageComment);
params.put("javadocConfig", javadocConfig);
globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, data.filePath);
});
}
public String getTemplatePath() {
return templatePath;
}
public void setTemplatePath(String templatePath) {
this.templatePath = templatePath;
}
private static class Data {
String packageName;
String packageComment;
File filePath;
Data(String sourceDir, String packageName, String packageComment) {
this.packageName = packageName;
this.packageComment = packageComment;
this.filePath = getFilePath(sourceDir, packageName);
}
File getFilePath(String sourceDir, String packageName) {
return new File(sourceDir, packageName.replace(".", "/") + "/package-info.java");
}
}
}

View File

@ -1,4 +1,4 @@
/**
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
@ -16,6 +16,9 @@
package com.mybatisflex.codegen.generator.impl;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.PackageConfig;
import com.mybatisflex.codegen.config.ServiceConfig;
import com.mybatisflex.codegen.constant.TemplateConst;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.codegen.generator.IGenerator;
@ -29,12 +32,12 @@ import java.util.Map;
* @author 王帅
* @since 2023-05-14
*/
@SuppressWarnings("unused")
public class ServiceGenerator implements IGenerator {
private String templatePath = "/templates/enjoy/service.tpl";
private String templatePath;
public ServiceGenerator() {
this(TemplateConst.SERVICE);
}
public ServiceGenerator(String templatePath) {
@ -48,20 +51,34 @@ public class ServiceGenerator implements IGenerator {
return;
}
String servicePackagePath = globalConfig.getServicePackage().replace(".", "/");
File serviceJavaFile = new File(globalConfig.getSourceDir(), servicePackagePath + "/" +
PackageConfig packageConfig = globalConfig.getPackageConfig();
ServiceConfig serviceConfig = globalConfig.getServiceConfig();
String servicePackagePath = packageConfig.getServicePackage().replace(".", "/");
File serviceJavaFile = new File(packageConfig.getSourceDir(), servicePackagePath + "/" +
table.buildServiceClassName() + ".java");
if (serviceJavaFile.exists() && !globalConfig.isServiceOverwriteEnable()) {
if (serviceJavaFile.exists() && !serviceConfig.isOverwriteEnable()) {
return;
}
Map<String, Object> params = new HashMap<>(2);
Map<String, Object> params = new HashMap<>(4);
params.put("table", table);
params.put("globalConfig", globalConfig);
params.put("serviceConfig", serviceConfig);
params.put("packageConfig", packageConfig);
params.put("javadocConfig", globalConfig.getJavadocConfig());
globalConfig.getTemplateEngine().generate(params, templatePath, serviceJavaFile);
globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, serviceJavaFile);
}
public String getTemplatePath() {
return templatePath;
}
public void setTemplatePath(String templatePath) {
this.templatePath = templatePath;
}
}

View File

@ -1,4 +1,4 @@
/**
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
@ -16,6 +16,9 @@
package com.mybatisflex.codegen.generator.impl;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.PackageConfig;
import com.mybatisflex.codegen.config.ServiceImplConfig;
import com.mybatisflex.codegen.constant.TemplateConst;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.codegen.generator.IGenerator;
@ -29,12 +32,12 @@ import java.util.Map;
* @author 王帅
* @since 2023-05-14
*/
@SuppressWarnings("unused")
public class ServiceImplGenerator implements IGenerator {
private String templatePath = "/templates/enjoy/serviceImpl.tpl";
private String templatePath;
public ServiceImplGenerator() {
this(TemplateConst.SERVICE_IMPL);
}
public ServiceImplGenerator(String templatePath) {
@ -48,20 +51,34 @@ public class ServiceImplGenerator implements IGenerator {
return;
}
String serviceImplPackagePath = globalConfig.getServiceImplPackage().replace(".", "/");
File serviceImplJavaFile = new File(globalConfig.getSourceDir(), serviceImplPackagePath + "/" +
PackageConfig packageConfig = globalConfig.getPackageConfig();
ServiceImplConfig serviceImplConfig = globalConfig.getServiceImplConfig();
String serviceImplPackagePath = packageConfig.getServiceImplPackage().replace(".", "/");
File serviceImplJavaFile = new File(packageConfig.getSourceDir(), serviceImplPackagePath + "/" +
table.buildServiceImplClassName() + ".java");
if (serviceImplJavaFile.exists() && !globalConfig.isServiceImplOverwriteEnable()) {
if (serviceImplJavaFile.exists() && !serviceImplConfig.isOverwriteEnable()) {
return;
}
Map<String, Object> params = new HashMap<>(2);
Map<String, Object> params = new HashMap<>(4);
params.put("table", table);
params.put("globalConfig", globalConfig);
params.put("packageConfig", packageConfig);
params.put("serviceImplConfig", serviceImplConfig);
params.put("javadocConfig", globalConfig.getJavadocConfig());
globalConfig.getTemplateEngine().generate(params, templatePath, serviceImplJavaFile);
globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, serviceImplJavaFile);
}
public String getTemplatePath() {
return templatePath;
}
public void setTemplatePath(String templatePath) {
this.templatePath = templatePath;
}
}

View File

@ -1,4 +1,4 @@
/**
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
@ -16,6 +16,9 @@
package com.mybatisflex.codegen.generator.impl;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.PackageConfig;
import com.mybatisflex.codegen.config.TableDefConfig;
import com.mybatisflex.codegen.constant.TemplateConst;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.codegen.generator.IGenerator;
@ -23,11 +26,18 @@ import java.io.File;
import java.util.HashMap;
import java.util.Map;
/**
* TableDef 生成器
*
* @author Michael Yang
* @author 王帅
*/
public class TableDefGenerator implements IGenerator {
private String templatePath = "/templates/enjoy/tableDef.tpl";
private String templatePath;
public TableDefGenerator() {
this(TemplateConst.TABLE_DEF);
}
public TableDefGenerator(String templatePath) {
@ -41,15 +51,34 @@ public class TableDefGenerator implements IGenerator {
return;
}
String tableDefPackagePath = globalConfig.getTableDefPackage().replace(".", "/");
File tableDefJavaFile = new File(globalConfig.getSourceDir(), tableDefPackagePath + "/" +
PackageConfig packageConfig = globalConfig.getPackageConfig();
TableDefConfig tableDefConfig = globalConfig.getTableDefConfig();
String tableDefPackagePath = packageConfig.getTableDefPackage().replace(".", "/");
File tableDefJavaFile = new File(packageConfig.getSourceDir(), tableDefPackagePath + "/" +
table.buildTableDefClassName() + ".java");
Map<String, Object> params = new HashMap<>();
params.put("table", table);
params.put("globalConfig", globalConfig);
if (tableDefJavaFile.exists() && !tableDefConfig.isOverwriteEnable()) {
return;
}
globalConfig.getTemplateEngine().generate(params, templatePath, tableDefJavaFile);
Map<String, Object> params = new HashMap<>(4);
params.put("table", table);
params.put("packageConfig", packageConfig);
params.put("tableDefConfig", tableDefConfig);
params.put("javadocConfig", globalConfig.getJavadocConfig());
globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, tableDefJavaFile);
}
public String getTemplatePath() {
return templatePath;
}
public void setTemplatePath(String templatePath) {
this.templatePath = templatePath;
}
}

View File

@ -13,10 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.codegen.template;
package com.mybatisflex.codegen.template.impl;
import com.jfinal.template.Engine;
import com.jfinal.template.expr.ast.FieldGetters;
import com.jfinal.template.source.ClassPathSource;
import com.jfinal.template.source.FileSource;
import com.jfinal.template.source.ISource;
import com.jfinal.template.source.ISourceFactory;
import com.mybatisflex.codegen.template.ITemplate;
import com.mybatisflex.core.util.StringUtil;
import java.io.File;
@ -29,8 +34,8 @@ public class EnjoyTemplate implements ITemplate {
public EnjoyTemplate() {
engine = Engine.create("mybatis-flex", engine -> {
engine.setToClassPathSourceFactory();
engine.addSharedMethod(StringUtil.class);
engine.setSourceFactory(new FileAndClassPathSourceFactory());
});
// 以下配置将支持 user.girl 表达式去调用 user 对象的 boolean isGirl() 方法
Engine.addFieldGetterToFirst(new FieldGetters.IsMethodFieldGetter());
@ -48,4 +53,18 @@ public class EnjoyTemplate implements ITemplate {
e.printStackTrace();
}
}
public static class FileAndClassPathSourceFactory implements ISourceFactory {
@Override
public ISource getSource(String baseTemplatePath, String fileName, String encoding) {
// 先从文件寻找资源找不到再从类路径寻找资源
if (new File(fileName).exists()) {
return new FileSource(baseTemplatePath, fileName, encoding);
}
return new ClassPathSource(baseTemplatePath, fileName, encoding);
}
}
}

View File

@ -1,27 +1,112 @@
package #(globalConfig.controllerPackage);
#set(tableComment = table.getComment())
#set(entityClassName = table.buildEntityClassName())
#set(entityVarName = firstCharToLowerCase(entityClassName))
#set(serviceVarName = firstCharToLowerCase(table.buildServiceClassName()))
package #(packageConfig.controllerPackage);
import com.mybatisflex.core.paginate.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
#if(globalConfig.restStyleController)
import org.springframework.beans.factory.annotation.Autowired;
import #(packageConfig.entityPackage).#(entityClassName);
import #(packageConfig.servicePackage).#(table.buildServiceClassName());
#if(controllerConfig.restStyle)
import org.springframework.web.bind.annotation.RestController;
#else
import org.springframework.stereotype.Controller;
#end
#if(globalConfig.controllerSupperClass)
import #(table.buildControllerImport())
#if(controllerConfig.supperClass)
import #(controllerConfig.buildSuperClassImport());
#end
#if(globalConfig.restStyleController)
import java.io.Serializable;
import java.util.List;
/**
* #(tableComment) 控制层。
*
* @author #(javadocConfig.getAuthor())
* @since #(javadocConfig.getSince())
*/
#if(controllerConfig.restStyle)
@RestController
#else
@Controller
#end
@RequestMapping("/#(table.getEntityJavaFileName())")
#if(globalConfig.controllerSupperClass)
public class #(table.table.buildControllerClassName()) extends #(table.buildControllerName()) {
@RequestMapping("/#(firstCharToLowerCase(entityClassName))")
public class #(table.buildControllerClassName()) #if(controllerConfig.supperClass)extends #(controllerConfig.buildSuperClassName()) #end {
@Autowired
private #(table.buildServiceClassName()) #(serviceVarName);
/**
* 添加#(tableComment)。
*
* @param #(entityVarName) #(tableComment)
* @return {@code true} 添加成功,{@code false} 添加失败
*/
@PostMapping("save")
public boolean save(@RequestBody #(entityClassName) #(entityVarName)) {
return #(serviceVarName).save(#(entityVarName));
}
/**
* 根据主键删除#(tableComment)。
*
* @param id 主键
* @return {@code true} 删除成功,{@code false} 删除失败
*/
@DeleteMapping("remove/{id}")
public boolean remove(@PathVariable Serializable id) {
return #(serviceVarName).removeById(id);
}
/**
* 根据主键更新#(tableComment)。
*
* @param #(entityVarName) #(tableComment)
* @return {@code true} 更新成功,{@code false} 更新失败
*/
@PutMapping("update")
public boolean update(@RequestBody #(entityClassName) #(entityVarName)) {
return #(serviceVarName).updateById(#(entityVarName));
}
/**
* 查询所有#(tableComment)。
*
* @return 所有数据
*/
@GetMapping("list")
public List<#(entityClassName)> list() {
return #(serviceVarName).list();
}
/**
* 根据#(tableComment)主键获取详细信息。
*
* @param id #(tableComment)主键
* @return #(tableComment)详情
*/
@GetMapping("getInfo/{id}")
public #(entityClassName) getInfo(@PathVariable Serializable id) {
return #(serviceVarName).getById(id);
}
/**
* 分页查询#(tableComment)。
*
* @param page 分页对象
* @return 分页对象
*/
@GetMapping("page")
public Page<#(entityClassName)> page(Page<#(entityClassName)> page) {
return #(serviceVarName).page(page);
}
}
#else
public class #(table.buildControllerClassName()) {
}
#end

View File

@ -1,19 +1,24 @@
package #(globalConfig.entityPackage);
package #(packageConfig.entityPackage);
#for(importClass:table.buildImports())
import #(importClass);
#end
#(table.buildRemarks())
/**
* #(table.getComment()) 实体类。
*
* @author #(javadocConfig.getAuthor())
* @since #(javadocConfig.getSince())
*/
#(table.buildTableAnnotation())
public class #(table.buildEntityClassName())#(table.buildExtends())#(table.buildImplements()) {
#for(column: table.columns)
#(column.buildRemarks())
#(column.buildComment())
#(column.buildAnnotations())private #(column.propertySimpleType) #(column.property);
#end
#if(!globalConfig.isEntityWithLombok())
#if(!entityConfig.isWithLombok())
#for(column: table.columns)
public #(column.propertySimpleType) #(column.getterMethod())() {
return #(column.property);

View File

@ -1,8 +1,14 @@
package #(globalConfig.mapperPackage);
package #(packageConfig.mapperPackage);
import #(table.buildMapperImport());
import #(globalConfig.entityPackage).#(table.buildEntityClassName());
import #(mapperConfig.buildSuperClassImport());
import #(packageConfig.entityPackage).#(table.buildEntityClassName());
public interface #(table.buildMapperClassName()) extends #(table.buildMapperName())<#(table.buildEntityClassName())> {
/**
* #(table.getComment()) 映射层。
*
* @author #(javadocConfig.getAuthor())
* @since #(javadocConfig.getSince())
*/
public interface #(table.buildMapperClassName()) extends #(mapperConfig.buildSuperClassName())<#(table.buildEntityClassName())> {
}

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="#(packageConfig.mapperPackage).#(table.buildMapperClassName())">
</mapper>

View File

@ -0,0 +1,7 @@
/**
* #(packageComment)
*
* @author #(javadocConfig.getAuthor())
* @since #(javadocConfig.getSince())
*/
package #(packageName);

View File

@ -1,8 +1,14 @@
package #(globalConfig.servicePackage);
package #(packageConfig.servicePackage);
import #(table.buildServiceImport());
import #(globalConfig.entityPackage).#(table.buildEntityClassName());
import #(serviceConfig.buildSuperClassImport());
import #(packageConfig.entityPackage).#(table.buildEntityClassName());
public interface #(table.buildServiceClassName()) extends #(table.buildServiceName())<#(table.buildEntityClassName())> {
/**
* #(table.getComment()) 服务层。
*
* @author #(javadocConfig.getAuthor())
* @since #(javadocConfig.getSince())
*/
public interface #(table.buildServiceClassName()) extends #(serviceConfig.buildSuperClassName())<#(table.buildEntityClassName())> {
}

View File

@ -1,12 +1,18 @@
package #(globalConfig.serviceImplPackage);
package #(packageConfig.serviceImplPackage);
import #(table.buildServiceImplImport());
import #(globalConfig.entityPackage).#(table.buildEntityClassName());
import #(globalConfig.mapperPackage).#(table.buildMapperClassName());
import #(globalConfig.servicePackage).#(table.buildServiceClassName());
import #(serviceImplConfig.buildSuperClassImport());
import #(packageConfig.entityPackage).#(table.buildEntityClassName());
import #(packageConfig.mapperPackage).#(table.buildMapperClassName());
import #(packageConfig.servicePackage).#(table.buildServiceClassName());
import org.springframework.stereotype.Service;
/**
* #(table.getComment()) 服务层实现。
*
* @author #(javadocConfig.getAuthor())
* @since #(javadocConfig.getSince())
*/
@Service
public class #(table.buildServiceImplClassName()) extends #(table.buildServiceImplName())<#(table.buildMapperClassName()), #(table.buildEntityClassName())> implements #(table.buildServiceClassName()) {
public class #(table.buildServiceImplClassName()) extends #(serviceImplConfig.buildSuperClassName())<#(table.buildMapperClassName()), #(table.buildEntityClassName())> implements #(table.buildServiceClassName()) {
}

View File

@ -1,9 +1,14 @@
package #(globalConfig.entityPackage).tables;
package #(packageConfig.tableDefPackage);
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.table.TableDef;
/**
* #(table.getComment()) 表定义层。
*
* @author #(javadocConfig.getAuthor())
* @since #(javadocConfig.getSince())
*/
public class #(table.buildTableDefClassName()) extends TableDef {
public static final #(table.buildTableDefClassName()) #(table.name) = new #(table.buildTableDefClassName())("#(table.name)");

View File

@ -1,4 +1,4 @@
/**
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
@ -13,83 +13,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.codegen.test;
import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.TableConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.Test;
import java.util.function.UnaryOperator;
public class GeneratorTest {
//@Test
public void testGenerator() {
//配置数据源
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/jbootadmin?characterEncoding=utf-8");
// dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/hh-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&allowMultiQueries=true");
dataSource.setUsername("root");
dataSource.setPassword("123456");
// JdbcTypeMapping.registerMapping(BigInteger.class, Long.class);
// JdbcTypeMapping.registerMapping(Integer.class, Long.class);
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setSourceDir(System.getProperty("user.dir") + "/src/test/java");
// globalConfig.setTablePrefix("tb_");
// globalConfig.setEntityWithLombok(true);
globalConfig.setEntitySupperClass(BaseEntity.class);
//设置只生成哪些表
globalConfig.addGenerateTable("account", "account_session");
//设置 entity 的包名
globalConfig.setEntityPackage("com.test.entity");
globalConfig.setEntityClassPrefix("My");
globalConfig.setEntityClassSuffix("Entity");
//设置 entity 的包名
globalConfig.setTableDefGenerateEnable(true);
globalConfig.setTableDefPackage("com.test.entity.tables");
globalConfig.setTableDefClassPrefix("My");
globalConfig.setTableDefClassSuffix("TableDef");
//是否生成 mapper 默认为 false
globalConfig.setMapperGenerateEnable(true);
globalConfig.setMapperClassPrefix("Flex");
globalConfig.setMapperClassSuffix("Dao");
//设置 mapper 类的包名
globalConfig.setMapperPackage("com.test.mapper");
globalConfig.setMapperSupperClass(MyBaseMapper.class);
TableConfig tableConfig = new TableConfig();
tableConfig.setTableName("account");
tableConfig.setUpdateListenerClass(MyUpdateListener.class);
globalConfig.addTableConfig(tableConfig);
//可以单独配置某个列
ColumnConfig columnConfig = new ColumnConfig();
columnConfig.setColumnName("tenant_id");
columnConfig.setLarge(true);
columnConfig.setVersion(true);
globalConfig.addColumnConfig("account", columnConfig);
//通过 datasource globalConfig 创建代码生成器
Generator generator = new Generator(dataSource, globalConfig);
//开始生成代码
generator.generate();
}
// @Test
public void testCodeGen() {
public void testCodeGen1() {
//配置数据源
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8");
@ -97,27 +35,106 @@ public class GeneratorTest {
dataSource.setPassword("12345678");
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setSourceDir(System.getProperty("user.dir") + "/src/test/java");
globalConfig.setTablePrefix("sys_");
globalConfig.setBasePackage("com.test");
globalConfig.setEntityWithLombok(true);
//用户信息表用于存放用户信息 -> 用户信息
UnaryOperator<String> tableFormat = (e) -> e.split("")[0].replace("", "");
//设置注解生成配置
globalConfig.setAuthor("Michael Yang");
globalConfig.setTableCommentFormat(tableFormat);
//设置生成文件目录和根包
globalConfig.setSourceDir(System.getProperty("user.dir") + "/src/test/java");
globalConfig.setMapperXmlPath(System.getProperty("user.dir") + "/src/test/java/resources/mapper");
globalConfig.setBasePackage("com.test");
//设置表前缀和只生成哪些表
globalConfig.setTablePrefix("sys_");
globalConfig.setGenerateTable("sys_user");
//设置模板路径
globalConfig.setEntityTemplatePath("D:\\Documents\\配置文件\\entity.tpl");
//配置生成 entity
globalConfig.setEntityGenerateEnable(true);
globalConfig.setEntityWithLombok(true);
globalConfig.setEntitySupperClass(BaseEntity.class);
// 设置只生成哪些表
globalConfig.addGenerateTable("sys_user");
// 设置 entity 的包名
globalConfig.setTableDefGenerateEnable(true);
// 是否生成 mapper 默认为 false
//配置生成 mapper
globalConfig.setMapperGenerateEnable(true);
// 是否生成 service 默认为 false
//配置生成 service
globalConfig.setServiceGenerateEnable(true);
// 是否生成 serviceImpl 默认为 false
//配置生成 serviceImpl
globalConfig.setServiceImplGenerateEnable(true);
// 是否生成 controller 默认为 false
//配置生成 controller
globalConfig.setControllerGenerateEnable(true);
//配置生成 tableDef
globalConfig.setTableDefGenerateEnable(true);
//配置生成 mapperXml
globalConfig.setMapperXmlGenerateEnable(true);
//配置生成 package-info.java
globalConfig.setPackageInfoGenerateEnable(true);
//通过 datasource globalConfig 创建代码生成器
Generator generator = new Generator(dataSource, globalConfig);
//开始生成代码
generator.generate();
}
@Test
public void testCodeGen2() {
//配置数据源
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8");
dataSource.setUsername("root");
dataSource.setPassword("12345678");
GlobalConfig globalConfig = new GlobalConfig();
//用户信息表用于存放用户信息 -> 用户信息
UnaryOperator<String> tableFormat = (e) -> e.split("")[0].replace("", "");
//设置注解生成配置
globalConfig.getJavadocConfig()
.setAuthor("王帅")
.setTableCommentFormat(tableFormat);
//设置生成文件目录和根包
globalConfig.getPackageConfig()
.setSourceDir(System.getProperty("user.dir") + "/src/test/java")
.setMapperXmlPath(System.getProperty("user.dir") + "/src/test/java/resources/mapper")
.setBasePackage("com.test");
//设置表前缀和只生成哪些表
globalConfig.getStrategyConfig()
.setTablePrefix("sys_")
.setGenerateTable("sys_user");
//设置模板路径
globalConfig.getTemplateConfig()
.setEntity("D:\\Documents\\配置文件\\entity.tpl");
//配置生成 entity
globalConfig.enableEntity()
.setOverwriteEnable(true)
.setWithLombok(true)
.setSupperClass(BaseEntity.class);
//配置生成 mapper
globalConfig.enableMapper();
//配置生成 service
globalConfig.enableService();
//配置生成 serviceImpl
globalConfig.enableServiceImpl();
//配置生成 controller
globalConfig.enableController();
//配置生成 tableDef
globalConfig.enableTableDef();
//配置生成 mapperXml
globalConfig.enableMapperXml();
//配置生成 package-info.java
globalConfig.enablePackageInfo();
//通过 datasource globalConfig 创建代码生成器
Generator generator = new Generator(dataSource, globalConfig);

View File

@ -1,10 +1,25 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.codegen.test;
import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.dialect.IDialect;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.Test;
import java.sql.Connection;
import java.sql.Statement;
@ -24,20 +39,21 @@ public class SqliteGeneratorTest {
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setSourceDir(System.getProperty("user.dir") + "/src/test/java");
//配置生成文件目录与根包
globalConfig.getPackageConfig()
.setSourceDir(System.getProperty("user.dir") + "/src/test/java")
.setBasePackage("com.test");
//设置只生成哪些表
globalConfig.addGenerateTable("person");
globalConfig.getStrategyConfig()
.setGenerateTable("person");
//设置 entity 的包名
globalConfig.setEntityPackage("com.test.entity");
globalConfig.enableEntity()
.setWithLombok(true);
//是否生成 mapper 默认为 false
globalConfig.setMapperGenerateEnable(true);
globalConfig.setEntityWithLombok(true);
//设置 mapper 类的包名
globalConfig.setMapperPackage("com.test.mapper");
//设置生成 mapper
globalConfig.enableMapper();
Generator generator = new Generator(dataSource, globalConfig, IDialect.SQLITE);

View File

@ -24,7 +24,7 @@ import java.util.List;
/**
* 数据库 聚合函数例如 count(id) max(account.age) 等等
*/
public class FunctionQueryColumn extends QueryColumn {
public class FunctionQueryColumn extends QueryColumn implements HasParamsColumn {
protected String fnName;
protected QueryColumn column;
@ -58,6 +58,14 @@ public class FunctionQueryColumn extends QueryColumn {
this.column = column;
}
@Override
public Object[] getParamValues() {
if (column instanceof HasParamsColumn){
return ((HasParamsColumn) column).getParamValues();
}
return WrapperUtil.NULL_PARA_ARRAY;
}
@Override
public String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
String sql = column.toSelectSql(queryTables, dialect);
@ -78,4 +86,6 @@ public class FunctionQueryColumn extends QueryColumn {
", column=" + column +
'}';
}
}