/*
 * Copyright (c) 2011-2020, hubin (jobob@qq.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.baomidou.mybatisplus.generator.config;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
import com.baomidou.mybatisplus.generator.config.converts.DB2TypeConvert;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.converts.OracleTypeConvert;
import com.baomidou.mybatisplus.generator.config.converts.PostgreSqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.converts.SqlServerTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.DB2Query;
import com.baomidou.mybatisplus.generator.config.querys.MariadbQuery;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.querys.OracleQuery;
import com.baomidou.mybatisplus.generator.config.querys.PostgreSqlQuery;
import com.baomidou.mybatisplus.generator.config.querys.SqlServerQuery;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * <p>
 * 数据库配置
 * </p>
 *
 * @author YangHu
 * @since 2016/8/30
 */
@Data
@Accessors(chain = true)
public class DataSourceConfig {

    /**
     * 数据库信息查询
     */
    private IDbQuery dbQuery;
    /**
     * 数据库类型
     */
    private DbType dbType;
    /**
     * PostgreSQL schemaName
     */
    private String schemaName;
    /**
     * 类型转换
     */
    private ITypeConvert typeConvert;
    /**
     * 驱动连接的URL
     */
    private String url;
    /**
     * 驱动名称
     */
    private String driverName;
    /**
     * 数据库连接用户名
     */
    private String username;
    /**
     * 数据库连接密码
     */
    private String password;

    public IDbQuery getDbQuery() {
        if (null == dbQuery) {
            switch (getDbType()) {
                case ORACLE:
                    dbQuery = new OracleQuery();
                    break;
                case SQL_SERVER:
                    dbQuery = new SqlServerQuery();
                    break;
                case POSTGRE_SQL:
                    dbQuery = new PostgreSqlQuery();
                    break;
                case DB2:
                    dbQuery = new DB2Query();
                    break;
                case MARIADB:
                    dbQuery = new MariadbQuery();
                    break;
                default:
                    // 默认 MYSQL
                    dbQuery = new MySqlQuery();
                    break;
            }
        }
        return dbQuery;
    }

    /**
     * 判断数据库类型
     *
     * @return 类型枚举值
     */
    public DbType getDbType() {
        if (null == dbType) {
            if (driverName.contains("mysql")) {
                dbType = DbType.MYSQL;
            } else if (driverName.contains("oracle")) {
                dbType = DbType.ORACLE;
            } else if (driverName.contains("postgresql")) {
                dbType = DbType.POSTGRE_SQL;
            } else if (driverName.contains("db2")) {
                dbType = DbType.DB2;
            } else if (driverName.contains("mariadb")) {
                dbType = DbType.MARIADB;
            } else {
                throw ExceptionUtils.mpe("Unknown type of database!");
            }
        }
        return dbType;
    }

    public ITypeConvert getTypeConvert() {
        if (null == typeConvert) {
            switch (getDbType()) {
                case ORACLE:
                    typeConvert = new OracleTypeConvert();
                    break;
                case SQL_SERVER:
                    typeConvert = new SqlServerTypeConvert();
                    break;
                case POSTGRE_SQL:
                    typeConvert = new PostgreSqlTypeConvert();
                    break;
                case DB2:
                    typeConvert = new DB2TypeConvert();
                    break;
                case MARIADB:
                    typeConvert = new MySqlTypeConvert();
                    break;
                default:
                    // 默认 MYSQL
                    typeConvert = new MySqlTypeConvert();
                    break;
            }
        }
        return typeConvert;
    }

    /**
     * 创建数据库连接对象
     *
     * @return Connection
     */
    public Connection getConn() {
        Connection conn = null;
        try {
            Class.forName(driverName);
            conn = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
}

@@ -672,98 +672,8 @@
 g;%0A%0A
-import java.sql.Connection;%0Aimport java.sql.DriverManager;%0Aimport java.sql.SQLException;%0A%0A
 impo
@@ -838,30 +838,17 @@
 onverts.
-DB2TypeConvert
+*
 ;%0Aimport
@@ -894,33 +894,16 @@
 fig.
-converts.MySqlTypeConvert
+querys.*
 ;%0Aim
@@ -911,76 +911,19 @@
 ort 
-com.baomidou.mybatisplus.generator.config.converts.OracleTypeConvert
+lombok.Data
 ;%0Aim
@@ -931,637 +931,127 @@
 ort 
-com.baomidou.mybatisplus.generator.config.converts.PostgreSqlTypeConvert;%0Aimport com.baomidou.mybatisplus.generator.config.converts.SqlServerTypeConvert
+lombok.experimental.Accessors
 ;%0A
+%0A
 import 
-com.baomidou.mybatisplus.generator.config.querys.DB2Query;%0Aimport com.baomidou.mybatisplus.generator.config.querys.MariadbQuery;%0Aimport com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;%0Aimport com.baomidou.mybatisplus.generator.config.querys.OracleQuery;%0Aimport com.baomidou.mybatisplus.generator.config.querys.PostgreSqlQuery;%0Aimport com.baomidou.mybatisplus.generator.config.querys.SqlServerQuery;%0A%0Aimport lombok.Data;%0Aimport lombok.experimental.Accessors
+java.sql.Connection;%0Aimport java.sql.DriverManager;%0Aimport java.sql.SQLException
 ;%0A%0A/
@@ -2912,24 +2912,127 @@
 OSTGRE_SQL;%0A
+            %7D else if (driverName.contains(%22sqlserver%22)) %7B%0A                dbType = DbType.SQL_SERVER;%0A
             

添加 sqlserver 判断