|
@@ -14,58 +14,148 @@ import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
+import org.springframework.context.annotation.Primary;
|
|
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
|
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
|
import org.springframework.transaction.PlatformTransactionManager;
|
|
import org.springframework.transaction.PlatformTransactionManager;
|
|
|
|
|
+import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|
|
|
|
|
|
|
import javax.sql.DataSource;
|
|
import javax.sql.DataSource;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 从数据源配置
|
|
|
|
|
|
|
+ * Bdshdb 数据源配置类
|
|
|
|
|
+ *
|
|
|
|
|
+ * 该配置类用于配置项目中第二个数据源(bdshdb数据库),实现多数据源支持。
|
|
|
|
|
+ * 主要用于中煤集团定制化业务模块的数据访问。
|
|
|
|
|
+ *
|
|
|
|
|
+ * 功能特性:
|
|
|
|
|
+ * 1. 使用 Druid 连接池管理数据库连接
|
|
|
|
|
+ * 2. 集成 MyBatis-Plus 框架,提供分页、性能优化等功能
|
|
|
|
|
+ * 3. 支持驼峰命名转换和SQL日志输出
|
|
|
|
|
+ * 4. 独立的事务管理机制
|
|
|
|
|
+ * 5. 扫描指定包路径下的Mapper接口
|
|
|
|
|
+ *
|
|
|
|
|
+ * 使用说明:
|
|
|
|
|
+ * - 配置文件中需要以 spring.datasource.bdshdb 为前缀配置数据库连接信息
|
|
|
|
|
+ * - Mapper接口需放在 com.linkwechat.coal.modules.bdshdb.*.mapper 包下
|
|
|
|
|
+ * - XML映射文件需放在 mapper/modules/bdshdb 目录下
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author LinkWeChat
|
|
|
|
|
+ * @since 1.0.0
|
|
|
*/
|
|
*/
|
|
|
@Configuration
|
|
@Configuration
|
|
|
-@MapperScan(basePackages = {"com.linkwechat.coal.modules.bdshdb.*.mapper"}, sqlSessionFactoryRef = "bdshdbSqlSessionFactory")
|
|
|
|
|
|
|
+@EnableTransactionManagement // 启用事务管理
|
|
|
|
|
+@MapperScan(
|
|
|
|
|
+ basePackages = {"com.linkwechat.coal.modules.bdshdb.*.mapper"},
|
|
|
|
|
+ sqlSessionFactoryRef = "bdshdbSqlSessionFactory"
|
|
|
|
|
+)
|
|
|
public class BdshdbDataSourceConfig {
|
|
public class BdshdbDataSourceConfig {
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 配置 Bdshdb 数据源
|
|
|
|
|
+ *
|
|
|
|
|
+ * 使用 Druid 连接池创建数据源,通过 @ConfigurationProperties 注解
|
|
|
|
|
+ * 自动绑定 application.yml 中的 spring.datasource.bdshdb 配置项。
|
|
|
|
|
+ *
|
|
|
|
|
+ * 配置示例:
|
|
|
|
|
+ * spring:
|
|
|
|
|
+ * datasource:
|
|
|
|
|
+ * bdshdb:
|
|
|
|
|
+ * url: jdbc:mysql://localhost:3306/bdshdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
|
|
|
|
|
+ * username: root
|
|
|
|
|
+ * password: password
|
|
|
|
|
+ * driver-class-name: com.mysql.cj.jdbc.Driver
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return 返回配置好的 Druid 数据源实例
|
|
|
|
|
+ */
|
|
|
@Bean(name = "bdshdbDataSource")
|
|
@Bean(name = "bdshdbDataSource")
|
|
|
- @ConfigurationProperties(prefix = "spring.datasource.bdshdb")
|
|
|
|
|
|
|
+ @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.bdshdb")
|
|
|
public DataSource bdshdbDataSource() {
|
|
public DataSource bdshdbDataSource() {
|
|
|
return DruidDataSourceBuilder.create().build();
|
|
return DruidDataSourceBuilder.create().build();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 配置 MyBatis-Plus 的 SqlSessionFactory
|
|
|
|
|
+ *
|
|
|
|
|
+ * 创建 MyBatis 的核心工厂类,配置 MyBatis-Plus 的相关功能,
|
|
|
|
|
+ * 包括分页插件、驼峰命名转换、SQL 日志输出等。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param datasource 注入名为 bdshdbDataSource 的数据源
|
|
|
|
|
+ * @return 配置好的 SqlSessionFactory 实例
|
|
|
|
|
+ * @throws Exception 配置过程中可能抛出的异常
|
|
|
|
|
+ */
|
|
|
@Bean(name = "bdshdbSqlSessionFactory")
|
|
@Bean(name = "bdshdbSqlSessionFactory")
|
|
|
- public SqlSessionFactory bdshdbSqlSessionFactory(@Qualifier("bdshdbDataSource") DataSource datasource)
|
|
|
|
|
- throws Exception {
|
|
|
|
|
|
|
+ public SqlSessionFactory bdshdbSqlSessionFactory(
|
|
|
|
|
+ @Qualifier("bdshdbDataSource") DataSource datasource
|
|
|
|
|
+ ) throws Exception {
|
|
|
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
|
|
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
|
|
|
- //configuration配置bean
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 配置 MyBatis 核心参数
|
|
|
MybatisConfiguration configuration = new MybatisConfiguration();
|
|
MybatisConfiguration configuration = new MybatisConfiguration();
|
|
|
- configuration.setMapUnderscoreToCamelCase(true);
|
|
|
|
|
- configuration.setCacheEnabled(false);
|
|
|
|
|
- // 配置打印sql语句s
|
|
|
|
|
- configuration.setLogImpl(StdOutImpl.class);
|
|
|
|
|
- // 添加自定义SQL注入
|
|
|
|
|
|
|
+ configuration.setMapUnderscoreToCamelCase(true); // 开启驼峰命名转换(user_name -> userName)
|
|
|
|
|
+ configuration.setCacheEnabled(false); // 关闭二级缓存,避免多数据源缓存混乱
|
|
|
|
|
+ configuration.setLogImpl(StdOutImpl.class); // 配置 SQL 输出到控制台,方便开发调试
|
|
|
bean.setConfiguration(configuration);
|
|
bean.setConfiguration(configuration);
|
|
|
|
|
|
|
|
- //插件对象
|
|
|
|
|
|
|
+ // 配置 MyBatis-Plus 插件
|
|
|
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
|
|
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
|
|
|
-
|
|
|
|
|
- //分页插件
|
|
|
|
|
|
|
+ // 添加分页插件,自动实现物理分页
|
|
|
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
|
|
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
|
|
|
|
|
+
|
|
|
|
|
+ // 设置数据源
|
|
|
bean.setDataSource(datasource);
|
|
bean.setDataSource(datasource);
|
|
|
- // 设置mybatis的xml所在位置
|
|
|
|
|
- bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/modules/bdshdb/*/*.xml"));
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 设置 Mapper XML 文件位置
|
|
|
|
|
+ // 扫描 classpath:mapper/modules/bdshdb 目录下所有子目录的 XML 文件
|
|
|
|
|
+ bean.setMapperLocations(
|
|
|
|
|
+ new PathMatchingResourcePatternResolver()
|
|
|
|
|
+ .getResources("classpath:mapper/modules/bdshdb/*/*.xml")
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ // 设置插件
|
|
|
bean.setPlugins(mybatisPlusInterceptor);
|
|
bean.setPlugins(mybatisPlusInterceptor);
|
|
|
|
|
+
|
|
|
return bean.getObject();
|
|
return bean.getObject();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 配置 SqlSessionTemplate
|
|
|
|
|
+ *
|
|
|
|
|
+ * SqlSessionTemplate 是 MyBatis-Spring 的核心类,提供了线程安全的 SqlSession 实现。
|
|
|
|
|
+ * 它确保每个线程都能获得独立的 SqlSession 实例,避免了并发问题。
|
|
|
|
|
+ *
|
|
|
|
|
+ * 使用场景:
|
|
|
|
|
+ * - 在 Service 层中注入使用
|
|
|
|
|
+ * - 替代原生的 MyBatis SqlSession,实现线程安全
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param sessionFactory 注入名为 bdshdbSqlSessionFactory 的 SqlSessionFactory
|
|
|
|
|
+ * @return 配置好的 SqlSessionTemplate 实例
|
|
|
|
|
+ */
|
|
|
@Bean("bdshdbSqlSessionTemplate")
|
|
@Bean("bdshdbSqlSessionTemplate")
|
|
|
public SqlSessionTemplate bdshdbSqlSessionTemplate(
|
|
public SqlSessionTemplate bdshdbSqlSessionTemplate(
|
|
|
- @Qualifier("bdshdbSqlSessionFactory") SqlSessionFactory sessionFactory) {
|
|
|
|
|
|
|
+ @Qualifier("bdshdbSqlSessionFactory") SqlSessionFactory sessionFactory
|
|
|
|
|
+ ) {
|
|
|
return new SqlSessionTemplate(sessionFactory);
|
|
return new SqlSessionTemplate(sessionFactory);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 配置事务管理器
|
|
|
|
|
+ *
|
|
|
|
|
+ * 为 Bdshdb 数据源配置独立的事务管理器,确保该数据源的数据库操作
|
|
|
|
|
+ * 能够正确地进行事务管理(提交、回滚等)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * 注意事项:
|
|
|
|
|
+ * - 在使用 @Transactional 注解时,需要指定事务管理器:
|
|
|
|
|
+ * @Transactional("bdshdbTransactionManager")
|
|
|
|
|
+ * - 每个数据源都需要独立的事务管理器,避免事务混乱
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param dataSource 注入名为 bdshdbDataSource 的数据源
|
|
|
|
|
+ * @return 配置好的事务管理器实例
|
|
|
|
|
+ */
|
|
|
@Bean("bdshdbTransactionManager")
|
|
@Bean("bdshdbTransactionManager")
|
|
|
- public PlatformTransactionManager bdshdbTransactionManager(@Qualifier("bdshdbDataSource") DataSource dataSource) {
|
|
|
|
|
|
|
+ public PlatformTransactionManager bdshdbTransactionManager(
|
|
|
|
|
+ @Qualifier("bdshdbDataSource") DataSource dataSource
|
|
|
|
|
+ ) {
|
|
|
return new DataSourceTransactionManager(dataSource);
|
|
return new DataSourceTransactionManager(dataSource);
|
|
|
}
|
|
}
|
|
|
|
|
|