springboot配置多数据源——示例

1.添加pom

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.16.20</version>
			<scope>provided</scope>
		</dependency>
		<!--pagehelper-->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>1.2.5</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.2</version>
		</dependency>
		<!-- 数据库驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.11</version>
		</dependency>
		<!-- 数据库连接池 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.1.20</version>
		</dependency>
		<!-- eureka客户端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

		<!--  dynamic-datasource多数据源配置 -->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
			<version>2.5.4</version>
		</dependency>

		<!-- Swagger依赖 -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.9.2</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.9.2</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

2.yml配置

spring:
  application:
    name: eureka-client-one #服务名称--调用的时候根据名称来调用服务的方法
  autoconfigure:
    exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure # 去除druid配置
  #配置数据库
  datasource:
    dynamic:
      #配置默认数据
      primary: test
      datasource:
        test: #数据源1配置
          url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8&allowPublicKeyRetrieval=true
          username: root
          password: root
          driver-class-name: com.mysql.cj.jdbc.Driver
        fyx_test: #数据源2配置
          url: jdbc:mysql://localhost:3306/fyx_test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8&allowPublicKeyRetrieval=true
          username: root
          password: root
          driver-class-name: com.mysql.cj.jdbc.Driver

在这里插入图片描述
3.给使用非默认数据源添加注解@DS
@DS 可以注解在方法上和类上,同时存在方法注解优先于类上注解。
注解在 service 实现或 mapper 接口方法上,不要同时在 service 和 mapper 注解。
在这里插入图片描述
4.测试
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个示例代码,展示了如何在Spring Boot中配置多数据源: 1. 创建一个配置类,用于配置第一个数据源: ```java @Configuration @MapperScan(basePackages = "com.example.firstdatasource.mapper", sqlSessionTemplateRef = "firstSqlSessionTemplate") public class FirstDataSourceConfig { @Bean(name = "firstDataSource") @ConfigurationProperties(prefix = "spring.datasource.first") public DataSource firstDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "firstSqlSessionFactory") public SqlSessionFactory firstSqlSessionFactory(@Qualifier("firstDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); return bean.getObject(); } @Bean(name = "firstTransactionManager") public DataSourceTransactionManager firstTransactionManager(@Qualifier("firstDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "firstSqlSessionTemplate") public SqlSessionTemplate firstSqlSessionTemplate(@Qualifier("firstSqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } } ``` 2. 创建另一个配置类,用于配置第二个数据源: ```java @Configuration @MapperScan(basePackages = "com.example.seconddatasource.mapper", sqlSessionTemplateRef = "secondSqlSessionTemplate") public class SecondDataSourceConfig { @Bean(name = "secondDataSource") @ConfigurationProperties(prefix = "spring.datasource.second") public DataSource secondDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "secondSqlSessionFactory") public SqlSessionFactory secondSqlSessionFactory(@Qualifier("secondDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); return bean.getObject(); } @Bean(name = "secondTransactionManager") public DataSourceTransactionManager secondTransactionManager(@Qualifier("secondDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "secondSqlSessionTemplate") public SqlSessionTemplate secondSqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } } ``` 3. 在application.properties或application.yml中添加数据源相关的配置: ```yaml spring.datasource.first.url=jdbc:mysql://localhost:3306/first_db spring.datasource.first.username=first_user spring.datasource.first.password=first_password spring.datasource.second.url=jdbc:mysql://localhost:3306/second_db spring.datasource.second.username=second_user spring.datasource.second.password=second_password ``` 4. 在需要使用第一个数据源的地方,使用@Qualifier注解指定要使用的数据源: ```java @Service public class FirstDataSourceService { private final FirstMapper firstMapper; public FirstDataSourceService(@Qualifier("firstSqlSessionTemplate") SqlSessionTemplate firstSqlSessionTemplate) { this.firstMapper = firstSqlSessionTemplate.getMapper(FirstMapper.class); } // 使用第一个数据源进行数据库操作 // ... } ``` 5. 在需要使用第二个数据源的地方,使用@Qualifier注解指定要使用的数据源: ```java @Service public class SecondDataSourceService { private final SecondMapper secondMapper; public SecondDataSourceService(@Qualifier("secondSqlSessionTemplate") SqlSessionTemplate secondSqlSessionTemplate) { this.secondMapper = secondSqlSessionTemplate.getMapper(SecondMapper.class); } // 使用第二个数据源进行数据库操作 // ... } ``` 这样就完成了Spring Boot中配置多数据源示例代码。你可以根据自己的需求进行相应的修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值