https://blog.csdn.net/mrczr/article/details/78903468java
https://blog.csdn.net/yhl_jxy/article/details/78645010spring
SpringBoot、MyBatis、Druid。sql
import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; import org.springframework.context.annotation.Primary; import com.alibaba.druid.filter.Filter; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.wall.WallConfig; import com.alibaba.druid.wall.WallFilter; /** * * 描述:若是不使用代碼手動初始化DataSource的話,監控界面的SQL監控會沒有數據("是spring boot的bug???") * * @author chhliu 建立時間:2017年2月9日 下午10:33:08 * @version 1.2.0 */ @Configuration public class DruidConfiguration { @Value("${spring.datasource.url}") private String dbUrl; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; // @Value("${spring.datasource.driverClassName}") @Value("${spring.datasource.driver-class-name}") private String driverClassName; @Value("${spring.datasource.initialSize}") private int initialSize; @Value("${spring.datasource.minIdle}") private int minIdle; @Value("${spring.datasource.maxActive}") private int maxActive; @Value("${spring.datasource.maxWait}") private int maxWait; @Value("${spring.datasource.timeBetweenEvictionRunsMillis}") private int timeBetweenEvictionRunsMillis; @Value("${spring.datasource.minEvictableIdleTimeMillis}") private int minEvictableIdleTimeMillis; @Value("${spring.datasource.validationQuery}") private String validationQuery; @Value("${spring.datasource.testWhileIdle}") private boolean testWhileIdle; @Value("${spring.datasource.testOnBorrow}") private boolean testOnBorrow; @Value("${spring.datasource.testOnReturn}") private boolean testOnReturn; @Value("${spring.datasource.poolPreparedStatements}") private boolean poolPreparedStatements; @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}") private int maxPoolPreparedStatementPerConnectionSize; @Value("${spring.datasource.filters}") private String filters; @Value("${spring.datasource.connectionProperties}") private String connectionProperties; @Value("${spring.datasource.useGlobalDataSourceStat}") private boolean useGlobalDataSourceStat; @Autowired WallFilter wallFilter; @Bean // 聲明其爲Bean實例 @Primary // 在一樣的DataSource中,首先使用被標註的DataSource public DataSource dataSource() { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(this.dbUrl); datasource.setUsername(username); datasource.setPassword(password); datasource.setDriverClassName(driverClassName); // configuration datasource.setInitialSize(initialSize); datasource.setMinIdle(minIdle); datasource.setMaxActive(maxActive); datasource.setMaxWait(maxWait); datasource .setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); datasource.setValidationQuery(validationQuery); datasource.setTestWhileIdle(testWhileIdle); datasource.setTestOnBorrow(testOnBorrow); datasource.setTestOnReturn(testOnReturn); datasource.setPoolPreparedStatements(poolPreparedStatements); datasource .setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); datasource.setUseGlobalDataSourceStat(useGlobalDataSourceStat); try { datasource.setFilters(filters); } catch (SQLException e) { System.err.println("druid configuration initialization filter: " + e); } datasource.setConnectionProperties(connectionProperties); // filter List<Filter> filterList = new ArrayList<Filter>(); //在設置以前先判斷是都已經存在WallConfig,若是有,直接將現有的替換掉 List<Filter> filters = datasource.getProxyFilters(); boolean isExist = false; for(Filter filter:filters){ if(filter instanceof WallFilter){ ((WallFilter)filter).setConfig(wallConfig()); isExist = true; } } if(!isExist){ filterList.add(wallFilter); datasource.setProxyFilters(filterList); } return datasource; } @Bean(name = "wallFilter") @DependsOn("wallConfig") public WallFilter wallFilter(WallConfig wallConfig) { WallFilter wallFilter = new WallFilter(); wallFilter.setConfig(wallConfig); return wallFilter; } @Bean(name = "wallConfig") public WallConfig wallConfig() { WallConfig wallConfig = new WallConfig(); wallConfig.setMultiStatementAllow(true);// 容許一次執行多條語句 wallConfig.setNoneBaseStatementAllow(true);// 容許一次執行多條語句 return wallConfig; } }
//在設置以前先判斷是都已經存在WallConfig,若是有,直接將現有的替換掉 List<Filter> filters = datasource.getProxyFilters(); boolean isExist = false; for(Filter filter:filters){ if(filter instanceof WallFilter){ ((WallFilter)filter).setConfig(wallConfig()); isExist = true; } } if(!isExist){ filterList.add(wallFilter); datasource.setProxyFilters(filterList); }
須要判斷WallConfig是否已存在。WallConfig不存在時可直接以下操做:ui
filterList.add(wallFilter);this
datasource.setProxyFilters(filterList);url
已存在的狀況下則須要替換以前的WallConfig。spa