springcloud 整合 druid 阿里的数据库连接池

740阅读 0评论2022-06-06 fhadmin
分类:Java


点击(此处)折叠或打开


  1.  1. 配置 pom

  2.      <!-- druid 数据库连接池 -->
  3.      <dependency>
  4.             <groupId>com.alibaba</groupId>
  5.             <artifactId>druid</artifactId>
  6.             <version>1.1.12</version>
  7.             <exclusions>
  8.          <exclusion>
  9.          <groupId>com.alibaba</groupId>
  10.          <artifactId>jconsole</artifactId>
  11.          </exclusion>
  12.          <exclusion>
  13.          <groupId>com.alibaba</groupId>
  14.          <artifactId>tools</artifactId>
  15.          </exclusion>
  16.          </exclusions>
  17.         </dependency>

  18. 2. 配置 application.properties

  19. #druid连接池
  20. spring.datasource.type: com.alibaba.druid.pool.DruidDataSource
  21. #最大活跃数
  22. spring.datasource.maxActive: 20
  23. #初始化数量
  24. spring.datasource.initialSize: 1
  25. #最大连接等待超时时间
  26. spring.datasource.maxWait: 60000
  27. #打开PSCache,并且指定每个连接PSCache的大小
  28. spring.datasource.poolPreparedStatements: true
  29. spring.datasource.maxPoolPreparedStatementPerConnectionSize: 20
  30. #通过connectionProperties属性来打开mergeSql功能;慢SQL记录
  31. #connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
  32. spring.datasource.minIdle: 1
  33. spring.datasource.timeBetweenEvictionRunsMillis: 60000
  34. spring.datasource.minEvictableIdleTimeMillis: 300000
  35. spring.datasource.validationQuery: select 1 from dual
  36. spring.datasource.testWhileIdle: true
  37. spring.datasource.testOnBorrow: false
  38. spring.datasource.testOnReturn: false
  39. #配置监控统计拦截的filters,去掉后监控界面sql将无法统计,'wall'用于防火墙
  40. filters: stat, wall, log4j

  41. 3.Config 配置类

  42.  
  43.  
  44. /**
  45.  * 说明:第一数据源配置
  46.  * 作者:FH
  47.  * from:fhadmin.cn
  48.  */
  49. @Configuration
  50. @MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory") //扫描 Mapper 接口并容器管理
  51. public class MasterDataSourceConfig {
  52.  
  53.     static final String PACKAGE = "org.fh.mapper.dsno1";                                //master 目录
  54.     static final String MAPPER_LOCATION = "classpath:mybatis/dsno1/*/*.xml";            //扫描的 xml 目录
  55.     static final String CONFIG_LOCATION = "classpath:mybatis/dsno1/mybatis-config.xml"; //自定义的mybatis config 文件位置
  56.     static final String TYPE_ALIASES_PACKAGE = "org.fh.entity";                         //扫描的 实体类 目录
  57.  
  58.     @Value("${datasource.no1.url}")
  59.     private String url;
  60.  
  61.     @Value("${datasource.no1.username}")
  62.     private String user;
  63.  
  64.     @Value("${datasource.no1.password}")
  65.     private String password;
  66.  
  67.     @Value("${datasource.no1.driver-class-name}")
  68.     private String driverClass;
  69.  
  70.     @Bean(name = "masterDataSource")
  71.     @Primary
  72.     public DataSource masterDataSource() {
  73.         DruidDataSource dataSource = new DruidDataSource();
  74.         dataSource.setDriverClassName(driverClass);
  75.         dataSource.setUrl(url);
  76.         dataSource.setUsername(user);
  77.         dataSource.setPassword(password);
  78.         return dataSource;
  79.     }
  80.  
  81.     @Bean(name = "masterTransactionManager")
  82.     @Primary
  83.     public DataSourceTransactionManager masterTransactionManager() {
  84.         return new DataSourceTransactionManager(masterDataSource());
  85.     }
  86.  
  87.     @Bean(name = "masterSqlSessionFactory")
  88.     @Primary
  89.     public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)throws Exception {
  90.         final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
  91.         sessionFactory.setDataSource(masterDataSource);
  92.         sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MasterDataSourceConfig.MAPPER_LOCATION));
  93.         sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(MasterDataSourceConfig.CONFIG_LOCATION));
  94.         sessionFactory.setTypeAliasesPackage(MasterDataSourceConfig.TYPE_ALIASES_PACKAGE);
  95.         return sessionFactory.getObject();
  96.     }
  97. }



上一篇:flowable 三种方式部署流程
下一篇:阿里注册中心 Nacos 启动报错 Unable to start web server