package cn.huge.module.config;
|
|
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.ibatis.session.SqlSessionFactory;
|
import org.mybatis.spring.SqlSessionFactoryBean;
|
import org.mybatis.spring.SqlSessionTemplate;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.jdbc.DataSourceBuilder;
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Primary;
|
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
import org.springframework.core.io.support.ResourcePatternResolver;
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
import javax.sql.DataSource;
|
import java.io.IOException;
|
|
@Configuration
|
@Slf4j
|
public class DorisDataSourceConfig {
|
|
|
// /**
|
// * 创建Doris数据源的Bean
|
// * 该方法使用Spring的@Bean注解来定义一个Bean,使用@ConfigurationProperties注解来绑定配置文件中的属性
|
// * 这里特别注明了数据源的前缀,以便Spring能够正确地映射配置属性
|
// *
|
// * @return DataSource 返回一个数据源实例,该实例的配置由配置文件中对应的前缀属性指定
|
// */
|
// @Bean("dorisDataSource")
|
// @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.doris")
|
// public DataSource createDorisDataSource(){
|
// return DataSourceBuilder.create().build();
|
// }
|
//
|
//
|
// /**
|
// * 创建并配置一个SqlSessionFactory bean
|
// * 该方法使用特定的DataSource来构建SqlSessionFactory,主要用于与Doris数据库交互
|
// *
|
// * @param dataSource 数据源,用于连接Doris数据库
|
// * @return SqlSessionFactory 用于创建SqlSession的工厂
|
// * @throws Exception 当创建SqlSessionFactory过程中出现错误时抛出
|
// */
|
// @Bean("dorisSqlSessionFactory")
|
// public SqlSessionFactory dorisSqlSessionFactory(@Qualifier("dorisDataSource") DataSource dataSource) throws Exception {
|
// // 实例化SqlSessionFactoryBean,这里使用的是MybatisSqlSessionFactoryBean,可能是因为它提供了更多配置MyBatis的选项
|
// MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
|
//
|
// // 设置数据源,这是与数据库交互的基础
|
// bean.setDataSource(dataSource);
|
//
|
// // 使用PathMatchingResourcePatternResolver来加载类路径下所有匹配的资源文件
|
// ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
|
// // 加载Mapper XML文件
|
// String xmlPath = "classpath*:cn/huge/module/*/dao/mapper/doris/*.xml";
|
// try {
|
// // 简化路径模式为classpath:mapper/doris/*.xml,避免不必要的多位置搜索
|
// Resource[] resources = resourceResolver.getResources(xmlPath);
|
//
|
// // 检查是否找到了任何Mapper XML文件
|
// if (resources.length == 0) {
|
// throw new IllegalStateException("No Mapper XML files found at " + xmlPath);
|
// }
|
// // 打印找到的Mapper XML文件路径,仅当处于DEBUG级别时才会输出此信息
|
// for (Resource resource : resources) {
|
// log.debug("Found Mapper XML file: {}", resource.getURL());
|
// }
|
// // 设置Mapper XML文件的位置
|
// bean.setMapperLocations(resources);
|
// } catch (IOException e) {
|
// throw new RuntimeException("Failed to load Mapper XML files from " + xmlPath, e);
|
// }
|
//
|
// // 返回配置好的SqlSessionFactory对象
|
// return bean.getObject();
|
// }
|
//
|
//
|
// // 配置事务管理器
|
// /**
|
// * 创建并配置DataSourceTransactionManager实例
|
// * 该实例用于管理数据库事务,确保数据一致性和完整性
|
// *
|
// * @param dataSource 数据源,通过Qualifier注解指定使用名为"dorisDataSource"的DataSource实例
|
// * @return DataSourceTransactionManager实例,用于管理数据库事务
|
// */
|
// @Bean(name = "dorisTransactionManager")
|
// public DataSourceTransactionManager dorisTransactionManager(@Qualifier("dorisDataSource") DataSource dataSource) {
|
// // 创建DataSourceTransactionManager实例并设置数据源
|
// DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dataSource);
|
//
|
// // 配置事务在提交失败时自动回滚
|
// dataSourceTransactionManager.setRollbackOnCommitFailure(true);
|
//
|
// // 配置事务在参与方失败时全局回滚
|
// dataSourceTransactionManager.setGlobalRollbackOnParticipationFailure(true);
|
//
|
// // 返回配置好的DataSourceTransactionManager实例
|
// return dataSourceTransactionManager;
|
// }
|
//
|
//
|
// /**
|
// * 创建并配置一个名为"dorisSqlSessionTemplate"的SqlSessionTemplate bean
|
// * 该bean用于执行与Doris数据库交互的操作
|
// *
|
// * @param sqlSessionFactory 一个SqlSessionFactory实例,用于创建SqlSessionTemplate
|
// * 该实例通过Qualifier注解从名为"dorisSqlSessionFactory"的bean中获取
|
// * @return 返回一个配置好的SqlSessionTemplate实例,用于执行SQL操作
|
// */
|
// @Bean("dorisSqlSessionTemplate")
|
// public SqlSessionTemplate dorisSqlSessionTemplate(@Qualifier("dorisSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
|
// return new SqlSessionTemplate(sqlSessionFactory);
|
// }
|
}
|