当前位置: 首页 > wzjs >正文

php网站后台密码怎么修改快速建站教程网

php网站后台密码怎么修改,快速建站教程网,河北网站制作公司哪家好,域名关键词查询关键词:Spring Boot、多数据源配置、MySQL、SQL Server、Oracle、动态切换 ✅ 摘要 在实际企业级开发中,一个 Spring Boot 项目可能需要连接多个数据库,比如 MySQL、SQL Server 和 Oracle。不同的业务模块可能依赖不同的数据源,这…

关键词:Spring Boot、多数据源配置、MySQL、SQL Server、Oracle、动态切换


✅ 摘要

在实际企业级开发中,一个 Spring Boot 项目可能需要连接多个数据库,比如 MySQL、SQL Server 和 Oracle。不同的业务模块可能依赖不同的数据源,这就要求我们掌握 如何在 Spring Boot 中灵活配置和管理多个数据源

本文将围绕以下内容进行详细讲解:

  • Spring Boot 默认数据源配置方式
  • 配置单个数据库(MySQL、SQL Server、Oracle)
  • 多数据源配置与使用(MySQL + SQL Server + Oracle)
  • 使用 AbstractRoutingDataSource 实现动态数据源切换
  • 常见问题与解决方案(驱动类、URL格式、连接失败)

每部分都配有 完整的 application.yml 配置文件和 Java 配置类代码示例


📌 一、Spring Boot 数据源配置基础

🔹 1. 默认数据源配置(以 MySQL 为例)

spring:datasource:url: jdbc:mysql://localhost:3306/test_db?useSSL=false&serverTimezone=UTCusername: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driver

⚠️ 注意:

  • url 要注意时区配置(serverTimezone)
  • 确保引入了正确的 JDBC 驱动包

📌 二、单个数据库的配置方式

🔹 1. MySQL 数据源配置

Maven 依赖:
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.33</version>
</dependency>
application.yml:
spring:datasource:mysql:url: jdbc:mysql://localhost:3306/mysql_db?useSSL=false&serverTimezone=UTCusername: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driver

🔹 2. SQL Server 数据源配置

Maven 依赖:
<dependency><groupId>com.microsoft.sqlserver</groupId><artifactId>mssql-jdbc</artifactId><version>12.4.0.jre8</version>
</dependency>
application.yml:
spring:datasource:sqlserver:url: jdbc:sqlserver://localhost:1433;databaseName=SqlServerDB;encrypt=true;trustServerCertificate=false;loginTimeout=30;username: sapassword: yourStrongPassworddriver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver

🔹 3. Oracle 数据源配置

Maven 依赖(需手动下载 ojdbc jar 并安装到本地仓库):
mvn install:install-file -Dfile=ojdbc8.jar -DgroupId=com.oracle.database.jdbc -DartifactId=ojdbc8 -Dversion=21.10.0.0 -Dpackaging=jar
pom.xml 添加依赖:
<dependency><groupId>com.oracle.database.jdbc</groupId><artifactId>ojdbc8</artifactId><version>21.10.0.0</version>
</dependency>
application.yml:
spring:datasource:oracle:url: jdbc:oracle:thin:@//localhost:1521/ORCLCDBusername: systempassword: oracledriver-class-name: oracle.jdbc.OracleDriver

📌 三、多数据源配置(MySQL + SQL Server + Oracle)

🔹 1. application.yml 多数据源配置

spring:datasource:mysql:url: jdbc:mysql://localhost:3306/mysql_db?useSSL=false&serverTimezone=UTCusername: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driversqlserver:url: jdbc:sqlserver://localhost:1433;databaseName=SqlServerDB;encrypt=true;trustServerCertificate=false;loginTimeout=30;username: sapassword: yourStrongPassworddriver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriveroracle:url: jdbc:oracle:thin:@//localhost:1521/ORCLCDBusername: systempassword: oracledriver-class-name: oracle.jdbc.OracleDriver

🔹 2. Java 配置类实现多数据源注入

