spring boot集成mybatis(druid线程池)
2017-06-16 00:00
543 查看
摘要: spring boot,mybatis,druid
###我的github代码示例
https://github.com/jiahaos/spring-boot-mybatis.git
###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
###2、DruidDataSource bean
###3、mybatis 配置类
####(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动态监控方式
引用他人博客:https://my.oschina.net/wangmengjun/blog/788386
###我的github代码示例
https://github.com/jiahaos/spring-boot-mybatis.git
###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 @Configuration public 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 @Configuration public 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动态监控方式
引用他人博客:https://my.oschina.net/wangmengjun/blog/788386
相关文章推荐
- SpringBoot 集成MyBatis ,druid 数据源
- [置顶] 一步一步学springboot (七)集成mybatis及druid数据源
- Spring Boot : 集成mybatis、pagehelper插件、开启事务、druid数据源配置、mybatis-generator(十一)
- SpringBoot+Mybatis+Druid的集成
- spring-boot 速成(8) 集成druid+mybatis
- 从0开始学习SpringCould(5)--SpringBoot 集成Mybatis使用Druid数据源
- Spring Boot集成MyBatis、druid
- Spring Boot 集成 Mybatis(druid 数据库连接池 以及 分页配置)
- Spring Boot (二)集成Mybatis、Druid
- spring-boot集成mybatis使用Druid监控
- springboot入门(二)初步集成mybatis
- springboot 集成mybatis Invalid bound statement (not found)异常
- spring-boot 集成mybatis配置使用
- Spring Boot 集成MyBatis
- spring-boot 注解配置mybatis+druid(新手上路)
- springboot-profiles配置,日志配置,mybatis集成
- spring-boot 集成mybatis的分页插件PageHelper和Generator (番外)
- SpringBoot集成mybatis
- SpringBoot+Maven项目实战(4):集成Mybatis
- 10.玩转Spring Boot 集成Mybatis