mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 17:48:25 +08:00
feat: 新增生成 mapperXml 文件。
This commit is contained in:
parent
04c6a363b9
commit
01f5a22a57
@ -41,6 +41,7 @@ public class GlobalConfig {
|
|||||||
private ServiceImplConfig serviceImplConfig;
|
private ServiceImplConfig serviceImplConfig;
|
||||||
private ControllerConfig controllerConfig;
|
private ControllerConfig controllerConfig;
|
||||||
private TableDefConfig tableDefConfig;
|
private TableDefConfig tableDefConfig;
|
||||||
|
private MapperXmlConfig mapperXmlConfig;
|
||||||
|
|
||||||
// === 其他配置 ===
|
// === 其他配置 ===
|
||||||
|
|
||||||
@ -54,6 +55,7 @@ public class GlobalConfig {
|
|||||||
private boolean serviceImplGenerateEnable;
|
private boolean serviceImplGenerateEnable;
|
||||||
private boolean controllerGenerateEnable;
|
private boolean controllerGenerateEnable;
|
||||||
private boolean tableDefGenerateEnable;
|
private boolean tableDefGenerateEnable;
|
||||||
|
private boolean mapperXmlGenerateEnable;
|
||||||
|
|
||||||
public GlobalConfig() {
|
public GlobalConfig() {
|
||||||
this.javadocConfig = new JavadocConfig();
|
this.javadocConfig = new JavadocConfig();
|
||||||
@ -115,6 +117,13 @@ public class GlobalConfig {
|
|||||||
return tableDefConfig;
|
return tableDefConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public MapperXmlConfig getMapperXmlConfig() {
|
||||||
|
if (mapperXmlConfig == null) {
|
||||||
|
mapperXmlConfig = new MapperXmlConfig();
|
||||||
|
}
|
||||||
|
return mapperXmlConfig;
|
||||||
|
}
|
||||||
|
|
||||||
public EntityConfig enableEntity() {
|
public EntityConfig enableEntity() {
|
||||||
entityGenerateEnable = true;
|
entityGenerateEnable = true;
|
||||||
return getEntityConfig();
|
return getEntityConfig();
|
||||||
@ -145,6 +154,11 @@ public class GlobalConfig {
|
|||||||
return getTableDefConfig();
|
return getTableDefConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public MapperXmlConfig enableMapperXml() {
|
||||||
|
mapperXmlGenerateEnable = true;
|
||||||
|
return mapperXmlConfig;
|
||||||
|
}
|
||||||
|
|
||||||
public void disableEntity() {
|
public void disableEntity() {
|
||||||
entityGenerateEnable = false;
|
entityGenerateEnable = false;
|
||||||
}
|
}
|
||||||
@ -169,6 +183,10 @@ public class GlobalConfig {
|
|||||||
tableDefGenerateEnable = false;
|
tableDefGenerateEnable = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void disableMapperXml() {
|
||||||
|
mapperXmlGenerateEnable = false;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isEntityGenerateEnable() {
|
public boolean isEntityGenerateEnable() {
|
||||||
return entityGenerateEnable;
|
return entityGenerateEnable;
|
||||||
}
|
}
|
||||||
@ -193,6 +211,10 @@ public class GlobalConfig {
|
|||||||
return tableDefGenerateEnable;
|
return tableDefGenerateEnable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isMapperXmlGenerateEnable() {
|
||||||
|
return mapperXmlGenerateEnable;
|
||||||
|
}
|
||||||
|
|
||||||
public void addCustomConfig(String key, Object value) {
|
public void addCustomConfig(String key, Object value) {
|
||||||
if (customConfig == null) {
|
if (customConfig == null) {
|
||||||
customConfig = new HashMap<>();
|
customConfig = new HashMap<>();
|
||||||
|
|||||||
@ -0,0 +1,39 @@
|
|||||||
|
package com.mybatisflex.codegen.config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成 MapperXml 的配置。
|
||||||
|
*
|
||||||
|
* @author 王帅
|
||||||
|
* @since 2023-05-17
|
||||||
|
*/
|
||||||
|
public class MapperXmlConfig {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MapperXml 文件的前缀。
|
||||||
|
*/
|
||||||
|
private String filePrefix = "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MapperXml 文件的后缀。
|
||||||
|
*/
|
||||||
|
private String fileSuffix = "Mapper";
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -51,8 +51,13 @@ public class PackageConfig {
|
|||||||
*/
|
*/
|
||||||
private String tableDefPackage;
|
private String tableDefPackage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MapperXml 文件所在位置。
|
||||||
|
*/
|
||||||
|
private String mapperXmlPath;
|
||||||
|
|
||||||
public String getSourceDir() {
|
public String getSourceDir() {
|
||||||
if (sourceDir == null || StringUtil.isBlank(sourceDir)) {
|
if (StringUtil.isBlank(sourceDir)) {
|
||||||
return System.getProperty("user.dir") + "/src/main/java";
|
return System.getProperty("user.dir") + "/src/main/java";
|
||||||
}
|
}
|
||||||
return sourceDir;
|
return sourceDir;
|
||||||
@ -144,4 +149,16 @@ public class PackageConfig {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getMapperXmlPath() {
|
||||||
|
if (StringUtil.isBlank(mapperXmlPath)) {
|
||||||
|
return getSourceDir().concat("/resources/mapper");
|
||||||
|
}
|
||||||
|
return mapperXmlPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PackageConfig setMapperXmlPath(String mapperXmlPath) {
|
||||||
|
this.mapperXmlPath = mapperXmlPath;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -14,6 +14,8 @@ public final class TemplateConst {
|
|||||||
public static final String SERVICE_IMPL = "/templates/enjoy/serviceImpl.tpl";
|
public static final String SERVICE_IMPL = "/templates/enjoy/serviceImpl.tpl";
|
||||||
public static final String CONTROLLER = "/templates/enjoy/controller.tpl";
|
public static final String CONTROLLER = "/templates/enjoy/controller.tpl";
|
||||||
public static final String TABLE_DEF = "/templates/enjoy/tableDef.tpl";
|
public static final String TABLE_DEF = "/templates/enjoy/tableDef.tpl";
|
||||||
|
public static final String MAPPER_XML = "/templates/enjoy/mapperXml.tpl";
|
||||||
|
|
||||||
private TemplateConst() {
|
private TemplateConst() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -198,6 +198,19 @@ public class Table {
|
|||||||
+ tableDefConfig.getClassSuffix();
|
+ tableDefConfig.getClassSuffix();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建 MapperXml 的文件名称
|
||||||
|
*
|
||||||
|
* @return fileName
|
||||||
|
*/
|
||||||
|
public String buildMapperXmlFileName() {
|
||||||
|
String tableDefJavaFileName = getEntityJavaFileName();
|
||||||
|
MapperXmlConfig mapperXmlConfig = globalConfig.getMapperXmlConfig();
|
||||||
|
return mapperXmlConfig.getFilePrefix()
|
||||||
|
+ tableDefJavaFileName
|
||||||
|
+ mapperXmlConfig.getFileSuffix();
|
||||||
|
}
|
||||||
|
|
||||||
public String buildExtends() {
|
public String buildExtends() {
|
||||||
EntityConfig entityConfig = globalConfig.getEntityConfig();
|
EntityConfig entityConfig = globalConfig.getEntityConfig();
|
||||||
if (entityConfig.getSupperClass() != null) {
|
if (entityConfig.getSupperClass() != null) {
|
||||||
|
|||||||
@ -32,6 +32,7 @@ public class GeneratorFactory {
|
|||||||
registerGenerator("serviceImpl", new ServiceImplGenerator());
|
registerGenerator("serviceImpl", new ServiceImplGenerator());
|
||||||
registerGenerator("controller", new ControllerGenerator());
|
registerGenerator("controller", new ControllerGenerator());
|
||||||
registerGenerator("tableDef", new TableDefGenerator());
|
registerGenerator("tableDef", new TableDefGenerator());
|
||||||
|
registerGenerator("mapperXml", new MapperXmlGenerator());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,72 @@
|
|||||||
|
/**
|
||||||
|
* 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.PackageConfig;
|
||||||
|
import com.mybatisflex.codegen.config.StrategyConfig;
|
||||||
|
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 final 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();
|
||||||
|
StrategyConfig strategyConfig = globalConfig.getStrategyConfig();
|
||||||
|
|
||||||
|
File mapperXmlFile = new File(packageConfig.getMapperXmlPath() + "/" +
|
||||||
|
table.buildMapperXmlFileName() + ".xml");
|
||||||
|
|
||||||
|
|
||||||
|
if (mapperXmlFile.exists() && !strategyConfig.isOverwriteEnable()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Map<String, Object> params = new HashMap<>(2);
|
||||||
|
params.put("table", table);
|
||||||
|
params.put("packageConfig", packageConfig);
|
||||||
|
|
||||||
|
strategyConfig.getTemplateEngine().generate(params, templatePath, mapperXmlFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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>
|
||||||
Loading…
x
Reference in New Issue
Block a user