这篇文章将为大家详细讲解有关springboot如何集成druid数据库连接池,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
1.配置pom
<!-- druid 数据库连接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.12</version><exclusions> <exclusion> <groupId>com.alibaba</groupId> <artifactId>jconsole</artifactId> </exclusion> <exclusion> <groupId>com.alibaba</groupId> <artifactId>tools</artifactId> </exclusion> </exclusions></dependency>
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数据库连接池”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。