1. 程式人生 > >Springboot 1.5.10.RELEASE中使用DruidDataSource連線池

Springboot 1.5.10.RELEASE中使用DruidDataSource連線池

maven:

<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.1.5</version>
</dependency>

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-configuration-processor</
artifactId> <optional>true</optional> </dependency>


application.yml配置:

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql:///test?serverTimezone=CTT&useSSL=false&nullNamePatternMatchesAll=true
    username: 
root password: 123456 initialSize: 10 minIdle: 10 maxActive: 30 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20 filters: stat connectionProperties: druid.stat.mergeSql: true druid.stat.slowSqlMillis: 5000

DruidProperties.java:

@ConfigurationProperties("spring.datasource")
public class DruidProperties {

   private String url;
   private String username;
   private String password;
   private String driverClassName;
   private int initialSize;
   private int minIdle;
   private int maxActive;
   private int maxWait;
   private int timeBetweenEvictionRunsMillis;
   private int minEvictableIdleTimeMillis;
   private String validationQuery;
   private boolean testWhileIdle;
   private boolean testOnBorrow;
   private boolean testOnReturn;
   private boolean poolPreparedStatements;
   private int maxPoolPreparedStatementPerConnectionSize;
   private String filters;
   private Properties connectionProperties = new Properties();
   // getter, setter 方法這邊省略。。。
}

 

DruidConfig.java

@Configuration
public class DruidConfig {

   @Autowired
   private DruidProperties druidProp;

   @Bean
   JdbcTemplate jdbcTemplate(DataSource dataSource){
      return new JdbcTemplate(dataSource);
   }

   @Bean
   NamedParameterJdbcTemplate namedParameterJdbcTemplate (JdbcTemplate jdbcTemplate) {
      return new NamedParameterJdbcTemplate(jdbcTemplate);
   }

   @Bean
   @Primary
   public DataSource druidDataSource() {
      DruidDataSource dataSource = new DruidDataSource();
      dataSource.setUrl(druidProp.getUrl());
      dataSource.setUsername(druidProp.getUsername());
      dataSource.setPassword(druidProp.getPassword());
      dataSource.setDriverClassName(druidProp.getDriverClassName());

      dataSource.setInitialSize(druidProp.getInitialSize());
      dataSource.setMinIdle(druidProp.getMinIdle());
      dataSource.setMaxActive(druidProp.getMaxActive());
      dataSource.setMaxWait(druidProp.getMaxWait());
      dataSource.setTimeBetweenEvictionRunsMillis(druidProp.getTimeBetweenEvictionRunsMillis());
      dataSource.setMinEvictableIdleTimeMillis(druidProp.getMinEvictableIdleTimeMillis());
      dataSource.setValidationQuery(druidProp.getValidationQuery());
      dataSource.setTestWhileIdle(druidProp.isTestWhileIdle());
      dataSource.setTestOnBorrow(druidProp.isTestOnBorrow());
      dataSource.setTestOnReturn(druidProp.isTestOnReturn());
      dataSource.setPoolPreparedStatements(druidProp.isPoolPreparedStatements());
      dataSource.setMaxPoolPreparedStatementPerConnectionSize(druidProp.getMaxPoolPreparedStatementPerConnectionSize());
      try {
         dataSource.setFilters(druidProp.getFilters());
      } catch (SQLException e) {
         System.err.println("druid configuration initialization filter: " + e);
      }

      dataSource.setConnectProperties(druidProp.getConnectionProperties());

      return dataSource;
   }

   @Bean
   public ServletRegistrationBean druidServlet() {
      ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
      servletRegistrationBean.addInitParameter("allow","0.0.0.0");
      servletRegistrationBean.addInitParameter("loginUsername","admin");
      servletRegistrationBean.addInitParameter("loginPassword","admin");
      servletRegistrationBean.addInitParameter("resetEnable","false");
      return servletRegistrationBean;
   }

   @Bean
   public FilterRegistrationBean filterRegistrationBean() {
      FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
      filterRegistrationBean.setFilter(new WebStatFilter());
      filterRegistrationBean.addUrlPatterns("/*");
      filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
      return filterRegistrationBean;
   }

}

 

訪問druid統計後臺:

http://localhost:8080/druid/index.html