第一步:定义配置属性类
@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
@Data
public class DataSourceProperties {private Map<String, DataSourceConfig> datasource;@Datapublic static class DataSourceConfig {private String url;private String username;private String password;private String driverClassName;}
}

第二步:创建多个数据源 Bean
@Configuration
@RequiredArgsConstructor
public class DataSourceConfig {private final DataSourceProperties dataSourceProperties;@Bean("mysqlDataSource")@ConfigurationProperties(prefix = "spring.datasource.mysql")public DataSource mysqlDataSource() {return DataSourceBuilder.create().url(dataSourceProperties.getDatasource().get("mysql").getUrl()).username(dataSourceProperties.getDatasource().get("mysql").getUsername()).password(dataSourceProperties.getDatasource().get("mysql").getPassword()).driverClassName(dataSourceProperties.getDatasource().get("mysql").getDriverClassName()).build();}@Bean("sqlServerDataSource")public DataSource sqlServerDataSource() {return DataSourceBuilder.create().url(dataSourceProperties.getDatasource().get("sqlserver").getUrl()).username(dataSourceProperties.getDatasource().get("sqlserver").getUsername()).password(dataSourceProperties.getDatasource().get("sqlserver").getPassword()).driverClassName(dataSourceProperties.getDatasource().get("sqlserver").getDriverClassName()).build();}@Bean("oracleDataSource")public DataSource oracleDataSource() {return DataSourceBuilder.create().url(dataSourceProperties.getDatasource().get("oracle").getUrl()).username(dataSourceProperties.getDatasource().get("oracle").getUsername()).password(dataSourceProperties.getDatasource().get("oracle").getPassword()).driverClassName(dataSourceProperties.getDatasource().get("oracle").getDriverClassName()).build();}
}

📌 四、动态切换数据源(基于 AbstractRoutingDataSource)

🔹 1. 定义当前线程使用的数据源标识

public class DynamicDataSourceContextHolder {private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<>();public static void setDataSourceKey(String key) {CONTEXT_HOLDER.set(key);}public static String getDataSourceKey() {return CONTEXT_HOLDER.get();}public static void clearDataSourceKey() {CONTEXT_HOLDER.remove();}
}

🔹 2. 自定义 AbstractRoutingDataSource

@Component
@RequiredArgsConstructor
public class DynamicDataSource extends AbstractRoutingDataSource {private final DataSource mysqlDataSource;private final DataSource sqlServerDataSource;private final DataSource oracleDataSource;@PostConstructpublic void init() {Map<Object, Object> targetDataSources = new HashMap<>();targetDataSources.put("mysql", mysqlDataSource);targetDataSources.put("sqlserver", sqlServerDataSource);targetDataSources.put("oracle", oracleDataSource);this.setTargetDataSources(targetDataSources);this.setDefaultTargetDataSource(mysqlDataSource); // 设置默认数据源this.afterPropertiesSet();}@Overrideprotected Object determineCurrentLookupKey() {return DynamicDataSourceContextHolder.getDataSourceKey();}
}

🔹 3. 配置为事务管理器的数据源

@Bean
public PlatformTransactionManager transactionManager(DynamicDataSource dynamicDataSource) {return new DataSourceTransactionManager(dynamicDataSource);
}

🔹 4. 在 Service 层使用动态数据源

@Service
@RequiredArgsConstructor
public class UserService {private final JdbcTemplate jdbcTemplate;public void queryFromMysql() {DynamicDataSourceContextHolder.setDataSourceKey("mysql");List<Map<String, Object>> result = jdbcTemplate.queryForList("SELECT * FROM user");System.out.println("MySQL 查询结果:" + result);}public void queryFromSqlServer() {DynamicDataSourceContextHolder.setDataSourceKey("sqlserver");List<Map<String, Object>> result = jdbcTemplate.queryForList("SELECT * FROM Users");System.out.println("SQL Server 查询结果:" + result);}public void queryFromOracle() {DynamicDataSourceContextHolder.setDataSourceKey("oracle");List<Map<String, Object>> result = jdbcTemplate.queryForList("SELECT * FROM employees");System.out.println("Oracle 查询结果:" + result);}
}

✅ 总结

以下几点为本文重点:

模块技能点
单数据源配置MySQL、SQL Server、Oracle 的基本配置方法
多数据源配置如何在一个 Spring Boot 项目中配置多个数据源
动态数据源切换使用 AbstractRoutingDataSource 实现运行时切换
实战能力结合 JdbcTemplate、事务管理器使用多数据源

这些技能是你构建复杂微服务系统、支持多数据库架构的重要基础。


📚 参考资料

