springboot配置双数据源 MySQL和SqlServer

看到网上好多实例,都失败了,报扫描不到mapper文件,下面是我的完整实例1. pom文件的驱动jar包加上去,不说了2. application.yml  spring:datasource: master: jdbc-url: jdbc:mysql://10.12.49.55:3306/smartcity-01 username: ro...
摘要由CSDN通过智能技术生成

看到网上好多实例,都失败了,报扫描不到mapper文件,下面是我的完整实例

1. pom文件的驱动jar包加上去,不说了

2. application.yml 

 
spring:
datasource:
        master:
          jdbc-url: jdbc:mysql://10.12.49.55:3306/smartcity-01
          username: root
          password: root
          # 使用druid数据源
          type: com.alibaba.druid.pool.DruidDataSource
          driverClassName: com.mysql.jdbc.Driver
        other:
          jdbc-url: jdbc:sqlserver://10.12.49.35:1433;DatabaseName=LandscapingDB
          username: sa
          password: Sql123
          #使用druid数据源
          type: com.alibaba.druid.pool.DruidDataSource
          driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver

注意不要使用url,要使用jdbc-url


主数据库配置:

package com.pactera.scm.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.sprin
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
可以在Spring Boot项目中同时连接MySQL和SQL Server数据库,具体步骤如下: 1. 在pom.xml文件中添加MySQL和SQL Server数据库的驱动依赖,例如: ```xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>${sqlserver.version}</version> </dependency> ``` 2. 在application.yml文件中配置两个数据源,例如: ```yaml spring: datasource: primary: url: jdbc:mysql://localhost:3306/db_mysql driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 secondary: url: jdbc:sqlserver://localhost:1433;databaseName=db_sqlserver driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver username: sa password: 123456 ``` 3. 在Spring Boot项目中配置两个数据源,例如: ```java @Configuration public class DataSourceConfig { @Bean(name = "primaryDataSource") @Qualifier("primaryDataSource") @ConfigurationProperties(prefix = "spring.datasource.primary") public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "secondaryDataSource") @Qualifier("secondaryDataSource") @ConfigurationProperties(prefix = "spring.datasource.secondary") public DataSource secondaryDataSource() { return DataSourceBuilder.create().build(); } } ``` 4. 在MyBatis配置两个数据源,并指定使用哪个数据源,例如: ```java @Configuration @MapperScan(basePackages = "com.example.mapper", sqlSessionTemplateRef = "primarySqlSessionTemplate") public class MybatisConfig { @Bean(name = "primarySqlSessionFactory") @Primary public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); return bean.getObject(); } @Bean(name = "secondarySqlSessionFactory") public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); return bean.getObject(); } @Bean(name = "primarySqlSessionTemplate") @Primary public SqlSessionTemplate primarySqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } @Bean(name = "secondarySqlSessionTemplate") public SqlSessionTemplate secondarySqlSessionTemplate(@Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } @Bean @Primary public PlatformTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean public PlatformTransactionManager secondaryTransactionManager(@Qualifier("secondaryDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } } ``` 在定义Mapper时,可以通过@Qualifier注解指定使用哪个数据源的SqlSessionTemplate,例如: ```java public interface UserMapper { @Select("SELECT * FROM user") @Results({ @Result(property = "id", column = "id"), @Result(property = "name", column = "name"), @Result(property = "age", column = "age") }) List<User> getAllUsers(); @Select("SELECT * FROM user WHERE id = #{id}") @Results({ @Result(property = "id", column = "id"), @Result(property = "name", column = "name"), @Result(property = "age", column = "age") }) User getUserById(@Param("id") Long id); } ``` 在Service层中,可以通过@Autowired注解注入UserMapper,并指定使用哪个数据源的SqlSessionTemplate,例如: ```java @Service public class UserService { @Autowired @Qualifier("primarySqlSessionTemplate") private SqlSessionTemplate primarySqlSessionTemplate; @Autowired @Qualifier("secondarySqlSessionTemplate") private SqlSessionTemplate secondarySqlSessionTemplate; public List<User> getAllUsersFromPrimaryDataSource() { UserMapper userMapper = primarySqlSessionTemplate.getMapper(UserMapper.class); return userMapper.getAllUsers(); } public User getUserByIdFromSecondaryDataSource(Long id) { UserMapper userMapper = secondarySqlSessionTemplate.getMapper(UserMapper.class); return userMapper.getUserById(id); } } ``` 这样就可以在Spring Boot项目中同时连接MySQL和SQL Server数据库了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值