package com.kedacom.moserver.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.kedacom.moserver.utils.KdvDecryptUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
/**
* @author yanyibo
* @date 2022/11/21
* @description
*/
@Component
public class ConnectionConfiguration {
private static final String DIALECT_GAUSS = "gauss";
private static final String DIALECT_HIGHGO = "highgo";
private static final String DIALECT_MYSQL = "mysql";
/**
* 数据库方言
*/
@Value("${db.dialect}")
private String dialect;
/**
* mysql连接参数
*/
@Value("${mysql.datasource.driver-class-name}")
private String mysqlDriver;
@Value("${mysql.datasource.url}")
private String mysqlUrl;
@Value("${mysql.datasource.username}")
private String mysqlUsername;
@Value("${mysql.datasource.password}")
private String mysqlPassword;
/**
* gauss连接参数
*/
@Value("${gauss.datasource.driver-class-name}")
private String gaussDriver;
@Value("${gauss.datasource.url}")
private String gaussUrl;
@Value("${gauss.datasource.username}")
private String gaussUsername;
@Value("${gauss.datasource.password}")
private String gaussPassword;
/**
* highgo连接参数
*/
@Value("${highgo.datasource.driver-class-name}")
private String highgoDriver;
@Value("${highgo.datasource.url}")
private String highgoUrl;
@Value("${highgo.datasource.username}")
private String highgoUsername;
@Value("${highgo.datasource.password}")
private String highgoPassword;
@Bean
public DataSource druidDataSource() throws Exception {
DruidDataSource dataSource = new DruidDataSource();
if (DIALECT_MYSQL.equals(dialect)) {
dataSource.setDriverClassName(mysqlDriver);
dataSource.setUrl(mysqlUrl);
dataSource.setUsername(mysqlUsername);
dataSource.setPassword(KdvDecryptUtils.kdvDecrypt(mysqlPassword));
return dataSource;
}
if (DIALECT_GAUSS.equals(dialect)) {
dataSource.setDriverClassName(gaussDriver);
dataSource.setUrl(gaussUrl);
dataSource.setUsername(gaussUsername);
dataSource.setPassword(KdvDecryptUtils.kdvDecrypt(gaussPassword));
return dataSource;
}
if (DIALECT_HIGHGO.equals(dialect)) {
dataSource.setDriverClassName(highgoDriver);
dataSource.setUrl(highgoUrl);
dataSource.setUsername(highgoUsername);
dataSource.setPassword(KdvDecryptUtils.kdvDecrypt(highgoPassword));
return dataSource;
}
throw new Exception("数据库方言配置异常, db.dialect: " + dialect);
}
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
// 创建 SqlSessionFactoryBean 对象
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
// 设置数据源
factoryBean.setDataSource(dataSource);
// 设置 mapper 映射文件路径
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
String mapperPathStr = "classpath:mappers/*.xml";
if (DIALECT_GAUSS.equals(dialect)) {
mapperPathStr = mapperPathStr.replace("*.xml", DIALECT_GAUSS + "/*.xml");
} else if (DIALECT_HIGHGO.equals(dialect)) {
mapperPathStr = mapperPathStr.replace("*.xml", DIALECT_HIGHGO + "/*.xml");
}
factoryBean.setMapperLocations(resolver.getResources(mapperPathStr));
// 设置 VFS
factoryBean.setVfs(SpringBootVFS.class);
// 返回 SqlSessionFactory 对象
return factoryBean.getObject();
}
}
数据源依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.5</version>
</dependency>