  • Spring Boot 官方文档
  • Spring Data Access 文档

文章转载自:

http://qqun1QzT.jrhmh.cn
http://8EVXY6EG.jrhmh.cn
http://sRT7SEkx.jrhmh.cn
http://IF39LG7u.jrhmh.cn
http://sQC6gdl4.jrhmh.cn
http://dgokp3nm.jrhmh.cn
http://auliuaNp.jrhmh.cn
http://VmhdYCMz.jrhmh.cn
http://tJ2jIuWD.jrhmh.cn
http://IeTsBUNY.jrhmh.cn
http://aj4Mvztl.jrhmh.cn
http://J7ymn13Z.jrhmh.cn
http://alQyFD4S.jrhmh.cn
http://USLzb62i.jrhmh.cn
http://NJ8TQnhF.jrhmh.cn
http://B9Bg0bke.jrhmh.cn
http://4OT9zJwr.jrhmh.cn
http://NHPzVPWw.jrhmh.cn
http://BbBKDRT2.jrhmh.cn
http://iyXpYpDp.jrhmh.cn
http://xV4o48Q6.jrhmh.cn
http://GxAqimBW.jrhmh.cn
http://H67Uvt2j.jrhmh.cn
http://ZiNuNfQK.jrhmh.cn
http://Dn4hnwHX.jrhmh.cn
http://LCHSvivl.jrhmh.cn
http://UB7YHnmm.jrhmh.cn
http://N5WnlyDe.jrhmh.cn
http://gUxtC5Nh.jrhmh.cn
http://Xr9g8RNg.jrhmh.cn
http://www.dtcms.com/wzjs/648523.html

相关文章:

  • 搭建论坛网站wordpress调用分类别名
  • 株洲网站制作公司在哪里电子工程专辑网站
  • 思源黑体做网站南通市港闸区城乡建设局网站
  • 网站制作的内容包含营销网站的功能
  • 怎样在百度建立自己的网站北京seo计费
  • php网站开发平台杭州规划建设网站
  • 中文企业网站模板wordpress+字体修改字体大小
  • 佛山快速建站哪家服务专业上海网站定制费用
  • 短视频营销的案例南京网站流量优化
  • 网站建设设计猫和老鼠做一个企业的官网可以做静态网站
  • wordpress 建站公司建设一个和聚享游差不多的网站
  • 找人帮你做ppt的网站吗国家开发大学网站作业怎么做
  • 长沙公司网站设计报价商城网站建设分为几块
  • 河南建设厅二建公示网站首页58同城类型网站制作
  • 棋牌类网站设计建设如何申请网页域名
  • 深圳网站建设公司官网购物网站上分期怎么做的
  • asp.net网站开发上海网站建设品牌
  • 怎样在手机上建设网站wordpress点击创建配置文件没反应
  • 客户网站建设洽谈方案福建省建设资格管理中心网站
  • 网站 相对路径云服务器建立多个网站
  • 网站怎么做才被收录快成都住建局官网从哪里查房屋备案没有
  • it公论 是建立在什么网站网站开发示例
  • 如何在百度上建网站安徽网站建设网站运营
  • 企业网站建设飞沐局网站建设合同
  • 站长工具seo综合查询pc网站制作的评价指标
  • 建站管理域名管理绑定外部域名中html5博客网站模板
  • 肇庆网站优化建设工业互联网平台排名
  • 做动图素材网站如何建设一个普通网页网站
  • 免费建设一个网站廊坊网站建设外包
  • 查询数据的网站怎么做在建设部网站首页