shishanglian

导航

Hikari 数据源 JavaConfig





原文
https://blog.csdn.net/qq_37778018/article/details/124745012

spring:
  datasource:
    #First data source
    pgsql:
      driverClassName: org.postgresql.Driver
      jdbcUrl: jdbc:postgresql://192.168.1.23:5432/databasepgsql?useUnicode=true&characterEncoding=utf8
      type: com.zaxxer.hikari.HikariDataSource
      username: postgres
      password: postgres
      initialSize: 10
      maxActive: 2000
      minIdle: 1
      maximumPoolSize: 200
      autoCommit: true
      poolName: HikariPool_pgsql
      maxLifetime: 1800000
      connectionTestQuery: SELECT 1
    #Second data source
    mysql:
      driverClassName: com.mysql.cj.jdbc.Driver
      jdbcUrl: jdbc:mysql://127.0.0.1:3306/databasemysql?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true&autoReconnect=true
      type: com.zaxxer.hikari.HikariDataSource
      username: root
      password: 123456
      initialSize: 10
      maxActive: 2000
      minIdle: 1
      maximumPoolSize: 200
      autoCommit: true
      poolName: HikariPool_mysql
      maxLifetime: 1800000
      connectionTestQuery: SELECT 1



      javaconfig Config 类,Config Hikari连接多data source


      @Configuration
//这里Setting Scan daointerface 了,启动类与daointerface 就不用在Config mapperScan Annotation
@MapperScan(basePackages = "com.aaa.bbb.dao.pgsqlsource", sqlSessionFactoryRef = "pgSqlSessionFactory")
public class HikariPgSqlConfig {
 
    /**
     * @ConfigurationProperties 读取yml中的Config 参数映射成为一个Object
     */
    @Bean(name = "pgSqlDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.pgsql")
    public HikariDataSource pgSqlDateSource() {
        return new HikariDataSource();
    }
 
   
    @Bean(name = "pgSqlSessionFactory")
    @Primary
    public SqlSessionFactory chSqlSessionFactory(@Qualifier("pgSqlDataSource") DataSource datasource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        //-----多Path 是Scan
        //Config xmlScan Path
        //List<String> resourcePatterns=new ArrayList<>();
        //resourcePatterns.add("classpath*:mapper/pgsqlmapper/*.xml");
        //resourcePatterns.add("classpath*:mapper/pgsqlmapper/*/*.xml");
        //Set<Resource> resources=new LinkedHashSet<>(resourcePatterns.size());
        //for (String resourcePattern : resourcePatterns) {
        //    Resource[] resource = new PathMatchingResourcePatternResolver().getResources(resourcePattern);
        //    resources.addAll(Arrays.asList(resource));
        //}
        //Resource[] resourcesArr=new Resource[resources.size()];
        //mybatisScan xml所在location
        //bean.setMapperLocations(resources.toArray(resourcesArr));
 
        //-----单Path 是Scan
        //mybatisScan xml所在location
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/pgsqlmapper/*/*.xml"));
        return bean.getObject();
    }
 
    @Bean("pgSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate chSqlSessionTemplate(@Qualifier("pgSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
 
    //不配值这个bean,@TransactionAnnotation 可能失效
    //使用时 @Transactional(value = "pgSqlTransactionManager",rollbackFor = {Exception.class, RuntimeException.class})
    @Bean("pgSqlTransactionManager")
    public TransactionManager mysqlTransactionManager(){
        return new JdbcTransactionManager(pgSqlDateSource());
    }
}


@Configuration
//这里Setting Scan daointerface 了,启动类与daointerface 就不用在Config mapperScan Annotation
@MapperScan(basePackages = "com.aaa.bbb.dao.mysqlsource", sqlSessionFactoryRef = "mySqlSessionFactory")
public class HikariMySqlConfig {
 
    /**
     * @ConfigurationProperties 读取yml中的Config 参数映射成为一个Object
     */
    @Bean(name = "mySqlDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.mysql")
    public HikariDataSource mySqlDateSource() {
        return new HikariDataSource();
    }
 
    @Bean(name = "mySqlSessionFactory")
    public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mySqlDataSource") DataSource datasource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        //-----多Path 是Scan
        //Config xmlScan Path
        //List<String> resourcePatterns=new ArrayList<>();
        //resourcePatterns.add("classpath*:mapper/mysqlmapper/*.xml");
        //resourcePatterns.add("classpath*:mapper/mysqlmapper/*/*.xml");
        //Set<Resource> resources=new LinkedHashSet<>(resourcePatterns.size());
        //for (String resourcePattern : resourcePatterns) {
        //    Resource[] resource = new PathMatchingResourcePatternResolver().getResources(resourcePattern);
        //    resources.addAll(Arrays.asList(resource));
        //}
        //Resource[] resourcesArr=new Resource[resources.size()];
        //mybatisScan xml所在location
        //bean.setMapperLocations(resources.toArray(resourcesArr));
 
        //-----单Path 是Scan
        //mybatisScan xml所在location
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/mysqlmapper/*.xml"));
        return bean.getObject();
    }
 
    @Bean("mySqlSessionTemplate")
    public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mySqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
 
    //不配值这个bean,@TransactionAnnotation 可能失效
    //使用时 @Transactional(value = "mySqlTransactionManager",rollbackFor = {Exception.class, RuntimeException.class})
    @Bean("mySqlTransactionManager")
    public TransactionManager mysqlTransactionManager(){
        return new JdbcTransactionManager(mySqlDateSource());
    }
}

yyyy-MM-dd HH:mm:ss [http-nio-8081-exec-n] INFO  com.zaxxer.hikari.HikariDataSource : HikariPool_pgsql - Starting...
yyyy-MM-dd HH:mm:ss [http-nio-8081-exec-n] INFO  com.zaxxer.hikari.HikariDataSource : HikariPool_pgsql - Start completed.
yyyy-MM-dd HH:mm:ss [http-nio-8081-exec-n] INFO  com.zaxxer.hikari.HikariDataSource : HikariPool_mysql - Starting...
yyyy-MM-dd HH:mm:ss [http-nio-8081-exec-n] INFO  com.zaxxer.hikari.HikariDataSource : HikariPool_mysql - Start completed.

posted on 2022-10-30 17:53  嘉合  阅读(171)  评论(0)    收藏  举报