Spring Boot集成Mybatis实现多数据源连接

  1. 云栖社区>
  2. 博客>
  3. 正文

Spring Boot集成Mybatis实现多数据源连接

优惠券发放 2018-08-18 07:47:04 浏览726 评论0

摘要: Spring Boot集成Mybatis实现多数据源连接 以两个数据源为例,application.properties有两组数据库配置,再配置对应的Config与mapper,各自处理各自的业务。

Spring Boot集成Mybatis实现多数据源连接

以两个数据源为例,application.properties有两组数据库配置,再配置对应的Config与mapper,各自处理各自的业务。

1.配置pom.xml依赖

首先配置好jdbc、mybatis的依赖:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <dependencies>
        <!--Spring Boot依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.36</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.3.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.6.3</version>
        </dependency>
        ......
    </dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

2.配置application.properties

#主数据库
spring.datasource.gm.jdbcUrl=jdbc:mysql://xx.xx.xx.xx/gmtools?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
spring.datasource.gm.username=xxxx
spring.datasource.gm.password=xxxx
spring.datasource.gm.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.bi.jdbcUrl=jdbc:mysql://xx.xx.xx.xx/bitools?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
spring.datasource.bi.username=xxxx
spring.datasource.bi.password=xxxx
spring.datasource.bi.driver-class-name=com.mysql.jdbc.Driver
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3.数据库连接

主数据库连接通过 @Primary 进行注解:

@Configuration
// 扫描 Mapper 接口并容器管理
//@EnableTransactionManagement //如果mybatis中service实现类中加入事务注解,需要此处添加该注解
@MapperScan(basePackages = "com.kodgames.gmtools.dao.gm", sqlSessionFactoryRef = "gmSqlSessionFactory")
public class GmDataDaoConfig {
    @Bean(name = "gmDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.gm") // application.properteis中对应属性的前缀
    public DataSource gmDataSource() {
        return DataSourceBuilder.create().type(com.zaxxer.hikari.HikariDataSource.class).build();
    }

    @Bean
    @Primary
    public SqlSessionFactory gmSqlSessionFactory(@Qualifier("gmDataSource") DataSource gmDataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(gmDataSource);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/GmDataDaoMapper.xml"));
        return factoryBean.getObject();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

从数据库连接:

@Configuration
@MapperScan(basePackages = "com.kodgames.gmtools.dao.bi", sqlSessionFactoryRef = "biSqlSessionFactory")
public class BIDataDaoConfig {
    @Bean(name = "biDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.bi") // application.properteis中对应属性的前缀
    public DataSource biDataSource() {
        return DataSourceBuilder.create().type(com.zaxxer.hikari.HikariDataSource.class).build();
    }

    @Bean
    public SqlSessionFactory biSqlSessionFactory(@Qualifier("biDataSource") DataSource biDataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(biDataSource);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/BIDataDaoMapper.xml"));
        return factoryBean.getObject();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

如果再增加一个数据源就得从头到尾增加一遍。

原文地址https://blog.csdn.net/qq_36526703/article/details/81778379

【云栖快讯】一站式开发者服务,海量学习资源免费学  详情请点击

网友评论

作者关闭了评论