Skip to content

Commit

Permalink
重构文件输出路径设置逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
qmdx committed May 31, 2021
1 parent 20bd456 commit 494bec0
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void start() {
/**
* 全局配置 Builder
*/
public IConfigBuilder<GlobalConfig> globalConfigBuilder() {
public GlobalConfig.Builder globalConfigBuilder() {
String outputDir = new File(System.getProperty("user.dir")) + File.separator + "build" + File.separator + "code";
System.out.println("\n输出文件目录:" + outputDir);
return new GlobalConfig.Builder().fileOverride().enableSwagger().outputDir(outputDir)
Expand All @@ -89,14 +89,14 @@ public IConfigBuilder<GlobalConfig> globalConfigBuilder() {
/**
* 生成文件包名配置 Builder
*/
public IConfigBuilder<PackageConfig> packageConfigBuilder() {
public PackageConfig.Builder packageConfigBuilder() {
return new PackageConfig.Builder().parent(scannerNext("\n请输入项目包名:")).moduleName(scannerNext("\n请输入项目模块名:"));
}

/**
* 自定义模板配置 Builder
*/
public IConfigBuilder<TemplateConfig> templateConfigBuilder() {
public TemplateConfig.Builder templateConfigBuilder() {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ public interface ConstVal {
String CONTROLLER = "Controller";
String PARENT = "Parent";

String ENTITY_PATH = "entity_path";
String SERVICE_PATH = "service_path";
String SERVICE_IMPL_PATH = "service_impl_path";
String MAPPER_PATH = "mapper_path";
String XML_PATH = "xml_path";
String CONTROLLER_PATH = "controller_path";

String JAVA_TMPDIR = "java.io.tmpdir";
String UTF8 = StandardCharsets.UTF_8.name();
String UNDERLINE = "_";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2011-2021, baomidou ([email protected]).
* <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>
* https://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.baomidou.mybatisplus.generator.config;

/**
* 输出文件类型
*
* @author hubin
* @since 2021-06-01
*/
public enum OutputFile {
entity,
service,
serviceImpl,
mapper,
mapperXml,
controller;

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class PackageConfig {
/**
* 路径配置信息
*/
private Map<String, String> pathInfo;
private Map<OutputFile, String> pathInfo;

/**
* 包配置信息
Expand Down Expand Up @@ -104,7 +104,6 @@ public String joinPackage(String subPackage) {
* @return 包配置信息
* @since 3.5.0
*/
//TODO xml需要自定义,可能不需要包定义
@NotNull
public Map<String, String> getPackageInfo() {
if (packageInfo.isEmpty()) {
Expand Down Expand Up @@ -163,14 +162,14 @@ public String getController() {
return controller;
}

public Map<String, String> getPathInfo() {
public Map<OutputFile, String> getPathInfo() {
return pathInfo;
}

/**
* 构建者
*
* @author nieqiurong 2020/10/13.
* @author nieqiurong
* @since 3.5.0
*/
public static class Builder implements IConfigBuilder<PackageConfig> {
Expand Down Expand Up @@ -281,7 +280,7 @@ public Builder controller(@NotNull String controller) {
* @param pathInfo 路径配置信息
* @return this
*/
public Builder pathInfo(@NotNull Map<String, String> pathInfo) {
public Builder pathInfo(@NotNull Map<OutputFile, String> pathInfo) {
this.packageConfig.pathInfo = pathInfo;
return this;
}
Expand Down Expand Up @@ -310,7 +309,6 @@ public String joinPackage(@NotNull String subPackage) {
*/
@Override
public PackageConfig build() {
//TODO 后面考虑把那些entity包名挂到Entity上去
return this.packageConfig;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
* @since 2016/8/30
*/
public class StrategyConfig {

/**
* 是否大写命名
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class ConfigBuilder {
/**
* 路径配置信息
*/
private final Map<String, String> pathInfo = new HashMap<>();
private final Map<OutputFile, String> pathInfo = new HashMap<>();
/**
* 策略配置
*/
Expand Down Expand Up @@ -136,7 +136,7 @@ public List<TableInfo> getTableInfoList() {
}

@NotNull
public Map<String, String> getPathInfo() {
public Map<OutputFile, String> getPathInfo() {
return pathInfo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,48 @@
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.*;

import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
* 路径信息处理
*
* @author nieqiurong 2020/10/6.
* @author nieqiurong hubin
* @since 2020-10-06
* @since 3.5.0
*/
class PathInfoHandler {

private final Map<String, String> pathInfo = new HashMap<>();

private final Map<OutputFile, String> pathInfo = new HashMap<>();
private final String outputDir;

private final PackageConfig packageConfig;

PathInfoHandler(GlobalConfig globalConfig, TemplateConfig templateConfig, PackageConfig packageConfig) {
this.outputDir = globalConfig.getOutputDir();
this.packageConfig = packageConfig;
Map<String, String> pathInfo = packageConfig.getPathInfo();
Map<OutputFile, String> pathInfo = packageConfig.getPathInfo();
if (CollectionUtils.isNotEmpty(pathInfo)) {
this.pathInfo.putAll(pathInfo);
} else {
putPathInfo(templateConfig.getEntity(globalConfig.isKotlin()), ConstVal.ENTITY_PATH, ConstVal.ENTITY);
putPathInfo(templateConfig.getMapper(), ConstVal.MAPPER_PATH, ConstVal.MAPPER);
putPathInfo(templateConfig.getXml(), ConstVal.XML_PATH, ConstVal.MAPPER);
putPathInfo(templateConfig.getService(), ConstVal.SERVICE_PATH, ConstVal.SERVICE);
putPathInfo(templateConfig.getServiceImpl(), ConstVal.SERVICE_IMPL_PATH, ConstVal.SERVICE_IMPL);
putPathInfo(templateConfig.getController(), ConstVal.CONTROLLER_PATH, ConstVal.CONTROLLER);
}
// 设置默认输出路径
putPathInfo(templateConfig.getEntity(globalConfig.isKotlin()), OutputFile.entity, ConstVal.ENTITY);
putPathInfo(templateConfig.getMapper(), OutputFile.mapper, ConstVal.MAPPER);
putPathInfo(templateConfig.getXml(), OutputFile.mapperXml, ConstVal.MAPPER);
putPathInfo(templateConfig.getService(), OutputFile.service, ConstVal.SERVICE);
putPathInfo(templateConfig.getServiceImpl(), OutputFile.serviceImpl, ConstVal.SERVICE_IMPL);
putPathInfo(templateConfig.getController(), OutputFile.controller, ConstVal.CONTROLLER);
}

public Map<String, String> getPathInfo() {
return Collections.unmodifiableMap(this.pathInfo);
public Map<OutputFile, String> getPathInfo() {
return this.pathInfo;
}

private void putPathInfo(String template, String path, String module) {
if (StringUtils.isNotBlank(template)) pathInfo.put(path, joinPath(outputDir, packageConfig.getPackageInfo(module)));
private void putPathInfo(String template, OutputFile outputFile, String module) {
if (StringUtils.isNotBlank(template)) {
pathInfo.putIfAbsent(outputFile, joinPath(outputDir, packageConfig.getPackageInfo(module)));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
Expand Down Expand Up @@ -68,7 +69,7 @@ public abstract class AbstractTemplateEngine {
*/
protected void outputEntity(@NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
String entityName = tableInfo.getEntityName();
String entityPath = getPathInfo(ConstVal.ENTITY_PATH);
String entityPath = getPathInfo(OutputFile.entity);
if (StringUtils.isNotBlank(entityName) && StringUtils.isNotBlank(entityPath)) {
getTemplateFilePath(template -> template.getEntity(getConfigBuilder().getGlobalConfig().isKotlin())).ifPresent((entity) -> {
String entityFile = String.format((entityPath + File.separator + "%s" + suffixJavaOrKt()), entityName);
Expand All @@ -87,15 +88,15 @@ protected void outputEntity(@NotNull TableInfo tableInfo, @NotNull Map<String, O
protected void outputMapper(@NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
// MpMapper.java
String entityName = tableInfo.getEntityName();
String mapperPath = getPathInfo(ConstVal.MAPPER_PATH);
String mapperPath = getPathInfo(OutputFile.mapper);
if (StringUtils.isNotBlank(tableInfo.getMapperName()) && StringUtils.isNotBlank(mapperPath)) {
getTemplateFilePath(TemplateConfig::getMapper).ifPresent(mapper -> {
String mapperFile = String.format((mapperPath + File.separator + tableInfo.getMapperName() + suffixJavaOrKt()), entityName);
outputFile(new File(mapperFile), objectMap, mapper);
});
}
// MpMapper.xml
String xmlPath = getPathInfo(ConstVal.XML_PATH);
String xmlPath = getPathInfo(OutputFile.mapperXml);
if (StringUtils.isNotBlank(tableInfo.getXmlName()) && StringUtils.isNotBlank(xmlPath)) {
getTemplateFilePath(TemplateConfig::getXml).ifPresent(xml -> {
String xmlFile = String.format((xmlPath + File.separator + tableInfo.getXmlName() + ConstVal.XML_SUFFIX), entityName);
Expand All @@ -114,15 +115,15 @@ protected void outputMapper(@NotNull TableInfo tableInfo, @NotNull Map<String, O
protected void outputService(@NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
// IMpService.java
String entityName = tableInfo.getEntityName();
String servicePath = getPathInfo(ConstVal.SERVICE_PATH);
String servicePath = getPathInfo(OutputFile.service);
if (StringUtils.isNotBlank(tableInfo.getServiceName()) && StringUtils.isNotBlank(servicePath)) {
getTemplateFilePath(TemplateConfig::getService).ifPresent(service -> {
String serviceFile = String.format((servicePath + File.separator + tableInfo.getServiceName() + suffixJavaOrKt()), entityName);
outputFile(new File(serviceFile), objectMap, service);
});
}
// MpServiceImpl.java
String serviceImplPath = getPathInfo(ConstVal.SERVICE_IMPL_PATH);
String serviceImplPath = getPathInfo(OutputFile.serviceImpl);
if (StringUtils.isNotBlank(tableInfo.getServiceImplName()) && StringUtils.isNotBlank(serviceImplPath)) {
getTemplateFilePath(TemplateConfig::getServiceImpl).ifPresent(serviceImpl -> {
String implFile = String.format((serviceImplPath + File.separator + tableInfo.getServiceImplName() + suffixJavaOrKt()), entityName);
Expand All @@ -140,7 +141,7 @@ protected void outputService(@NotNull TableInfo tableInfo, @NotNull Map<String,
*/
protected void outputController(@NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
// MpController.java
String controllerPath = getPathInfo(ConstVal.CONTROLLER_PATH);
String controllerPath = getPathInfo(OutputFile.controller);
if (StringUtils.isNotBlank(tableInfo.getControllerName()) && StringUtils.isNotBlank(controllerPath)) {
getTemplateFilePath(TemplateConfig::getController).ifPresent(controller -> {
String entityName = tableInfo.getEntityName();
Expand Down Expand Up @@ -194,13 +195,12 @@ protected Optional<String> getTemplateFilePath(@NotNull Function<TemplateConfig,
/**
* 获取路径信息
*
* @param key key {@link ConstVal}_xxxPath
* @param outputFile 输出文件
* @return 路径信息
*/
@Nullable
protected String getPathInfo(@NotNull String key) {
Map<String, String> pathInfo = getConfigBuilder().getPathInfo();
return pathInfo.get(key);
protected String getPathInfo(@NotNull OutputFile outputFile) {
return getConfigBuilder().getPathInfo().get(outputFile);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ private void buildAssert(PackageConfig packageConfig){
Assertions.assertEquals(packageConfig.getService(),"iservice");
Assertions.assertEquals(packageConfig.getServiceImpl(),"serviceIm");
Assertions.assertEquals(1,packageConfig.getPathInfo().size());
Assertions.assertTrue(packageConfig.getPathInfo().containsKey("aaaa"));
Assertions.assertTrue(packageConfig.getPathInfo().containsKey(OutputFile.controller));
}

@Test
void buildTest(){
buildAssert(GeneratorBuilder.packageConfigBuilder().parent("com.baomihua")
.moduleName("demo").controller("action").entity("entity")
.mapper("dao").service("iservice").serviceImpl("serviceIm")
.pathInfo(Collections.singletonMap("aaaa","bbbb")).build());
.pathInfo(Collections.singletonMap(OutputFile.controller,"bbbb")).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import org.h2.Driver;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -36,26 +37,26 @@ void matcherRegTableTest(){
@Test
void pathInfoTest() {
ConfigBuilder configBuilder;
Map<String, String> pathInfo;
Map<OutputFile, String> pathInfo;
configBuilder = new ConfigBuilder(GeneratorBuilder.packageConfig(), DATA_SOURCE_CONFIG, GeneratorBuilder.strategyConfig(),
GeneratorBuilder.templateConfig(), null, null);
pathInfo = configBuilder.getPathInfo();
Assertions.assertFalse(pathInfo.isEmpty());
Assertions.assertEquals(6, pathInfo.size());
Assertions.assertTrue(pathInfo.containsKey(ConstVal.ENTITY_PATH));
Assertions.assertTrue(pathInfo.containsKey(ConstVal.CONTROLLER_PATH));
Assertions.assertTrue(pathInfo.containsKey(ConstVal.SERVICE_PATH));
Assertions.assertTrue(pathInfo.containsKey(ConstVal.SERVICE_IMPL_PATH));
Assertions.assertTrue(pathInfo.containsKey(ConstVal.XML_PATH));
Assertions.assertTrue(pathInfo.containsKey(ConstVal.MAPPER_PATH));
Assertions.assertTrue(pathInfo.containsKey(OutputFile.entity));
Assertions.assertTrue(pathInfo.containsKey(OutputFile.controller));
Assertions.assertTrue(pathInfo.containsKey(OutputFile.service));
Assertions.assertTrue(pathInfo.containsKey(OutputFile.serviceImpl));
Assertions.assertTrue(pathInfo.containsKey(OutputFile.mapperXml));
Assertions.assertTrue(pathInfo.containsKey(OutputFile.mapper));

configBuilder = new ConfigBuilder(
GeneratorBuilder.packageConfigBuilder().pathInfo(Collections.singletonMap(ConstVal.ENTITY_PATH,
GeneratorBuilder.packageConfigBuilder().pathInfo(Collections.singletonMap(OutputFile.entity,
"/tmp/code/entity")).build(), DATA_SOURCE_CONFIG, GeneratorBuilder.strategyConfig(),
GeneratorBuilder.templateConfig(), null, null);
pathInfo = configBuilder.getPathInfo();
Assertions.assertFalse(pathInfo.isEmpty());
Assertions.assertEquals(1, pathInfo.size());
Assertions.assertTrue(pathInfo.containsKey(ConstVal.ENTITY_PATH));
Assertions.assertEquals(6, pathInfo.size());
Assertions.assertTrue(pathInfo.containsKey(OutputFile.entity));
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.baomidou.mybatisplus.test.generator;

import com.baomidou.mybatisplus.generator.SimpleAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.IConfigBuilder;
import com.baomidou.mybatisplus.generator.config.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;

import java.util.Collections;

/**
* H2 代码生成
Expand Down Expand Up @@ -32,6 +32,11 @@ public IConfigBuilder<InjectionConfig> injectionConfigBuilder() {
+ "objectMap: " + objectMap.size());
});
}

@Override
public PackageConfig.Builder packageConfigBuilder() {
return super.packageConfigBuilder().pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D://"));
}
}.execute();
}
}

0 comments on commit 494bec0

Please sign in to comment.