###我的github代码示例 ###Druid是什么?有什么作用? Druid首先是一个数据库连接池,但它不仅仅是一个数据库连接池,它还包含一个ProxyDriver,一系列内置的JDBC组件库,一个SQL Parser。 ###Druid支持哪些数据库? Druid支持所有JDBC兼容的数据库,包括Oracle、MySql、Derby、Postgresql、SQL Server、H2等等。
Druid针对Oracle和MySql做了特别优化,比如Oracle的PS Cache内存占用优化,MySql的ping检测优化。 ###为什么说Druid是“最好的数据库连接池”?体现在哪些方面?这是如何实现的? 阿里巴巴是一个重度使用关系数据库的公司,我们在生产环境中大量的使用Druid,通过长期在极高负载的生产环境中实际使用、修改和完善,让Druid逐步发展成最好的数据库连接池。Druid在监控、可扩展性、稳定性和性能方面都有明显的优势。 ###配置源码 ###1、监控ServletRegistrationBean
@Slf4j@Configurationpublic class DruidAdminConfiguration { @Bean public ServletRegistrationBean druidServlet() { ServletRegistrationBean reg = new ServletRegistrationBean(); reg.setServlet(new StatViewServlet()); reg.addUrlMappings("/druid/*"); //reg.addInitParameter("allow", "127.0.0.1"); //reg.addInitParameter("deny",""); reg.addInitParameter("loginUsername", "paicheya"); reg.addInitParameter("loginPassword", "PaiCheYaDev"); return reg; } @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; }}
###2、DruidDataSource bean
@Slf4j@Configurationpublic class DruidDatabaseConfiguration { @Autowired private Environment environment; @Bean public DruidDataSource dataSource() throws PropertyVetoException { String url = environment.getProperty("spring.datasource.url"); String username = environment.getProperty("spring.datasource.username"); String password = environment.getProperty("spring.datasource.password"); String driverClassName = environment.getProperty("spring.datasource.driver-class-name"); if (url == null || "".equals(url)) { return null; } DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName(driverClassName); druidDataSource.setUrl(url); druidDataSource.setUsername(username); druidDataSource.setPassword(password); druidDataSource.setMaxActive(30); druidDataSource.setMinIdle(5); druidDataSource.setInitialSize(5); druidDataSource.setTestWhileIdle(true); druidDataSource.setTestOnBorrow(true); druidDataSource.setValidationQuery("select 1"); druidDataSource.setValidationQueryTimeout(3600); try { // druidDataSource.setFilters("stat, wall"); druidDataSource.setFilters("stat"); } catch (SQLException e) { log.warn("设置Druid的Filters错误。", e); } return druidDataSource; }}
###3、mybatis 配置类
@Configuration@ConditionalOnExpression("'${fortress.mybatis.enabled}'=='true'")public class MyBatisDataSourceConfig { @Autowired private DataSource dataSource; @Bean public DataSourceTransactionManager transactionManager() { return new DataSourceTransactionManager(dataSource); } @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sessionFactory.setDataSource(dataSource); sessionFactory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml")); PageHelper pageHelper = new PageHelper(); Properties properties = new Properties(); properties.setProperty("reasonable", "true"); properties.setProperty("supportMethodsArguments", "true"); properties.setProperty("returnPageInfo", "check"); properties.setProperty("params", "count=countSql"); pageHelper.setProperties(properties); sessionFactory.setPlugins(new Interceptor[]{pageHelper}); return sessionFactory.getObject(); }}
####(1)@ConditionalOnExpression("'${fortress.mybatis.enabled}'=='true'"),此配置为动态加载配置。配置文件中配置true则加载该配置 ####(2)sessionFactory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));,此配置为为加载xml文件。 ###4、加载原理 MyBatisDataSourceConfig 中加载sqlSessionFactory的参数DataSource 来自spring默认配置。如果在代码中重写 dataSourceBean则加载传入重写的source。如DruidDatabaseConfiguration的dataSourceBean方法。
###4、附赠xml配置方式,加上sql动态监控方式 引用他人博客: