您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。