当前位置:首页 > 图灵资讯 > 技术篇> SpringBoot下动态数据源
SpringBoot下动态数据源
发布时间:2023-05-23 09:29:50
1.引入依赖
复制xml代码<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId></dependency>
2.配置数据源
在application.properties
数据源信息配置在文件中:
properties复制代码代码# Spring.datasource.url=jdbc:mysql://localhost:3306/main_db?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf-8spring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver# Spring从数据源配置.dynamic.datasource.master.url=jdbc:mysql://localhost:3306/master_db?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf-8spring.dynamic.datasource.master.username=rootspring.dynamic.datasource.master.password=rootspring.dynamic.datasource.master.driver-class-name=com.mysql.cj.jdbc.Driverspring.dynamic.datasource.slave.url=jdbc:mysql://localhost:3306/slave_db?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf-8spring.dynamic.datasource.slave.username=rootspring.dynamic
properties复制代码代码# Spring.datasource.url=jdbc:mysql://localhost:3306/main_db?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf-8spring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver# Spring从数据源配置.dynamic.datasource.master.url=jdbc:mysql://localhost:3306/master_db?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf-8spring.dynamic.datasource.master.username=rootspring.dynamic.datasource.master.password=rootspring.dynamic.datasource.master.driver-class-name=com.mysql.cj.jdbc.Driverspring.dynamic.datasource.slave.url=jdbc:mysql://localhost:3306/slave_db?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf-8spring.dynamic.datasource.slave.username=rootspring.dynamic
ps 图灵课堂老师从近一百套最新一线互联网公司面试题中精选而出,涵盖Java架构面试 所有技术栈,包括JVM,Mysql,并发,Spring,Redis,MQ,Zookeeper,Netty, Dubbo,Spring Boot,Spring Cloud,数据结构与算法,设计模式等相关技术领域的大 厂面试题及详解。 详情咨询客服获取全套面经试题。