content: package com.alibaba.sreworks.dataset.common;
  
  import com.alibaba.sreworks.dataset.common.properties.ApplicationProperties;
  import com.zaxxer.hikari.HikariDataSource;
  import org.apache.ibatis.session.SqlSessionFactory;
  import org.mybatis.spring.SqlSessionFactoryBean;
  import org.mybatis.spring.SqlSessionTemplate;
  import org.mybatis.spring.annotation.MapperScan;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Qualifier;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  
  import javax.sql.DataSource;
  
  /**
   * @author: fangzong.lyj@alibaba-inc.com
   * @date: 2021/09/15 10:46
   */
  
  @Configuration
  @MapperScan(basePackages = {"com.alibaba.sreworks.dataset.domain.pmdb"}, sqlSessionFactoryRef = "sqlSessionFactory2")
  public class PmdbSourceConfig {
  
      @Autowired
      ApplicationProperties properties;
  
      @Bean(name = "dataSource2")
      public DataSource dataSource() {
          HikariDataSource dataSource = new HikariDataSource();
          dataSource.setDriverClassName(properties.getDriverClassName());
          dataSource.setJdbcUrl(properties.getPmdbUrl());
          dataSource.setUsername(properties.getPmdbUsername());
          dataSource.setPassword(properties.getPmdbPassword());
          return dataSource;
      }
  
      @Bean(name = "sqlSessionFactory2")
      public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource2") DataSource dataSource) throws Exception {
          SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
          bean.setDataSource(dataSource);
          bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/pmdb/*.xml"));
          return bean.getObject();
      }
  
      @Bean(name = "transactionManager2")
      public DataSourceTransactionManager transactionManager(@Qualifier("dataSource2") DataSource dataSource) {
          return new DataSourceTransactionManager(dataSource);
      }
  
      @Bean(name = "sqlSessionTemplate2")
      public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory) {
          return new SqlSessionTemplate(sqlSessionFactory);
      }
  }
  