重构代码生成器模块

This commit is contained in:
开源海哥 2023-05-06 12:18:42 +08:00
parent 090faf8cdf
commit 2a47e0b526
11 changed files with 283 additions and 104 deletions

View File

@ -18,15 +18,16 @@ package com.mybatisflex.codegen;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.dialect.IDialect;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.codegen.template.ITemplate;
import com.mybatisflex.codegen.generator.GeneratorFactory;
import com.mybatisflex.codegen.generator.IGenerator;
import javax.sql.DataSource;
import java.io.File;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Generator {
@ -59,52 +60,12 @@ public class Generator {
List<Table> tables = buildTables();
ITemplate templateEngine = globalConfig.getTemplateEngine();
for (Table table : tables) {
String entityPackagePath = globalConfig.getEntityPackage().replace(".", "/");
File entityJavaFile = new File(globalConfig.getSourceDir(), entityPackagePath + "/" +
table.buildEntityClassName() + ".java");
if (!entityJavaFile.getParentFile().exists()) {
if (!entityJavaFile.getParentFile().mkdirs()) {
throw new IllegalStateException("Can not mkdirs by dir: " + entityJavaFile.getParentFile());
}
}
templateEngine.generateEntity(globalConfig, table, entityJavaFile);
String tableDefPackagePath = globalConfig.getTableDefPackage().replace(".", "/");
File tableDefJavaFile = new File(globalConfig.getSourceDir(), tableDefPackagePath + "/" +
table.buildTableDefClassName() + ".java");
if (!tableDefJavaFile.getParentFile().exists()) {
if (!tableDefJavaFile.getParentFile().mkdirs()) {
throw new IllegalStateException("Can not mkdirs by dir: " + tableDefJavaFile.getParentFile());
}
}
templateEngine.generateTableDef(globalConfig, table, tableDefJavaFile);
if (globalConfig.isMapperGenerateEnable()) {
String mapperPackagePath = globalConfig.getMapperPackage().replace(".", "/");
File mapperJavaFile = new File(globalConfig.getSourceDir(), mapperPackagePath + "/" +
table.buildMapperClassName() + ".java");
if (!mapperJavaFile.getParentFile().exists()) {
if (!mapperJavaFile.getParentFile().mkdirs()) {
throw new IllegalStateException("Can not mkdirs by dir: " + mapperJavaFile.getParentFile());
}
}
if (mapperJavaFile.exists() && !globalConfig.isMapperOverwriteEnable()) {
//ignore
} else {
templateEngine.generateMapper(globalConfig, table, mapperJavaFile);
}
Collection<IGenerator> generators = GeneratorFactory.getGenerators();
for (IGenerator generator : generators) {
generator.generate(table, globalConfig);
}
}
} catch (Exception e) {
e.printStackTrace();
}

View File

@ -49,6 +49,8 @@ public class GlobalConfig {
//entity 是否使用 Lombok
private boolean entityWithLombok = false;
private boolean tableDefGenerateEnable = false;
//tableDef 的包名
private String tableDefPackage;
@ -112,7 +114,7 @@ public class GlobalConfig {
}
public void setSourceDir(String sourceDir) {
this.sourceDir = sourceDir;
this.sourceDir = StringUtil.trimOrNull(sourceDir);
}
public String getEntityPackage() {
@ -123,7 +125,7 @@ public class GlobalConfig {
}
public void setEntityPackage(String entityPackage) {
this.entityPackage = entityPackage.trim();
this.entityPackage = StringUtil.trimOrNull(entityPackage);
}
public String getEntityClassPrefix() {
@ -134,7 +136,7 @@ public class GlobalConfig {
}
public void setEntityClassPrefix(String entityClassPrefix) {
this.entityClassPrefix = entityClassPrefix;
this.entityClassPrefix = StringUtil.trimOrNull(entityClassPrefix);
}
public String getEntityClassSuffix() {
@ -145,7 +147,7 @@ public class GlobalConfig {
}
public void setEntityClassSuffix(String entityClassSuffix) {
this.entityClassSuffix = entityClassSuffix;
this.entityClassSuffix = StringUtil.trimOrNull(entityClassSuffix);
}
public Class<?> getEntitySupperClass() {
@ -172,12 +174,23 @@ public class GlobalConfig {
this.entityWithLombok = entityWithLombok;
}
public boolean isTableDefGenerateEnable() {
return tableDefGenerateEnable;
}
public void setTableDefGenerateEnable(boolean tableDefGenerateEnable) {
this.tableDefGenerateEnable = tableDefGenerateEnable;
}
public String getTableDefPackage() {
if (StringUtil.isBlank(tableDefPackage) && StringUtil.isNotBlank(entityPackage)) {
return entityPackage + ".tables";
}
return tableDefPackage;
}
public void setTableDefPackage(String tableDefPackage) {
this.tableDefPackage = tableDefPackage;
this.tableDefPackage = StringUtil.trimOrNull(tableDefPackage);
}
public String getTableDefClassPrefix() {
@ -185,7 +198,7 @@ public class GlobalConfig {
}
public void setTableDefClassPrefix(String tableDefClassPrefix) {
this.tableDefClassPrefix = tableDefClassPrefix;
this.tableDefClassPrefix = StringUtil.trimOrNull(tableDefClassPrefix);
}
public String getTableDefClassSuffix() {
@ -193,7 +206,7 @@ public class GlobalConfig {
}
public void setTableDefClassSuffix(String tableDefClassSuffix) {
this.tableDefClassSuffix = tableDefClassSuffix;
this.tableDefClassSuffix = StringUtil.trimOrNull(tableDefClassSuffix);
}
public boolean isMapperGenerateEnable() {
@ -220,7 +233,7 @@ public class GlobalConfig {
}
public void setMapperClassPrefix(String mapperClassPrefix) {
this.mapperClassPrefix = mapperClassPrefix;
this.mapperClassPrefix = StringUtil.trimOrNull(mapperClassPrefix);
}
public String getMapperClassSuffix() {
@ -228,7 +241,7 @@ public class GlobalConfig {
}
public void setMapperClassSuffix(String mapperClassSuffix) {
this.mapperClassSuffix = mapperClassSuffix;
this.mapperClassSuffix = StringUtil.trimOrNull(mapperClassSuffix);
}
public String getMapperPackage() {
@ -239,7 +252,7 @@ public class GlobalConfig {
}
public void setMapperPackage(String mapperPackage) {
this.mapperPackage = mapperPackage;
this.mapperPackage = StringUtil.trimOrNull(mapperPackage);
}
public Class<?> getMapperSupperClass() {
@ -255,7 +268,7 @@ public class GlobalConfig {
}
public void setTablePrefix(String tablePrefix) {
this.tablePrefix = tablePrefix;
this.tablePrefix = StringUtil.trimOrNull(tablePrefix);
}
public String getLogicDeleteColumn() {
@ -263,7 +276,7 @@ public class GlobalConfig {
}
public void setLogicDeleteColumn(String logicDeleteColumn) {
this.logicDeleteColumn = logicDeleteColumn;
this.logicDeleteColumn = StringUtil.trimOrNull(logicDeleteColumn);
}
public String getVersionColumn() {
@ -271,7 +284,7 @@ public class GlobalConfig {
}
public void setVersionColumn(String versionColumn) {
this.versionColumn = versionColumn;
this.versionColumn = StringUtil.trimOrNull(versionColumn);
}
public Map<String, TableConfig> getTableConfigMap() {

View File

@ -0,0 +1,43 @@
/**
* 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;
import com.mybatisflex.codegen.generator.impl.EntityGenerator;
import com.mybatisflex.codegen.generator.impl.MapperGenerator;
import com.mybatisflex.codegen.generator.impl.TableDefGenerator;
import java.util.*;
public class GeneratorFactory {
private static Map<String, IGenerator> generators = new HashMap<>();
static {
registerGenerator("entity", new EntityGenerator());
registerGenerator("mapper", new MapperGenerator());
registerGenerator("tableDef", new TableDefGenerator());
}
public static Collection<IGenerator> getGenerators() {
return generators.values();
}
public static void registerGenerator(String name, IGenerator generator) {
generators.put(name, generator);
}
}

View File

@ -0,0 +1,23 @@
/**
* 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;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.entity.Table;
public interface IGenerator {
void generate(Table table, GlobalConfig globalConfig);
}

View File

@ -0,0 +1,50 @@
/**
* 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.entity.Table;
import com.mybatisflex.codegen.generator.IGenerator;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class EntityGenerator implements IGenerator {
private String templatePath = "/templates/enjoy/entity.tpl";
public EntityGenerator() {
}
public EntityGenerator(String templatePath) {
this.templatePath = templatePath;
}
@Override
public void generate(Table table, GlobalConfig globalConfig) {
String entityPackagePath = globalConfig.getEntityPackage().replace(".", "/");
File entityJavaFile = new File(globalConfig.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);
}
}

View File

@ -0,0 +1,60 @@
/**
* 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.entity.Table;
import com.mybatisflex.codegen.generator.IGenerator;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class MapperGenerator implements IGenerator {
private String templatePath = "/templates/enjoy/mapper.tpl";
public MapperGenerator() {
}
public MapperGenerator(String templatePath) {
this.templatePath = templatePath;
}
@Override
public void generate(Table table, GlobalConfig globalConfig) {
if (!globalConfig.isMapperGenerateEnable()) {
return;
}
String mapperPackagePath = globalConfig.getMapperPackage().replace(".", "/");
File mapperJavaFile = new File(globalConfig.getSourceDir(), mapperPackagePath + "/" +
table.buildMapperClassName() + ".java");
if (mapperJavaFile.exists() && !globalConfig.isMapperOverwriteEnable()) {
return;//ignore
}
Map<String, Object> params = new HashMap<>();
params.put("table", table);
params.put("globalConfig", globalConfig);
globalConfig.getTemplateEngine().generate(params, templatePath, mapperJavaFile);
}
}

View File

@ -0,0 +1,55 @@
/**
* 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.entity.Table;
import com.mybatisflex.codegen.generator.IGenerator;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class TableDefGenerator implements IGenerator {
private String templatePath = "/templates/enjoy/tableDef.tpl";
public TableDefGenerator() {
}
public TableDefGenerator(String templatePath) {
this.templatePath = templatePath;
}
@Override
public void generate(Table table, GlobalConfig globalConfig) {
if (!globalConfig.isTableDefGenerateEnable()){
return;
}
String tableDefPackagePath = globalConfig.getTableDefPackage().replace(".", "/");
File tableDefJavaFile = new File(globalConfig.getSourceDir(), tableDefPackagePath + "/" +
table.buildTableDefClassName() + ".java");
Map<String, Object> params = new HashMap<>();
params.put("table", table);
params.put("globalConfig", globalConfig);
globalConfig.getTemplateEngine().generate(params, templatePath, tableDefJavaFile);
}
}

View File

@ -16,13 +16,10 @@
package com.mybatisflex.codegen.template;
import com.jfinal.template.Engine;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.core.util.StringUtil;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
public class EnjoyTemplate implements ITemplate {
@ -37,39 +34,15 @@ public class EnjoyTemplate implements ITemplate {
}
@Override
public void generateEntity(GlobalConfig globalConfig, Table table, File entityJavaFile) throws Exception {
Map<String, Object> params = new HashMap<>();
params.put("globalConfig", globalConfig);
params.put("table", table);
public void generate(Map<String, Object> params, String templateFilePath, File generateFile) {
if (!generateFile.getParentFile().exists() && !generateFile.getParentFile().mkdirs()){
throw new IllegalStateException("Can not mkdirs by dir: " + generateFile.getParentFile());
}
FileOutputStream fileOutputStream = new FileOutputStream(entityJavaFile);
engine.getTemplate("/templates/enjoy/entity.tpl").render(params, fileOutputStream);
System.out.println("Entity has been generated: " + entityJavaFile.getAbsolutePath());
}
@Override
public void generateTableDef(GlobalConfig globalConfig, Table table, File entityJavaFile) throws Exception {
Map<String, Object> params = new HashMap<>();
params.put("globalConfig", globalConfig);
params.put("table", table);
FileOutputStream fileOutputStream = new FileOutputStream(entityJavaFile);
engine.getTemplate("/templates/enjoy/tableDef.tpl").render(params, fileOutputStream);
System.out.println("TableDef has been generated: " + entityJavaFile.getAbsolutePath());
}
@Override
public void generateMapper(GlobalConfig globalConfig, Table table, File mapperJavaFile) throws Exception {
Map<String, Object> params = new HashMap<>();
params.put("globalConfig", globalConfig);
params.put("table", table);
FileOutputStream fileOutputStream = new FileOutputStream(mapperJavaFile);
engine.getTemplate("/templates/enjoy/mapper.tpl").render(params, fileOutputStream);
System.out.println("Mapper has been generated: " + mapperJavaFile.getAbsolutePath());
try(FileOutputStream fileOutputStream = new FileOutputStream(generateFile)) {
engine.getTemplate(templateFilePath).render(params, fileOutputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -15,16 +15,11 @@
*/
package com.mybatisflex.codegen.template;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.entity.Table;
import java.io.File;
import java.util.Map;
public interface ITemplate {
void generateEntity(GlobalConfig globalConfig, Table table, File entityJavaFile) throws Exception ;
void generate(Map<String, Object> params, String templateFilePath, File generateFile);
void generateTableDef(GlobalConfig globalConfig, Table table, File tableDefJavaFile) throws Exception ;
void generateMapper(GlobalConfig globalConfig, Table table, File mapperJavaFile) throws Exception ;
}

View File

@ -20,16 +20,16 @@ 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;
public class GeneratorTest {
@Test
// @Test
public void testGenerator() {
//配置数据源
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/hh-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&allowMultiQueries=true");
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");
@ -52,6 +52,7 @@ public class GeneratorTest {
globalConfig.setEntityClassSuffix("Entity");
//设置 entity 的包名
globalConfig.setTableDefGenerateEnable(true);
globalConfig.setTableDefPackage("com.test.entity.tables");
globalConfig.setTableDefClassPrefix("My");
globalConfig.setTableDefClassSuffix("TableDef");

View File

@ -190,6 +190,11 @@ public class StringUtil {
}
public static String trimOrNull(String string) {
return string != null ? string.trim() : null;
}
/**
* 正则匹配
*