/**
* 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.exceptions.MybatisPlusException;
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.rules.DbType;
/**
* <p>
* 数据库配置
* </p>
*
* @author YangHu
* @since 2016/8/30
*/
public class DataSourceConfig {
/**
* 数据库类型
*/
private DbType dbType;
/**
* 类型转换
*/
private ITypeConvert typeConvert;
/**
* 驱动连接的URL
*/
private String url;
/**
* 驱动名称
*/
private String driverName;
/**
* 数据库连接用户名
*/
private String username;
/**
* 数据库连接密码
*/
private String password;
/**
* 判断数据库类型
*
* @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 {
throw new MybatisPlusException("Unknown type of database!");
}
}
return dbType;
}
public void setDbType(DbType dbType) {
this.dbType = 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;
default:
// 默认 MYSQL
typeConvert = new MySqlTypeConvert();
break;
}
}
return typeConvert;
}
public void setTypeConvert(ITypeConvert typeConvert) {
this.typeConvert = 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;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}