|
| 1 | +package com.boot.mybatis.config; |
| 2 | + |
| 3 | +import com.alibaba.druid.pool.DruidDataSource; |
| 4 | +import com.alibaba.druid.support.http.StatViewServlet; |
| 5 | +import com.alibaba.druid.support.http.WebStatFilter; |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.beans.factory.annotation.Value; |
| 10 | +import org.springframework.boot.web.servlet.FilterRegistrationBean; |
| 11 | +import org.springframework.boot.web.servlet.ServletRegistrationBean; |
| 12 | +import org.springframework.context.annotation.Bean; |
| 13 | +import org.springframework.context.annotation.Configuration; |
| 14 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 15 | + |
| 16 | +/** |
| 17 | + * Druid数据库连接池配置文件 |
| 18 | + */ |
| 19 | +@Configuration |
| 20 | +public class DruidConfig { |
| 21 | + private static final Logger logger = LoggerFactory.getLogger(DruidConfig.class); |
| 22 | + |
| 23 | + @Value("${spring.datasource.druid.url}") |
| 24 | + private String dbUrl; |
| 25 | + |
| 26 | + @Value("${spring.datasource.druid.username}") |
| 27 | + private String username; |
| 28 | + |
| 29 | + @Value("${spring.datasource.druid.password}") |
| 30 | + private String password; |
| 31 | + |
| 32 | + @Value("${spring.datasource.druid.driverClassName}") |
| 33 | + private String driverClassName; |
| 34 | + |
| 35 | + @Value("${spring.datasource.druid.initial-size}") |
| 36 | + private int initialSize; |
| 37 | + |
| 38 | + @Value("${spring.datasource.druid.max-active}") |
| 39 | + private int maxActive; |
| 40 | + |
| 41 | + @Value("${spring.datasource.druid.min-idle}") |
| 42 | + private int minIdle; |
| 43 | + |
| 44 | + @Value("${spring.datasource.druid.max-wait}") |
| 45 | + private int maxWait; |
| 46 | + |
| 47 | + @Value("${spring.datasource.druid.pool-prepared-statements}") |
| 48 | + private boolean poolPreparedStatements; |
| 49 | + |
| 50 | + @Value("${spring.datasource.druid.max-pool-prepared-statement-per-connection-size}") |
| 51 | + private int maxPoolPreparedStatementPerConnectionSize; |
| 52 | + |
| 53 | + @Value("${spring.datasource.druid.time-between-eviction-runs-millis}") |
| 54 | + private int timeBetweenEvictionRunsMillis; |
| 55 | + |
| 56 | + @Value("${spring.datasource.druid.min-evictable-idle-time-millis}") |
| 57 | + private int minEvictableIdleTimeMillis; |
| 58 | + |
| 59 | + @Value("${spring.datasource.druid.max-evictable-idle-time-millis}") |
| 60 | + private int maxEvictableIdleTimeMillis; |
| 61 | + |
| 62 | + @Value("${spring.datasource.druid.validation-query}") |
| 63 | + private String validationQuery; |
| 64 | + |
| 65 | + @Value("${spring.datasource.druid.test-while-idle}") |
| 66 | + private boolean testWhileIdle; |
| 67 | + |
| 68 | + @Value("${spring.datasource.druid.test-on-borrow}") |
| 69 | + private boolean testOnBorrow; |
| 70 | + |
| 71 | + @Value("${spring.datasource.druid.test-on-return}") |
| 72 | + private boolean testOnReturn; |
| 73 | + |
| 74 | + @Value("${spring.datasource.druid.filters}") |
| 75 | + private String filters; |
| 76 | + |
| 77 | + @Value("{spring.datasource.druid.connection-properties}") |
| 78 | + private String connectionProperties; |
| 79 | + |
| 80 | + /** |
| 81 | + * Druid 连接池配置 |
| 82 | + */ |
| 83 | + @Bean //声明其为Bean实例 |
| 84 | + public DruidDataSource dataSource() { |
| 85 | + DruidDataSource datasource = new DruidDataSource(); |
| 86 | + datasource.setUrl(dbUrl); |
| 87 | + datasource.setUsername(username); |
| 88 | + datasource.setPassword(password); |
| 89 | + datasource.setDriverClassName(driverClassName); |
| 90 | + datasource.setInitialSize(initialSize); |
| 91 | + datasource.setMinIdle(minIdle); |
| 92 | + datasource.setMaxActive(maxActive); |
| 93 | + datasource.setMaxWait(maxWait); |
| 94 | + datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); |
| 95 | + datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); |
| 96 | + datasource.setMaxEvictableIdleTimeMillis(minEvictableIdleTimeMillis); |
| 97 | + datasource.setValidationQuery(validationQuery); |
| 98 | + datasource.setTestWhileIdle(testWhileIdle); |
| 99 | + datasource.setTestOnBorrow(testOnBorrow); |
| 100 | + datasource.setTestOnReturn(testOnReturn); |
| 101 | + datasource.setPoolPreparedStatements(poolPreparedStatements); |
| 102 | + datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); |
| 103 | + try { |
| 104 | + datasource.setFilters(filters); |
| 105 | + } catch (Exception e) { |
| 106 | + logger.error("druid configuration initialization filter", e); |
| 107 | + } |
| 108 | + datasource.setConnectionProperties(connectionProperties); |
| 109 | + return datasource; |
| 110 | + } |
| 111 | + /** |
| 112 | + * JDBC操作配置 |
| 113 | + */ |
| 114 | + @Bean(name = "dataOneTemplate") |
| 115 | + public JdbcTemplate jdbcTemplate (@Autowired DruidDataSource dataSource){ |
| 116 | + return new JdbcTemplate(dataSource) ; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * 配置 Druid 监控界面 |
| 121 | + */ |
| 122 | + @Bean |
| 123 | + public ServletRegistrationBean statViewServlet(){ |
| 124 | + ServletRegistrationBean srb = |
| 125 | + new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); |
| 126 | + //设置控制台管理用户 |
| 127 | + srb.addInitParameter("loginUsername","root"); |
| 128 | + srb.addInitParameter("loginPassword","root"); |
| 129 | + //是否可以重置数据 |
| 130 | + srb.addInitParameter("resetEnable","false"); |
| 131 | + return srb; |
| 132 | + } |
| 133 | + @Bean |
| 134 | + public FilterRegistrationBean statFilter(){ |
| 135 | + //创建过滤器 |
| 136 | + FilterRegistrationBean frb = |
| 137 | + new FilterRegistrationBean(new WebStatFilter()); |
| 138 | + //设置过滤器过滤路径 |
| 139 | + frb.addUrlPatterns("/*"); |
| 140 | + //忽略过滤的形式 |
| 141 | + frb.addInitParameter("exclusions", |
| 142 | + "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); |
| 143 | + return frb; |
| 144 | + } |
| 145 | +} |
0 commit comments