案例: springboot 同时集成hive和mysql

1.配置maven( pom.xml文件 ):

   1.1 集成hive:


        
            org.apache.hive
            hive-jdbc
            1.1.0
            
                
                    org.apache.httpcomponents
                    httpcore
                
            
        
        
            org.apache.hadoop
            hadoop-common
            
                
                    log4j
                    log4j
                
                
                    org.slf4j
                    slf4j-log4j12
                
                
                    org.apache.httpcomponents
                    httpclient
                
                
                    com.google.code.gson
                    gson
                
            
            2.7.3
        

   1.2 集成mysql:

 
        
            com.alibaba
            druid-spring-boot-starter
            1.1.10
        
        
            mysql
            mysql-connector-java
            5.1.46
            
        

2.配置数据源(.yml文件)

   2.1 hive 数据源:

#hive数据库配置
hive:
  datasource:
    druid:
      username: hive
      password: hive
      driverClassName: org.apache.hive.jdbc.HiveDriver
      url: jdbc:hive2://IP:端口/default
      initialSize: 10
      maxActive: 50
      removeAbandoned: true
      removeAbandonedTimeout: 60
      maxWait: 60000

 2.2 mysql 数据源:

mysql:
  datasource:
    druid:
      url: jdbc:mysql://IP:端口/数据库名称?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=UTC&useSSL=false
      username: 用户名
      password: 密码
      driver-class-name: com.mysql.jdbc.Driver
      type: com.alibaba.druid.pool.DruidDataSource
      #配置初始化大小/最小/最大
      initial-size: 1
      min-idle: 1
      max-active: 20
      #获取连接等待超时时间
      max-wait: 60000
      #间隔多久进行一次检测,检测需要关闭的空闲连接
      time-between-eviction-runs-millis: 60000
      #一个连接在池中最小生存的时间
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 'x'
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      pool-prepared-statements: false
      max-pool-prepared-statement-per-connection-size: 20

3.编写多数据源代码:

  3.1 hive 数据源连接代码:

@Configuration
public class HiveDataSourceConfig {

    @Bean(name = "hiveDataSource")
    @ConfigurationProperties(prefix = "hive.datasource.druid")//和yml中的配置项,呼应
    public DataSource dataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "hiveJdbcTemplate")
    public JdbcTemplate getJdbcTemplate(@Qualifier("hiveDataSource") DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }

    /**
     * 获取hive连接
     */
    public Connection getHiveConnection() throws SQLException {
        return this.dataSource().getConnection();
    }

}

   3.2 mysql 数据源连接代码:

@Configuration
@MapperScan(basePackages = "com.web.dao", sqlSessionTemplateRef = "mySqlSqlSessionTemplate" ,sqlSessionFactoryRef = "mySqlSqlSessionFactory")
public class MySqlDataSourceConfig {

    @Primary
    @Bean(name = "mySqlDataSource")
    @ConfigurationProperties(prefix = "mysql.datasource.druid")
    public DataSource mySqlDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "mySqlTransactionManager")
    public DataSourceTransactionManager mySqlTransactionManager(@Qualifier("mySqlDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }


    @Bean
    @ConfigurationProperties(prefix = "mybatis.configuration")//需要和yml中的mybatis配置对应
    public org.apache.ibatis.session.Configuration configuration(){
        return new org.apache.ibatis.session.Configuration();
    }

    @Primary
    @Bean(name = "mySqlSqlSessionFactory")
    public SqlSessionFactory mySqlSqlSessionFactory(@Qualifier("mySqlDataSource") DataSource dataSource,org.apache.ibatis.session.Configuration configuration) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setConfiguration(configuration);
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "mySqlSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate mySqlSqlSessionTemplate(@Qualifier("mySqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

  3.2中需要补充mybatis的扫描配置:

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.web.model
  configuration:
    log-impl: org.apache.ibatis.logging.log4j.Log4jImpl
    map-underscore-to-camel-case: true

 

你可能感兴趣的:(spring,boot集成系列)