SpringBoot怎么使用druid配置多数据源
来源:亿速云
时间:2024-04-05 18:09:39 483浏览 收藏
文章小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《SpringBoot怎么使用druid配置多数据源》带大家来了解一下##content_title##,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!
一、背景
使用spring boot配置多数据源,数据源分别为postgresql、mysql
二、版本介绍
spring boot——2.5.4
druid——1.2.11
postgresql——12
mysql——8.0.16
maven——3.0
idea——2019
三、项目结构
java package目录
resource目录存放mapper.xml文件,按照数据源创建package
四、maven依赖
org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-web com.alibaba fastjson 2.0.4 com.alibaba druid-spring-boot-starter 1.2.11 org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 org.postgresql postgresql runtime mysql mysql-connector-java
五、yaml配置文件
server: port: 8081 spring: datasource: type: com.alibaba.druid.pool.DruidDataSource druid: web-stat-filter: enabled: true #是否启用StatFilter默认值true url-pattern: /* exclusions: /druid/*,*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico session-stat-enable: true session-stat-max-count: 10 stat-view-servlet: enabled: true #是否启用StatViewServlet默认值true url-pattern: /druid/* reset-enable: true login-username: admin login-password: admin allow: db1: username: postgres password: localhost url: jdbc:postgresql://localhost:5432/test driver-class-name: org.postgresql.Driver initial-size: 5 # 初始化大小 min-idle: 5 # 最小 max-active: 100 # 最大 max-wait: 60000 # 配置获取连接等待超时的时间 validation-query: select version() time-between-eviction-runs-millis: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 min-evictable-idle-time-millis: 300000 # 指定一个空闲连接最少空闲多久后可被清除,单位是毫秒 filters: config,wall,stat # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 connectionProperties: druid.stat.slowSqlMillis=200;druid.stat.logSlowSql=true;config.decrypt=false test-while-idle: true test-on-borrow: true test-on-return: false # 是否缓存preparedStatement,也就是PSCache 官方建议MySQL下建议关闭 个人建议如果想用SQL防火墙 建议打开 pool-prepared-statements: true max-pool-prepared-statement-per-connection-size: 20 db2: username: root password: localhost url: jdbc:mysql://localhost:3306/springboot?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai driver-class-name: com.mysql.cj.jdbc.Driver initial-size: 5 # 初始化大小 min-idle: 5 # 最小 max-active: 100 # 最大 max-wait: 60000 # 配置获取连接等待超时的时间 validation-query: select 'x' time-between-eviction-runs-millis: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 min-evictable-idle-time-millis: 300000 # 指定一个空闲连接最少空闲多久后可被清除,单位是毫秒 filters: config,wall,stat # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 connectionProperties: druid.stat.slowSqlMillis=200;druid.stat.logSlowSql=true;config.decrypt=false test-while-idle: true test-on-borrow: true test-on-return: false # 是否缓存preparedStatement,也就是PSCache 官方建议MySQL下建议关闭 个人建议如果想用SQL防火墙 建议打开 pool-prepared-statements: true max-pool-prepared-statement-per-connection-size: 20 mybatis: mapper-locations: classpath:com/demo/mapper/*.xml type-aliases-package: com.demo.entity configuration: log-impl: mapUnderscoreToCamelCase: true #showSql logging: level: java.sql: debug org.apache.ibatis: debug com.demo.mapper: debug config: classpath:logback-spring.xml
六、数据源配置文件
@Configuration @MapperScan(basePackages = "com.demo.mapper.postgre.**", sqlSessionFactoryRef = "oneSqlSessionFactory") public class DataSourceConfig1 { // 将这个对象放入Spring容器中 @Bean(name = "oneDataSource") // 表示这个数据源是默认数据源 @Primary // 读取application.properties中的配置参数映射成为一个对象 // prefix表示参数的前缀 @ConfigurationProperties(prefix = "spring.datasource.druid.db1") public DataSource getDateSource1() { return DataSourceBuilder.create().type(DruidDataSource.class).build(); } @Bean(name = "oneSqlSessionFactory") // 表示这个数据源是默认数据源 @Primary // @Qualifier表示查找Spring容器中名字为oneDataSource的对象 public SqlSessionFactory oneSqlSessionFactory(@Qualifier("oneDataSource") DataSource datasource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(datasource); bean.setMapperLocations( // 设置mybatis的xml所在位置 new PathMatchingResourcePatternResolver().getResources("classpath*:com.demo.mapper.postgre/*.xml")); return bean.getObject(); } @Bean("oneSqlSessionTemplate") // 表示这个数据源是默认数据源 @Primary public SqlSessionTemplate oneSqlSessionTemplate( @Qualifier("oneSqlSessionFactory") SqlSessionFactory sessionFactory) { return new SqlSessionTemplate(sessionFactory); } }
@Configuration @MapperScan(basePackages = "com.demo.mapper.mysql", sqlSessionFactoryRef = "twoSqlSessionFactory") public class DataSourceConfig2 { // 将这个对象放入Spring容器中 @Bean(name = "twoDataSource") // 读取application.properties中的配置参数映射成为一个对象 // prefix表示参数的前缀 @ConfigurationProperties(prefix = "spring.datasource.druid.db2") public DataSource getDateSource1() { return DataSourceBuilder.create().type(DruidDataSource.class).build(); } @Bean(name = "twoSqlSessionFactory") // 表示这个数据源是默认数据源 //@Primary // @Qualifier表示查找Spring容器中名字为oneDataSource的对象 public SqlSessionFactory oneSqlSessionFactory(@Qualifier("twoDataSource") DataSource datasource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(datasource); bean.setMapperLocations( // 设置mybatis的xml所在位置 new PathMatchingResourcePatternResolver().getResources("classpath*:com.demo.mapper.mysql/*.xml")); return bean.getObject(); } @Bean("twoSqlSessionTemplate") // 表示这个数据源是默认数据源 //@Primary public SqlSessionTemplate oneSqlSessionTemplate( @Qualifier("twoSqlSessionFactory") SqlSessionFactory sessionFactory) { return new SqlSessionTemplate(sessionFactory); } }
七、启动类配置
关键点:去除 exclude = {DataSourceAutoConfiguration.class} 及扫描 com.demo.mapper目录
@MapperScan("com.demo.mapper") @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication .class, args); } }
八、druid管理页面
输入地址 localhost://8081/druid,输入 admin/admin
今天关于《SpringBoot怎么使用druid配置多数据源》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
声明:本文转载于:亿速云 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
405 收藏
-
169 收藏
-
328 收藏
-
270 收藏
-
351 收藏
-
459 收藏
-
133 收藏
-
267 收藏
-
278 收藏
-
236 收藏
-
237 收藏
-
194 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习