mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 09:38:26 +08:00
重构代码生成器模块
This commit is contained in:
parent
090faf8cdf
commit
2a47e0b526
@ -18,15 +18,16 @@ package com.mybatisflex.codegen;
|
|||||||
import com.mybatisflex.codegen.config.GlobalConfig;
|
import com.mybatisflex.codegen.config.GlobalConfig;
|
||||||
import com.mybatisflex.codegen.dialect.IDialect;
|
import com.mybatisflex.codegen.dialect.IDialect;
|
||||||
import com.mybatisflex.codegen.entity.Table;
|
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 javax.sql.DataSource;
|
||||||
import java.io.File;
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DatabaseMetaData;
|
import java.sql.DatabaseMetaData;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Generator {
|
public class Generator {
|
||||||
@ -59,52 +60,12 @@ public class Generator {
|
|||||||
|
|
||||||
List<Table> tables = buildTables();
|
List<Table> tables = buildTables();
|
||||||
|
|
||||||
ITemplate templateEngine = globalConfig.getTemplateEngine();
|
|
||||||
for (Table table : tables) {
|
for (Table table : tables) {
|
||||||
|
Collection<IGenerator> generators = GeneratorFactory.getGenerators();
|
||||||
String entityPackagePath = globalConfig.getEntityPackage().replace(".", "/");
|
for (IGenerator generator : generators) {
|
||||||
File entityJavaFile = new File(globalConfig.getSourceDir(), entityPackagePath + "/" +
|
generator.generate(table, globalConfig);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -49,6 +49,8 @@ public class GlobalConfig {
|
|||||||
//entity 是否使用 Lombok
|
//entity 是否使用 Lombok
|
||||||
private boolean entityWithLombok = false;
|
private boolean entityWithLombok = false;
|
||||||
|
|
||||||
|
private boolean tableDefGenerateEnable = false;
|
||||||
|
|
||||||
//tableDef 的包名
|
//tableDef 的包名
|
||||||
private String tableDefPackage;
|
private String tableDefPackage;
|
||||||
|
|
||||||
@ -112,7 +114,7 @@ public class GlobalConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setSourceDir(String sourceDir) {
|
public void setSourceDir(String sourceDir) {
|
||||||
this.sourceDir = sourceDir;
|
this.sourceDir = StringUtil.trimOrNull(sourceDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getEntityPackage() {
|
public String getEntityPackage() {
|
||||||
@ -123,7 +125,7 @@ public class GlobalConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setEntityPackage(String entityPackage) {
|
public void setEntityPackage(String entityPackage) {
|
||||||
this.entityPackage = entityPackage.trim();
|
this.entityPackage = StringUtil.trimOrNull(entityPackage);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getEntityClassPrefix() {
|
public String getEntityClassPrefix() {
|
||||||
@ -134,7 +136,7 @@ public class GlobalConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setEntityClassPrefix(String entityClassPrefix) {
|
public void setEntityClassPrefix(String entityClassPrefix) {
|
||||||
this.entityClassPrefix = entityClassPrefix;
|
this.entityClassPrefix = StringUtil.trimOrNull(entityClassPrefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getEntityClassSuffix() {
|
public String getEntityClassSuffix() {
|
||||||
@ -145,7 +147,7 @@ public class GlobalConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setEntityClassSuffix(String entityClassSuffix) {
|
public void setEntityClassSuffix(String entityClassSuffix) {
|
||||||
this.entityClassSuffix = entityClassSuffix;
|
this.entityClassSuffix = StringUtil.trimOrNull(entityClassSuffix);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Class<?> getEntitySupperClass() {
|
public Class<?> getEntitySupperClass() {
|
||||||
@ -172,12 +174,23 @@ public class GlobalConfig {
|
|||||||
this.entityWithLombok = entityWithLombok;
|
this.entityWithLombok = entityWithLombok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isTableDefGenerateEnable() {
|
||||||
|
return tableDefGenerateEnable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTableDefGenerateEnable(boolean tableDefGenerateEnable) {
|
||||||
|
this.tableDefGenerateEnable = tableDefGenerateEnable;
|
||||||
|
}
|
||||||
|
|
||||||
public String getTableDefPackage() {
|
public String getTableDefPackage() {
|
||||||
|
if (StringUtil.isBlank(tableDefPackage) && StringUtil.isNotBlank(entityPackage)) {
|
||||||
|
return entityPackage + ".tables";
|
||||||
|
}
|
||||||
return tableDefPackage;
|
return tableDefPackage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTableDefPackage(String tableDefPackage) {
|
public void setTableDefPackage(String tableDefPackage) {
|
||||||
this.tableDefPackage = tableDefPackage;
|
this.tableDefPackage = StringUtil.trimOrNull(tableDefPackage);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTableDefClassPrefix() {
|
public String getTableDefClassPrefix() {
|
||||||
@ -185,7 +198,7 @@ public class GlobalConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setTableDefClassPrefix(String tableDefClassPrefix) {
|
public void setTableDefClassPrefix(String tableDefClassPrefix) {
|
||||||
this.tableDefClassPrefix = tableDefClassPrefix;
|
this.tableDefClassPrefix = StringUtil.trimOrNull(tableDefClassPrefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTableDefClassSuffix() {
|
public String getTableDefClassSuffix() {
|
||||||
@ -193,7 +206,7 @@ public class GlobalConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setTableDefClassSuffix(String tableDefClassSuffix) {
|
public void setTableDefClassSuffix(String tableDefClassSuffix) {
|
||||||
this.tableDefClassSuffix = tableDefClassSuffix;
|
this.tableDefClassSuffix = StringUtil.trimOrNull(tableDefClassSuffix);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isMapperGenerateEnable() {
|
public boolean isMapperGenerateEnable() {
|
||||||
@ -220,7 +233,7 @@ public class GlobalConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setMapperClassPrefix(String mapperClassPrefix) {
|
public void setMapperClassPrefix(String mapperClassPrefix) {
|
||||||
this.mapperClassPrefix = mapperClassPrefix;
|
this.mapperClassPrefix = StringUtil.trimOrNull(mapperClassPrefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMapperClassSuffix() {
|
public String getMapperClassSuffix() {
|
||||||
@ -228,7 +241,7 @@ public class GlobalConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setMapperClassSuffix(String mapperClassSuffix) {
|
public void setMapperClassSuffix(String mapperClassSuffix) {
|
||||||
this.mapperClassSuffix = mapperClassSuffix;
|
this.mapperClassSuffix = StringUtil.trimOrNull(mapperClassSuffix);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMapperPackage() {
|
public String getMapperPackage() {
|
||||||
@ -239,7 +252,7 @@ public class GlobalConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setMapperPackage(String mapperPackage) {
|
public void setMapperPackage(String mapperPackage) {
|
||||||
this.mapperPackage = mapperPackage;
|
this.mapperPackage = StringUtil.trimOrNull(mapperPackage);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Class<?> getMapperSupperClass() {
|
public Class<?> getMapperSupperClass() {
|
||||||
@ -255,7 +268,7 @@ public class GlobalConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setTablePrefix(String tablePrefix) {
|
public void setTablePrefix(String tablePrefix) {
|
||||||
this.tablePrefix = tablePrefix;
|
this.tablePrefix = StringUtil.trimOrNull(tablePrefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLogicDeleteColumn() {
|
public String getLogicDeleteColumn() {
|
||||||
@ -263,7 +276,7 @@ public class GlobalConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setLogicDeleteColumn(String logicDeleteColumn) {
|
public void setLogicDeleteColumn(String logicDeleteColumn) {
|
||||||
this.logicDeleteColumn = logicDeleteColumn;
|
this.logicDeleteColumn = StringUtil.trimOrNull(logicDeleteColumn);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getVersionColumn() {
|
public String getVersionColumn() {
|
||||||
@ -271,7 +284,7 @@ public class GlobalConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setVersionColumn(String versionColumn) {
|
public void setVersionColumn(String versionColumn) {
|
||||||
this.versionColumn = versionColumn;
|
this.versionColumn = StringUtil.trimOrNull(versionColumn);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, TableConfig> getTableConfigMap() {
|
public Map<String, TableConfig> getTableConfigMap() {
|
||||||
|
|||||||
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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);
|
||||||
|
}
|
||||||
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -16,13 +16,10 @@
|
|||||||
package com.mybatisflex.codegen.template;
|
package com.mybatisflex.codegen.template;
|
||||||
|
|
||||||
import com.jfinal.template.Engine;
|
import com.jfinal.template.Engine;
|
||||||
import com.mybatisflex.codegen.config.GlobalConfig;
|
|
||||||
import com.mybatisflex.codegen.entity.Table;
|
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class EnjoyTemplate implements ITemplate {
|
public class EnjoyTemplate implements ITemplate {
|
||||||
@ -37,39 +34,15 @@ public class EnjoyTemplate implements ITemplate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void generateEntity(GlobalConfig globalConfig, Table table, File entityJavaFile) throws Exception {
|
public void generate(Map<String, Object> params, String templateFilePath, File generateFile) {
|
||||||
Map<String, Object> params = new HashMap<>();
|
if (!generateFile.getParentFile().exists() && !generateFile.getParentFile().mkdirs()){
|
||||||
params.put("globalConfig", globalConfig);
|
throw new IllegalStateException("Can not mkdirs by dir: " + generateFile.getParentFile());
|
||||||
params.put("table", table);
|
}
|
||||||
|
|
||||||
|
try(FileOutputStream fileOutputStream = new FileOutputStream(generateFile)) {
|
||||||
FileOutputStream fileOutputStream = new FileOutputStream(entityJavaFile);
|
engine.getTemplate(templateFilePath).render(params, fileOutputStream);
|
||||||
engine.getTemplate("/templates/enjoy/entity.tpl").render(params, fileOutputStream);
|
} catch (Exception e) {
|
||||||
System.out.println("Entity has been generated: " + entityJavaFile.getAbsolutePath());
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
@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());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,16 +15,11 @@
|
|||||||
*/
|
*/
|
||||||
package com.mybatisflex.codegen.template;
|
package com.mybatisflex.codegen.template;
|
||||||
|
|
||||||
import com.mybatisflex.codegen.config.GlobalConfig;
|
|
||||||
import com.mybatisflex.codegen.entity.Table;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public interface ITemplate {
|
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 ;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,16 +20,16 @@ import com.mybatisflex.codegen.config.ColumnConfig;
|
|||||||
import com.mybatisflex.codegen.config.GlobalConfig;
|
import com.mybatisflex.codegen.config.GlobalConfig;
|
||||||
import com.mybatisflex.codegen.config.TableConfig;
|
import com.mybatisflex.codegen.config.TableConfig;
|
||||||
import com.zaxxer.hikari.HikariDataSource;
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class GeneratorTest {
|
public class GeneratorTest {
|
||||||
|
|
||||||
|
|
||||||
@Test
|
// @Test
|
||||||
public void testGenerator() {
|
public void testGenerator() {
|
||||||
//配置数据源
|
//配置数据源
|
||||||
HikariDataSource dataSource = new HikariDataSource();
|
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.setUsername("root");
|
||||||
dataSource.setPassword("123456");
|
dataSource.setPassword("123456");
|
||||||
|
|
||||||
@ -52,6 +52,7 @@ public class GeneratorTest {
|
|||||||
globalConfig.setEntityClassSuffix("Entity");
|
globalConfig.setEntityClassSuffix("Entity");
|
||||||
|
|
||||||
//设置 entity 的包名
|
//设置 entity 的包名
|
||||||
|
globalConfig.setTableDefGenerateEnable(true);
|
||||||
globalConfig.setTableDefPackage("com.test.entity.tables");
|
globalConfig.setTableDefPackage("com.test.entity.tables");
|
||||||
globalConfig.setTableDefClassPrefix("My");
|
globalConfig.setTableDefClassPrefix("My");
|
||||||
globalConfig.setTableDefClassSuffix("TableDef");
|
globalConfig.setTableDefClassSuffix("TableDef");
|
||||||
|
|||||||
@ -190,6 +190,11 @@ public class StringUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static String trimOrNull(String string) {
|
||||||
|
return string != null ? string.trim() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 正则匹配
|
* 正则匹配
|
||||||
*
|
*
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user