Skip to content

Commit

Permalink
重构生成器模板配置.
Browse files Browse the repository at this point in the history
  • Loading branch information
nieqiurong committed Mar 26, 2024
1 parent 1ee6b08 commit 642941c
Show file tree
Hide file tree
Showing 15 changed files with 296 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@ public AutoGenerator packageInfo(@NotNull PackageConfig packageConfig) {
*
* @param templateConfig 模板配置
* @return this
* @see #strategy
* @since 3.5.0
*/
@Deprecated
public AutoGenerator template(@NotNull TemplateConfig templateConfig) {
this.template = templateConfig;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.baomidou.mybatisplus.generator.config;

import com.baomidou.mybatisplus.generator.config.builder.Service;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -90,9 +91,11 @@ private GlobalConfig() {
* 是否生成service 接口(默认 true)
* 增加此开关的原因:在某些项目实践中,只需要生成service实现类,不需要抽象sevice接口
* 针对某些项目,生成service接口,开发时反而麻烦,这种情况,可以将该属性设置为false
* @deprecated 3.5.6 {@link Service.Builder#disableService()}
*/
@Getter
@Setter
@Deprecated
private boolean serviceInterface = true;

public boolean isSwagger() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
package com.baomidou.mybatisplus.generator.config;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.config.builder.Controller;
import com.baomidou.mybatisplus.generator.config.builder.Entity;
import com.baomidou.mybatisplus.generator.config.builder.Service;
import com.baomidou.mybatisplus.generator.config.builder.Mapper;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
Expand All @@ -27,6 +31,7 @@
* @author tzg hubin
* @since 2017-06-17
*/
@Deprecated
public class TemplateConfig {

private static final Logger LOGGER = LoggerFactory.getLogger(TemplateConfig.class);
Expand Down Expand Up @@ -171,6 +176,7 @@ public TemplateConfig disable() {
*
* @author nieqiurong 3.5.0
*/
@Deprecated
public static class Builder implements IConfigBuilder<TemplateConfig> {

private final TemplateConfig templateConfig;
Expand Down Expand Up @@ -207,7 +213,9 @@ public Builder disable(@NotNull TemplateType... templateTypes) {
*
* @param entityTemplate 实体模板
* @return this
* @deprecated {@link Entity.Builder#javaTemplate}
*/
@Deprecated
public Builder entity(@NotNull String entityTemplate) {
this.templateConfig.disableEntity = false;
this.templateConfig.entity = entityTemplate;
Expand All @@ -219,7 +227,9 @@ public Builder entity(@NotNull String entityTemplate) {
*
* @param entityKtTemplate 实体模板
* @return this
* @deprecated {@link Entity.Builder#javaTemplate}
*/
@Deprecated
public Builder entityKt(@NotNull String entityKtTemplate) {
this.templateConfig.disableEntity = false;
this.templateConfig.entityKt = entityKtTemplate;
Expand All @@ -229,9 +239,11 @@ public Builder entityKt(@NotNull String entityKtTemplate) {
/**
* 设置service模板路径
*
* @param serviceTemplate service接口模板路径
* @param serviceTemplate service接口模板路径
* @return this
* @deprecated {@link Service.Builder#serviceTemplate(String)}
*/
@Deprecated
public Builder service(@NotNull String serviceTemplate) {
this.templateConfig.service = serviceTemplate;
return this;
Expand All @@ -242,7 +254,9 @@ public Builder service(@NotNull String serviceTemplate) {
*
* @param serviceImplTemplate service实现类模板路径
* @return this
* @deprecated {@link Service.Builder#serviceImplTemplate(String)}
*/
@Deprecated
public Builder serviceImpl(@NotNull String serviceImplTemplate) {
this.templateConfig.serviceImpl = serviceImplTemplate;
return this;
Expand All @@ -253,7 +267,9 @@ public Builder serviceImpl(@NotNull String serviceImplTemplate) {
*
* @param mapperTemplate mapper模板路径
* @return this
* @deprecated {@link Mapper.Builder#mapperTemplate(String)}
*/
@Deprecated
public Builder mapper(@NotNull String mapperTemplate) {
this.templateConfig.mapper = mapperTemplate;
return this;
Expand All @@ -264,7 +280,9 @@ public Builder mapper(@NotNull String mapperTemplate) {
*
* @param xmlTemplate xml模板路径
* @return this
* @deprecated {@link Mapper.Builder#mapperXmlTemplate(String)}
*/
@Deprecated
public Builder xml(@NotNull String xmlTemplate) {
this.templateConfig.xml = xmlTemplate;
return this;
Expand All @@ -275,7 +293,9 @@ public Builder xml(@NotNull String xmlTemplate) {
*
* @param controllerTemplate 控制器模板路径
* @return this
* @deprecated {@link Controller.Builder#template(String)}
*/
@Deprecated
public Builder controller(@NotNull String controllerTemplate) {
this.templateConfig.controller = controllerTemplate;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class ConfigBuilder {
/**
* 模板路径配置信息
*/
@Deprecated
private final TemplateConfig templateConfig;

/**
Expand Down Expand Up @@ -142,6 +143,7 @@ public ConfigBuilder setInjectionConfig(@NotNull InjectionConfig injectionConfig
}

@NotNull
@Deprecated
public TemplateConfig getTemplateConfig() {
return templateConfig;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ private Controller() {
@Getter
private boolean fileOverride;

/**
* 是否生成
*
* @since 3.5.6
*/
@Getter
private boolean generate = true;

/**
* 模板路径
* @since 3.5.6
*/
@Getter
private String templatePath = ConstVal.TEMPLATE_CONTROLLER;

@Nullable
public String getSuperClass() {
return superClass;
Expand Down Expand Up @@ -200,6 +215,29 @@ public Builder enableFileOverride() {
return this;
}

/**
* 禁用生成
*
* @return this
* @since 3.5.6
*/
public Builder disable() {
this.controller.generate = false;
return this;
}

/**
* 指定模板路径
*
* @param template 模板路径
* @return this
* @since 3.5.6
*/
public Builder template(String template) {
this.controller.templatePath = template;
return this;
}

@NotNull
public Controller get() {
return this.controller;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public class CustomFile {
*/
private boolean fileOverride;

/**
* 是否生成
*/
@Getter
private boolean generate;

/**
* 构建者
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.IFill;
import com.baomidou.mybatisplus.generator.ITemplate;
import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.baomidou.mybatisplus.generator.config.INameConvert;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
Expand Down Expand Up @@ -58,6 +59,20 @@ public class Entity implements ITemplate {

private static final Logger LOGGER = LoggerFactory.getLogger(Entity.class);

/**
* Java模板默认路径
*
* @since 3.5.6
*/
@Getter
private String javaTemplate = ConstVal.TEMPLATE_ENTITY_JAVA;

/**
* Kotlin模板默认撸
*/
@Getter
private String kotlinTemplate = ConstVal.TEMPLATE_ENTITY_KT;

private Entity() {
}

Expand Down Expand Up @@ -199,6 +214,15 @@ private Entity() {
@Getter
private boolean fileOverride;


/**
* 是否生成
*
* @since 3.5.6
*/
@Getter
private boolean generate;

/**
* <p>
* 父类 Class 反射属性转换为公共字段
Expand Down Expand Up @@ -617,6 +641,31 @@ public Builder enableFileOverride() {
return this;
}

/**
* 指定模板路径
*
* @param template 模板路径
* @return this
* @since 3.5.6
*/
public Builder javaTemplate(String template) {
this.entity.javaTemplate = template;
return this;
}

/**
* 指定模板路径
*
* @param template 模板路径
* @return this
* @since 3.5.6
*/
public Builder kotlinTemplatePath(String template) {
this.entity.kotlinTemplate = template;
return this;
}


public Entity get() {
String superClass = this.entity.superClass;
if (StringUtils.isNotBlank(superClass)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,38 @@ private Mapper() {
*/
private Class<? extends Cache> cache;

/**
* 是否生成XML
*
* @since 3.5.6
*/
@Getter
private boolean generateMapperXml = true;

/**
* 是否生成Mapper
*
* @since 3.5.6
*/
@Getter
private boolean generateMapper = true;

/**
* Mapper模板路径
*
* @since 3.5.6
*/
@Getter
private String mapperTemplatePath = ConstVal.TEMPLATE_MAPPER;

/**
* MapperXml模板路径
*
* @since 3.5.6
*/
@Getter
private String mapperXmlTemplatePath = ConstVal.TEMPLATE_XML;

@NotNull
public String getSuperClass() {
return superClass;
Expand Down Expand Up @@ -145,6 +177,8 @@ public Map<String, Object> renderData(@NotNull TableInfo tableInfo) {
data.put("cacheClassName", cacheClass.getName());
}
data.put("superMapperClass", ClassUtils.getSimpleName(this.superClass));
data.put("generateMapperXml", this.generateMapperXml);
data.put("generateMapper", this.generateMapper);
return data;
}

Expand Down Expand Up @@ -306,6 +340,28 @@ public Builder enableFileOverride() {
return this;
}

/**
* Service模板路径
*
* @return this
* @since 3.5.6
*/
public Builder mapperTemplate(String template) {
this.mapper.mapperTemplatePath = template;
return this;
}

/**
* ServiceImpl模板路径
*
* @return this
* @since 3.5.6
*/
public Builder mapperXmlTemplate(String template) {
this.mapper.mapperXmlTemplatePath = template;
return this;
}

@NotNull
public Mapper get() {
return this.mapper;
Expand Down
Loading

0 comments on commit 642941c

Please sign in to comment.