重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章将为大家详细讲解有关springboot如何集成druid数据库连接池,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
成都创新互联电话联系:13518219792,为您提供成都网站建设网页设计及定制高端网站建设服务,成都创新互联网页制作领域十多年,包括塑料袋等多个领域拥有丰富的网站设计经验,选择成都创新互联,为企业保驾护航。
1.配置pom
com.alibaba druid 1.1.12 com.alibaba jconsole com.alibaba tools
2.配置 application.properties
#druid连接池spring.datasource.type: com.alibaba.druid.pool.DruidDataSource#最大活跃数spring.datasource.maxActive: 20#初始化数量spring.datasource.initialSize: 1#最大连接等待超时时间spring.datasource.maxWait: 60000#打开PSCache,并且指定每个连接PSCache的大小spring.datasource.poolPreparedStatements: truespring.datasource.maxPoolPreparedStatementPerConnectionSize: 20#通过connectionProperties属性来打开mergeSql功能;慢SQL记录#connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000spring.datasource.minIdle: 1spring.datasource.timeBetweenEvictionRunsMillis: 60000spring.datasource.minEvictableIdleTimeMillis: 300000spring.datasource.validationQuery: select 1 from dualspring.datasource.testWhileIdle: truespring.datasource.testOnBorrow: falsespring.datasource.testOnReturn: false#配置监控统计拦截的filters,去掉后监控界面sql将无法统计,'wall'用于防火墙filters: stat, wall, log4j
3.Config配置类
/** * 说明:第一数据源配置 */@Configuration@MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory") //扫描 Mapper 接口并容器管理public class MasterDataSourceConfig { static final String PACKAGE = "org.fh.mapper.dsno1"; //master 目录static final String MAPPER_LOCATION = "classpath:mybatis/dsno1/*/*.xml"; //扫描的 xml 目录static final String CONFIG_LOCATION = "classpath:mybatis/dsno1/mybatis-config.xml"; //自定义的mybatis config 文件位置static final String TYPE_ALIASES_PACKAGE = "org.fh.entity"; //扫描的 实体类 目录 @Value("${datasource.no1.url}")private String url; @Value("${datasource.no1.username}")private String user; @Value("${datasource.no1.password}")private String password; @Value("${datasource.no1.driver-class-name}")private String driverClass; @Bean(name = "masterDataSource")@Primarypublic DataSource masterDataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClass); dataSource.setUrl(url); dataSource.setUsername(user); dataSource.setPassword(password);return dataSource; } @Bean(name = "masterTransactionManager")@Primarypublic DataSourceTransactionManager masterTransactionManager() {return new DataSourceTransactionManager(masterDataSource()); } @Bean(name = "masterSqlSessionFactory")@Primarypublic SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)throws Exception {final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(masterDataSource); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MasterDataSourceConfig.MAPPER_LOCATION)); sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(MasterDataSourceConfig.CONFIG_LOCATION)); sessionFactory.setTypeAliasesPackage(MasterDataSourceConfig.TYPE_ALIASES_PACKAGE);return sessionFactory.getObject(); } }
关于“springboot如何集成druid数据库连接池”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。