Browse Source

优化模板配置与数据库配置.

nieqiurong 4 years ago
parent
commit
9bde5177fc

+ 1 - 0
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/ConstVal.java

@@ -53,6 +53,7 @@ public interface ConstVal {
 
     String TEMPLATE_ENTITY_JAVA = "/templates/entity.java";
     String TEMPLATE_ENTITY_KT = "/templates/entity.kt";
+    String TEMPLATE_ENTITY = "/templates/entity";
     String TEMPLATE_MAPPER = "/templates/mapper.java";
     String TEMPLATE_XML = "/templates/mapper.xml";
     String TEMPLATE_SERVICE = "/templates/service.java";

+ 265 - 18
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/DataSourceConfig.java

@@ -26,6 +26,7 @@ import lombok.Data;
 import lombok.experimental.Accessors;
 
 import java.sql.Connection;
+import java.sql.Driver;
 import java.sql.DriverManager;
 import java.sql.SQLException;
 import java.util.Optional;
@@ -79,6 +80,142 @@ public class DataSourceConfig {
      */
     private String password;
 
+    /**
+     * 后续不再公开次构造方法
+     *
+     * @see Builder#Builder(String, String, String)
+     * @deprecated 3.4.1
+     */
+    @Deprecated
+    public DataSourceConfig() {
+    }
+
+    /**
+     * 设置表数据查询实现类
+     *
+     * @param dbQuery 表数据查询
+     * @return this
+     * @see Builder#dbQuery(IDbQuery)
+     * @deprecated 3.4.1
+     */
+    @Deprecated
+    public DataSourceConfig setDbQuery(IDbQuery dbQuery) {
+        this.dbQuery = dbQuery;
+        return this;
+    }
+
+    /**
+     * 设置数据库类型
+     *
+     * @param dbType 数据库类型
+     * @return this
+     * @see Builder#dbType(DbType)
+     * @deprecated 3.4.1
+     */
+    @Deprecated
+    public DataSourceConfig setDbType(DbType dbType) {
+        this.dbType = dbType;
+        return this;
+    }
+
+    /**
+     * 设置数据库schema
+     *
+     * @param schemaName 指定schema
+     * @return this
+     * @see Builder#schema(String)
+     * @deprecated 3.4.1
+     */
+    @Deprecated
+    public DataSourceConfig setSchemaName(String schemaName) {
+        this.schemaName = schemaName;
+        return this;
+    }
+
+    /**
+     * 设置数据库字段转换实现
+     *
+     * @param typeConvert 数据库字段转换实现
+     * @return this
+     * @see Builder#typeConvert(ITypeConvert)
+     * @deprecated 3.4.1
+     */
+    @Deprecated
+    public DataSourceConfig setTypeConvert(ITypeConvert typeConvert) {
+        this.typeConvert = typeConvert;
+        return this;
+    }
+
+    /**
+     * 设置关键字处理器
+     *
+     * @param keyWordsHandler 关键字处理器
+     * @return this
+     * @see Builder#keyWordsHandler(IKeyWordsHandler)
+     * @deprecated 3.4.1
+     */
+    @Deprecated
+    public DataSourceConfig setKeyWordsHandler(IKeyWordsHandler keyWordsHandler) {
+        this.keyWordsHandler = keyWordsHandler;
+        return this;
+    }
+
+    /**
+     * 设置数据库连接地址
+     *
+     * @param url 数据库连接地址
+     * @return this
+     * @see Builder#Builder(java.lang.String, java.lang.String, java.lang.String)
+     * @deprecated 3.4.1
+     */
+    @Deprecated
+    public DataSourceConfig setUrl(String url) {
+        this.url = url;
+        return this;
+    }
+
+    /**
+     * 设置驱动名称
+     *
+     * @param driverName 驱动名
+     * @return this
+     * @see Builder#driver(String)
+     * @deprecated 3.4.1
+     */
+    @Deprecated
+    public DataSourceConfig setDriverName(String driverName) {
+        this.driverName = driverName;
+        return this;
+    }
+
+    /**
+     * 设置数据库账号
+     *
+     * @param username 数据库账号
+     * @return this
+     * @see Builder#Builder(java.lang.String, java.lang.String, java.lang.String)
+     * @deprecated 3.4.1
+     */
+    @Deprecated
+    public DataSourceConfig setUsername(String username) {
+        this.username = username;
+        return this;
+    }
+
+    /**
+     * 设置数据库密码
+     *
+     * @param password 数据库密码
+     * @return this
+     * @see Builder#Builder(java.lang.String, java.lang.String, java.lang.String)
+     * @deprecated 3.4.1
+     */
+    @Deprecated
+    public DataSourceConfig setPassword(String password) {
+        this.password = password;
+        return this;
+    }
+
     public IDbQuery getDbQuery() {
         if (null == dbQuery) {
             DbType dbType = getDbType();
@@ -97,9 +234,9 @@ public class DataSourceConfig {
      */
     public DbType getDbType() {
         if (null == this.dbType) {
-            this.dbType = this.getDbType(this.driverName);
+            this.dbType = this.getDbType(this.url.toLowerCase());
             if (null == this.dbType) {
-                this.dbType = this.getDbType(this.url.toLowerCase());
+                this.dbType = this.getDbType(this.driverName);
                 if (null == this.dbType) {
                     throw ExceptionUtils.mpe("Unknown type of database!");
                 }
@@ -116,34 +253,33 @@ public class DataSourceConfig {
      * @return 类型枚举值,如果没找到,则返回 null
      */
     private DbType getDbType(String str) {
-        if (str.contains("mysql")) {
+        if (url.contains(":mysql:") || url.contains(":cobar:")) {
             return DbType.MYSQL;
-        } else if (str.contains("oracle")) {
+        } else if (str.contains(":oracle:")) {
             return DbType.ORACLE;
-        } else if (str.contains("postgresql")) {
+        } else if (str.contains(":postgresql:")) {
             return DbType.POSTGRE_SQL;
-        } else if (str.contains("sqlserver")) {
+        } else if (str.contains(":sqlserver:")) {
             return DbType.SQL_SERVER;
-        } else if (str.contains("db2")) {
+        } else if (str.contains(":db2:")) {
             return DbType.DB2;
-        } else if (str.contains("mariadb")) {
+        } else if (str.contains(":mariadb:")) {
             return DbType.MARIADB;
-        } else if (str.contains("sqlite")) {
+        } else if (str.contains(":sqlite:")) {
             return DbType.SQLITE;
-        } else if (str.contains("h2")) {
+        } else if (str.contains(":h2:")) {
             return DbType.H2;
-        } else if (str.contains("kingbase") || str.contains("kingbase8")) {
+        } else if (str.contains(":kingbase:") || str.contains(":kingbase8:")) {
             return DbType.KINGBASE_ES;
-        } else if (str.contains("dm")) {
+        } else if (str.contains(":dm:")) {
             return DbType.DM;
-        } else if (str.contains("zenith")) {
+        } else if (str.contains(":zenith:")) {
             return DbType.GAUSS;
-        } else if (str.contains("oscar")) {
+        } else if (str.contains(":oscar:")) {
             return DbType.OSCAR;
-        } else if (str.contains("firebird")) {
+        } else if (str.contains(":firebird:")) {
             return DbType.FIREBIRD;
-        }
-        else {
+        } else {
             return DbType.OTHER;
         }
     }
@@ -170,7 +306,9 @@ public class DataSourceConfig {
     public Connection getConn() {
         Connection conn;
         try {
-            Class.forName(this.driverName);
+            if (StringUtils.isNotBlank(this.driverName)) {
+                Class.forName(this.driverName);
+            }
             conn = DriverManager.getConnection(this.url, this.username, this.password);
             String schema = StringUtils.isNotBlank(this.schemaName) ? this.schemaName : getDefaultSchema();
             if (StringUtils.isNotBlank(schema)) {
@@ -207,4 +345,113 @@ public class DataSourceConfig {
         }
         return schema;
     }
+
+    /**
+     * 数据库配置构建者
+     *
+     * @author nieqiurong 2020/10/10.
+     * @since 3.4.1
+     */
+    public static class Builder {
+
+        private final DataSourceConfig dataSourceConfig = new DataSourceConfig();
+
+        /**
+         * 构造初始化方法
+         *
+         * @param url      数据库连接地址
+         * @param username 数据库账号
+         * @param password 数据库密码
+         */
+        public Builder(String url, String username, String password) {
+            this.dataSourceConfig.url = url;
+            this.dataSourceConfig.username = username;
+            this.dataSourceConfig.password = password;
+        }
+
+        /**
+         * 设置数据库查询实现
+         *
+         * @param dbQuery 数据库查询实现
+         * @return this
+         */
+        public Builder dbQuery(IDbQuery dbQuery) {
+            this.dataSourceConfig.dbQuery = dbQuery;
+            return this;
+        }
+
+        /**
+         * 设置数据库类型
+         *
+         * @param dbType 数据库类型
+         * @return this
+         */
+        public Builder dbType(DbType dbType) {
+            this.dataSourceConfig.dbType = dbType;
+            return this;
+        }
+
+        /**
+         * 设置数据库schema
+         *
+         * @param schemaName 数据库schema
+         * @return this
+         */
+        public Builder schema(String schemaName) {
+            this.dataSourceConfig.schemaName = schemaName;
+            return this;
+        }
+
+        /**
+         * 设置数据库驱动
+         *
+         * @param driverName 驱动名
+         * @return this
+         */
+        public Builder driver(String driverName) {
+            this.dataSourceConfig.driverName = driverName;
+            return this;
+        }
+
+        /**
+         * 设置数据库驱动
+         *
+         * @param driver 驱动类
+         * @return this
+         */
+        public Builder driver(Class<? extends Driver> driver) {
+            return driver(driver.getName());
+        }
+
+        /**
+         * 设置类型转换器
+         *
+         * @param typeConvert 类型转换器
+         * @return this
+         */
+        public Builder typeConvert(ITypeConvert typeConvert) {
+            this.dataSourceConfig.typeConvert = typeConvert;
+            return this;
+        }
+
+        /**
+         * 设置数据库关键字处理器
+         *
+         * @param keyWordsHandler 关键字处理器
+         * @return this
+         */
+        public Builder keyWordsHandler(IKeyWordsHandler keyWordsHandler) {
+            this.dataSourceConfig.keyWordsHandler = keyWordsHandler;
+            return this;
+        }
+
+        /**
+         * 构建数据库配置
+         *
+         * @return 数据库配置
+         */
+        public DataSourceConfig build() {
+            return this.dataSourceConfig;
+        }
+    }
 }

+ 2 - 2
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/GlobalConfig.java

@@ -32,9 +32,9 @@ import lombok.experimental.Accessors;
 public class GlobalConfig {
 
     /**
-     * 生成文件的输出目录【默认 D 盘根目录
+     * 生成文件的输出目录【 windows:D://  linux or mac:/tmp 
      */
-    private String outputDir = "D://";
+    private String outputDir = System.getProperty("os.name").toLowerCase().contains("windows") ? "D://" : "/tmp";
 
     /**
      * 是否覆盖已有文件

+ 365 - 12
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/TemplateConfig.java

@@ -15,10 +15,17 @@
  */
 package com.baomidou.mybatisplus.generator.config;
 
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import lombok.AccessLevel;
 import lombok.Data;
 import lombok.Getter;
+import lombok.Setter;
 import lombok.experimental.Accessors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 
 /**
@@ -31,23 +38,207 @@ import lombok.experimental.Accessors;
 @Accessors(chain = true)
 public class TemplateConfig {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(TemplateConfig.class);
+
+    private static final Pattern FILE_TYPE = Pattern.compile("\\.[^.\\\\/:*?\"<>|\\r\\n]+$");
+
+    /**
+     * 由于需要支持kotlin与java两种模板,当不需要支持其中一种的话,可以使用方式一进行设置.
+     * example:
+     * 1.setEntity("/templates/entity.java") or setEntity("/templates/entity.kt")
+     * 2.setEntity("/templates/entity")
+     * 3.setEntity("/templates/entity%s")
+     * 设置实体模板路径
+     */
+    @Getter(AccessLevel.NONE)
+    private String entity;
+
     @Getter(AccessLevel.NONE)
-    private String entity = ConstVal.TEMPLATE_ENTITY_JAVA;
+    @Setter(AccessLevel.NONE)
+    private boolean disableEntity;
 
+    /**
+     * 设置实体模板路径
+     *
+     * @see #entity
+     * @deprecated 3.4.1
+     */
+    @Deprecated
     private String entityKt = ConstVal.TEMPLATE_ENTITY_KT;
 
-    private String service = ConstVal.TEMPLATE_SERVICE;
+    private String service;
+
+    private String serviceImpl;
+
+    private String mapper;
+
+    private String xml;
+
+    private String controller;
+
+    /**
+     * 后续不再公开此方法.
+     *
+     * @see Builder#all()
+     * @deprecated 3.4.1
+     */
+    @Deprecated
+    public TemplateConfig() {
+        this.controller = ConstVal.TEMPLATE_CONTROLLER;
+        this.entity = ConstVal.TEMPLATE_ENTITY;
+        this.xml = ConstVal.TEMPLATE_XML;
+        this.service = ConstVal.TEMPLATE_SERVICE;
+        this.serviceImpl = ConstVal.TEMPLATE_SERVICE_IMPL;
+        this.mapper = ConstVal.TEMPLATE_MAPPER;
+    }
+
+    /**
+     * 设置实体模板
+     *
+     * @param entity 实体模板
+     * @return this
+     * @see Builder#entity(String)
+     * @deprecated 3.4.1
+     */
+    @Deprecated
+    public TemplateConfig setEntity(String entity) {
+        logger(entity, TemplateType.ENTITY);
+        this.entity = entity;
+        return this;
+    }
+
+    /**
+     * 设置service接口模板
+     *
+     * @param service service接口模板
+     * @return this
+     * @see Builder#service(String, String)
+     * @deprecated 3.4.1
+     */
+    @Deprecated
+    public TemplateConfig setService(String service) {
+        logger(service, TemplateType.SERVICE);
+        this.service = service;
+        return this;
+    }
+
+    /**
+     * 设置service实现类模板
+     *
+     * @param serviceImpl service实现类模板
+     * @return this
+     * @see Builder#service(String, String)
+     * @deprecated 3.4.1
+     */
+    @Deprecated
+    public TemplateConfig setServiceImpl(String serviceImpl) {
+        logger(serviceImpl, TemplateType.SERVICE);
+        this.serviceImpl = serviceImpl;
+        return this;
+    }
+
+    /**
+     * 设置mapper模板
+     *
+     * @param mapper mapper模板
+     * @return this
+     * @see Builder#mapper(String)
+     * @deprecated 3.4.1
+     */
+    @Deprecated
+    public TemplateConfig setMapper(String mapper) {
+        logger(mapper, TemplateType.ENTITY);
+        this.mapper = mapper;
+        return this;
+    }
+
+    /**
+     * 设置mapperXml模板
+     *
+     * @param xml mapperXml模板
+     * @return this
+     * @see Builder#mapperXml(String)
+     * @deprecated 3.4.1
+     */
+    @Deprecated
+    public TemplateConfig setXml(String xml) {
+        logger(xml, TemplateType.XML);
+        this.xml = xml;
+        return this;
+    }
 
-    private String serviceImpl = ConstVal.TEMPLATE_SERVICE_IMPL;
+    /**
+     * 设置控制器模板
+     *
+     * @param controller 控制器模板
+     * @return this
+     * @see Builder#controller(String)
+     * @deprecated 3.4.1
+     */
+    @Deprecated
+    public TemplateConfig setController(String controller) {
+        logger(controller, TemplateType.CONTROLLER);
+        this.controller = controller;
+        return this;
+    }
 
-    private String mapper = ConstVal.TEMPLATE_MAPPER;
+    /**
+     * 当模板赋值为空时进行日志提示打印
+     *
+     * @param value        模板值
+     * @param templateType 模板类型
+     */
+    private void logger(String value, TemplateType templateType) {
+        if (StringUtils.isBlank(value)) {
+            LOGGER.warn("推荐使用disable(TemplateType.{})方法进行默认模板禁用.", templateType.name());
+        }
+    }
 
-    private String xml = ConstVal.TEMPLATE_XML;
+    /**
+     * 设置实体模板路径
+     *
+     * @param entityKt 模板路径
+     * @deprecated 3.4.1 {@link #setEntity(String)}
+     */
+    @Deprecated
+    public TemplateConfig setEntityKt(String entityKt) {
+        return setEntity(entityKt);
+    }
 
-    private String controller = ConstVal.TEMPLATE_CONTROLLER;
+    /**
+     * @return 获取实体模板路径
+     * @deprecated 3.4.1 {@link #getEntity(boolean)}
+     */
+    @Deprecated
+    public String getEntityKt() {
+        return getEntity(true);
+    }
 
+    /**
+     * 获取实体模板路径
+     *
+     * @param kotlin 是否kotlin
+     * @return 模板路径
+     */
     public String getEntity(boolean kotlin) {
-        return kotlin ? entityKt : entity;
+        if (!disableEntity) {
+            if (StringUtils.isBlank(entity)) {
+                // 默认情况
+                return kotlin ? ConstVal.TEMPLATE_ENTITY_KT : ConstVal.TEMPLATE_ENTITY_JAVA;
+            }
+            // 用户自定义情况,尝试替换文件后缀进行加载
+            Matcher matcher = FILE_TYPE.matcher(entity);
+            if (matcher.find()) {
+                return matcher.replaceAll(kotlin ? ".kt" : ".java");
+            }
+            if (entity.endsWith("%s")) {
+                return kotlin ? String.format(entity, ".kt") : String.format(entity, ".java");
+            } else {
+                //支持无后缀情况,自动加后缀
+                return kotlin ? entity + ".kt" : entity + ".java";
+            }
+        }
+        return null;
     }
 
     /**
@@ -62,19 +253,23 @@ public class TemplateConfig {
             for (TemplateType templateType : templateTypes) {
                 switch (templateType) {
                     case XML:
-                        setXml(null);
+                        this.xml = null;
                         break;
                     case ENTITY:
-                        setEntity(null).setEntityKt(null);
+                        this.entity = null;
+                        this.entityKt = null;
+                        //暂时没其他多的需求,使用一个单独的boolean变量进行支持一下.
+                        this.disableEntity = true;
                         break;
                     case MAPPER:
-                        setMapper(null);
+                        this.mapper = null;
                         break;
                     case SERVICE:
-                        setService(null).setServiceImpl(null);
+                        this.service = null;
+                        this.serviceImpl = null;
                         break;
                     case CONTROLLER:
-                        setController(null);
+                        this.controller = null;
                         break;
                     default:
                 }
@@ -83,4 +278,162 @@ public class TemplateConfig {
         return this;
     }
 
+    /**
+     * 禁用全部模板
+     *
+     * @return this
+     * @since 3.4.1
+     */
+    public TemplateConfig disable() {
+        return disable(TemplateType.values());
+    }
+
+
+    /**
+     * 模板路径配置构建者
+     *
+     * @author nieqiurong 3.4.1
+     */
+    public static class Builder {
+
+        private final TemplateConfig templateConfig = new TemplateConfig();
+
+        /**
+         * 默认生成一个空的
+         */
+        public Builder() {
+            // 后续去除构造之后去除此方法调用
+            templateConfig.disable(TemplateType.values());
+        }
+
+        /**
+         * 激活所有默认配置模板
+         *
+         * @return 默认配置模板
+         */
+        public Builder all() {
+            this.templateConfig.controller = ConstVal.TEMPLATE_CONTROLLER;
+            this.templateConfig.entity = ConstVal.TEMPLATE_ENTITY;
+            this.templateConfig.xml = ConstVal.TEMPLATE_XML;
+            this.templateConfig.service = ConstVal.TEMPLATE_SERVICE;
+            this.templateConfig.serviceImpl = ConstVal.TEMPLATE_SERVICE_IMPL;
+            this.templateConfig.mapper = ConstVal.TEMPLATE_MAPPER;
+            this.templateConfig.disableEntity = false;
+            return this;
+        }
+
+        /**
+         * 使用默认实体模板
+         *
+         * @return this
+         */
+        public Builder entity() {
+            return entity(ConstVal.TEMPLATE_ENTITY);
+        }
+
+        /**
+         * 设置实体模板路径
+         *
+         * @param entityTemplate 实体模板
+         * @return this
+         */
+        public Builder entity(String entityTemplate) {
+            this.templateConfig.disableEntity = false;
+            this.templateConfig.setEntity(entityTemplate);
+            return this;
+        }
+
+        /**
+         * 使用默认service模板
+         *
+         * @return this
+         */
+        public Builder service() {
+            return service(ConstVal.TEMPLATE_SERVICE, ConstVal.TEMPLATE_SERVICE_IMPL);
+        }
+
+        /**
+         * 设置service模板路径
+         *
+         * @param serviceTemplate     service接口模板路径
+         * @param serviceImplTemplate service实现类模板路径
+         * @return this
+         */
+        public Builder service(String serviceTemplate, String serviceImplTemplate) {
+            this.templateConfig.setService(serviceTemplate).setServiceImpl(serviceImplTemplate);
+            return this;
+        }
+
+        /**
+         * 使用默认mapper模板
+         *
+         * @return this
+         */
+        public Builder mapper() {
+            return mapper(ConstVal.TEMPLATE_MAPPER);
+        }
+
+        /**
+         * 设置mapper模板路径
+         *
+         * @param mapperTemplate mapper模板路径
+         * @return this
+         */
+        public Builder mapper(String mapperTemplate) {
+            this.templateConfig.setMapper(mapperTemplate);
+            return this;
+        }
+
+
+        /**
+         * 使用默认mapperXml模板
+         *
+         * @return this
+         */
+        public Builder mapperXml() {
+            return mapperXml(ConstVal.TEMPLATE_XML);
+        }
+
+        /**
+         * 设置mapperXml模板路径
+         *
+         * @param mapperXmlTemplate xml模板路径
+         * @return this
+         */
+        public Builder mapperXml(String mapperXmlTemplate) {
+            this.templateConfig.setXml(mapperXmlTemplate);
+            return this;
+        }
+
+        /**
+         * 使用默认控制器模板
+         *
+         * @return this
+         */
+        public Builder controller() {
+            return controller(ConstVal.TEMPLATE_CONTROLLER);
+        }
+
+        /**
+         * 设置控制器模板路径
+         *
+         * @param controllerTemplate 控制器模板路径
+         * @return this
+         */
+        public Builder controller(String controllerTemplate) {
+            this.templateConfig.setController(controllerTemplate);
+            return this;
+        }
+
+        /**
+         * 构建模板配置对象
+         *
+         * @return 模板配置对象
+         */
+        public TemplateConfig build() {
+            return this.templateConfig;
+        }
+
+    }
+
 }

+ 1 - 1
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/builder/ConfigBuilder.java

@@ -111,7 +111,7 @@ public class ConfigBuilder {
         this.dataSourceConfig = dataSourceConfig;
         this.dbQuery = new DecoratorDbQuery(dataSourceConfig.getDbQuery(), dataSourceConfig, strategyConfig);
         this.globalConfig = Optional.ofNullable(globalConfig).orElseGet(GlobalConfig::new);
-        this.template = Optional.ofNullable(template).orElseGet(TemplateConfig::new);
+        this.template = Optional.ofNullable(template).orElseGet(() -> new TemplateConfig.Builder().all().build());
         this.packageConfig = Optional.ofNullable(packageConfig).orElseGet(PackageConfig::new);
         this.pathInfo.putAll(new PathInfoHandler(this.globalConfig, this.template, this.packageConfig).getPathInfo());
         this.tableInfoList.addAll(getTablesInfo());

+ 48 - 0
mybatis-plus-generator/src/test/java/com/baomidou/mybatisplus/generator/config/DataSourceConfigTest.java

@@ -0,0 +1,48 @@
+package com.baomidou.mybatisplus.generator.config;
+
+import com.baomidou.mybatisplus.annotation.DbType;
+import com.baomidou.mybatisplus.generator.config.converts.PostgreSqlTypeConvert;
+import com.baomidou.mybatisplus.generator.config.querys.H2Query;
+import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
+import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;
+import org.h2.Driver;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+
+/**
+ *
+ * @author nieqiurong 2020/10/10.
+ */
+public class DataSourceConfigTest {
+
+    @Test
+    void buildTest() {
+        DataSourceConfig dataSourceConfig;
+        dataSourceConfig = new DataSourceConfig.Builder("jdbc:h2:mem:test;MODE=mysql;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE", "sa", "").build();
+        Assertions.assertNotNull(dataSourceConfig.getDbType());
+        Assertions.assertNotNull(dataSourceConfig.getConn());
+        Assertions.assertNotNull(dataSourceConfig.getTypeConvert());
+        Assertions.assertEquals(dataSourceConfig.getDbType(), DbType.H2);
+        Assertions.assertEquals(dataSourceConfig.getDbQuery().getClass(), H2Query.class);
+
+        dataSourceConfig = new DataSourceConfig.Builder("jdbc:h2:mem:test;MODE=mysql;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE", "sa", "").driver(Driver.class).build();
+        Assertions.assertNotNull(dataSourceConfig.getDbType());
+        Assertions.assertNotNull(dataSourceConfig.getConn());
+        Assertions.assertNotNull(dataSourceConfig.getTypeConvert());
+        Assertions.assertEquals(Driver.class.getName(), dataSourceConfig.getDriverName());
+        Assertions.assertEquals(dataSourceConfig.getDbType(), DbType.H2);
+        Assertions.assertEquals(dataSourceConfig.getDbQuery().getClass(), H2Query.class);
+
+        dataSourceConfig = new DataSourceConfig.Builder("jdbc:h2:mem:test;MODE=mysql;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE", "sa", "")
+            .dbType(DbType.MYSQL).dbQuery(new MySqlQuery()).schema("mp").keyWordsHandler(new MySqlKeyWordsHandler()).typeConvert(new PostgreSqlTypeConvert())
+            .driver(Driver.class).build();
+        Assertions.assertEquals(dataSourceConfig.getSchemaName(), "mp");
+        Assertions.assertEquals(dataSourceConfig.getDriverName(), Driver.class.getName());
+        Assertions.assertEquals(dataSourceConfig.getDbType(), DbType.MYSQL);
+        Assertions.assertEquals(dataSourceConfig.getDbQuery().getClass(), MySqlQuery.class);
+        Assertions.assertEquals(dataSourceConfig.getKeyWordsHandler().getClass(), MySqlKeyWordsHandler.class);
+        Assertions.assertEquals(dataSourceConfig.getTypeConvert().getClass(), PostgreSqlTypeConvert.class);
+    }
+
+}

+ 107 - 0
mybatis-plus-generator/src/test/java/com/baomidou/mybatisplus/generator/config/TemplateConfigTest.java

@@ -0,0 +1,107 @@
+package com.baomidou.mybatisplus.generator.config;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ *
+ * @author nieqiurong 2020/10/10.
+ */
+public class TemplateConfigTest {
+
+    @Test
+    void disableTest() {
+        TemplateConfig templateConfig;
+        templateConfig = new TemplateConfig().disable();
+        Assertions.assertNull(templateConfig.getController());
+        Assertions.assertNull(templateConfig.getService());
+        Assertions.assertNull(templateConfig.getServiceImpl());
+        Assertions.assertNull(templateConfig.getMapper());
+        Assertions.assertNull(templateConfig.getXml());
+        Assertions.assertNull(templateConfig.getEntity(true));
+        Assertions.assertNull(templateConfig.getEntity(false));
+
+        templateConfig = new TemplateConfig.Builder().build();
+        Assertions.assertNull(templateConfig.getController());
+        Assertions.assertNull(templateConfig.getService());
+        Assertions.assertNull(templateConfig.getServiceImpl());
+        Assertions.assertNull(templateConfig.getMapper());
+        Assertions.assertNull(templateConfig.getXml());
+        Assertions.assertNull(templateConfig.getEntity(true));
+        Assertions.assertNull(templateConfig.getEntity(false));
+
+
+        templateConfig = new TemplateConfig().disable(TemplateType.SERVICE);
+        Assertions.assertNull(templateConfig.getServiceImpl());
+        Assertions.assertNull(templateConfig.getService());
+        Assertions.assertNotNull(templateConfig.getEntity(true));
+        Assertions.assertNotNull(templateConfig.getEntity(false));
+
+        templateConfig = new TemplateConfig.Builder().all().build().disable(TemplateType.SERVICE);
+        Assertions.assertNull(templateConfig.getServiceImpl());
+        Assertions.assertNull(templateConfig.getService());
+        Assertions.assertNotNull(templateConfig.getEntity(true));
+        Assertions.assertNotNull(templateConfig.getEntity(false));
+
+        templateConfig = new TemplateConfig().disable(TemplateType.ENTITY);
+        Assertions.assertNotNull(templateConfig.getServiceImpl());
+        Assertions.assertNotNull(templateConfig.getService());
+        Assertions.assertNull(templateConfig.getEntity(true));
+        Assertions.assertNull(templateConfig.getEntity(false));
+
+        templateConfig = new TemplateConfig.Builder().all().build().disable(TemplateType.ENTITY);
+        Assertions.assertNotNull(templateConfig.getServiceImpl());
+        Assertions.assertNotNull(templateConfig.getService());
+        Assertions.assertNull(templateConfig.getEntity(true));
+        Assertions.assertNull(templateConfig.getEntity(false));
+
+    }
+
+    @Test
+    void entityTest() {
+        Assertions.assertEquals("/templates/entity.kt", new TemplateConfig().getEntity(true));
+        Assertions.assertEquals("/templates/entity.kt", new TemplateConfig.Builder().all().build().getEntity(true));
+        Assertions.assertEquals("/templates/entity.kt", new TemplateConfig.Builder().entity().build().getEntity(true));
+        Assertions.assertEquals("/templates/entity.java", new TemplateConfig().getEntity(false));
+        Assertions.assertEquals("/templates/entity.java", new TemplateConfig.Builder().all().build().getEntity(false));
+        Assertions.assertEquals("/templates/entity.java", new TemplateConfig.Builder().entity().build().getEntity(false));
+        Assertions.assertEquals("/tm/entity.kt", new TemplateConfig().setEntity("/tm/entity.java").getEntity(true));
+        Assertions.assertEquals("/tm/entity.java", new TemplateConfig().setEntity("/tm/entity.java").getEntity(false));
+        Assertions.assertEquals("/tm/entity.kt", new TemplateConfig.Builder().entity("/tm/entity.java").build().getEntity(true));
+        Assertions.assertEquals("/tm/entity.java", new TemplateConfig.Builder().entity("/tm/entity.java").build().getEntity(false));
+        Assertions.assertEquals("/tm/entity.kt", new TemplateConfig().setEntity("/tm/entity").getEntity(true));
+        Assertions.assertEquals("/tm/entity.kt", new TemplateConfig.Builder().entity("/tm/entity").build().getEntity(true));
+        Assertions.assertEquals("/tm/entity.java", new TemplateConfig().setEntity("/tm/entity").getEntity(false));
+        Assertions.assertEquals("/tm/entity.java", new TemplateConfig.Builder().entity("/tm/entity").build().getEntity(false));
+        Assertions.assertEquals("/tm/entity.kt", new TemplateConfig().setEntity("/tm/entity%s").getEntity(true));
+        Assertions.assertEquals("/tm/entity.kt", new TemplateConfig.Builder().entity("/tm/entity%s").build().getEntity(true));
+        Assertions.assertEquals("/tm/entity.java", new TemplateConfig().setEntity("/tm/entity%s").getEntity(false));
+        Assertions.assertEquals("/tm/entity.java", new TemplateConfig.Builder().entity("/tm/entity%s").build().getEntity(false));
+    }
+
+    @Test
+    void builderTest() {
+        TemplateConfig templateConfig;
+        templateConfig = new TemplateConfig.Builder().entity().service().build();
+        Assertions.assertNotNull(templateConfig.getEntity(true));
+        Assertions.assertNotNull(templateConfig.getEntity(false));
+        Assertions.assertNotNull(templateConfig.getService());
+        Assertions.assertNotNull(templateConfig.getServiceImpl());
+        Assertions.assertNull(templateConfig.getController());
+        Assertions.assertNull(templateConfig.getMapper());
+        Assertions.assertNull(templateConfig.getXml());
+
+        templateConfig = new TemplateConfig.Builder().entity("/tmp/entity").service("/tmp/service.java", "/tmp/serviceImpl.java").build();
+        Assertions.assertNotNull(templateConfig.getEntity(true));
+        Assertions.assertNotNull(templateConfig.getEntity(false));
+        Assertions.assertNotNull(templateConfig.getService());
+        Assertions.assertNotNull(templateConfig.getServiceImpl());
+        Assertions.assertNull(templateConfig.getController());
+        Assertions.assertNull(templateConfig.getMapper());
+        Assertions.assertNull(templateConfig.getXml());
+        Assertions.assertEquals("/tmp/entity.kt", templateConfig.getEntity(true));
+        Assertions.assertEquals("/tmp/entity.java", templateConfig.getEntity(false));
+        Assertions.assertEquals("/tmp/service.java", templateConfig.getService());
+        Assertions.assertEquals("/tmp/serviceImpl.java", templateConfig.getServiceImpl());
+    }
+}