1. 程式人生 > >springboot連線多個數據庫

springboot連線多個數據庫

今天借到一個新的需求,需要把自己資料庫某個表的資料遷移到別的資料庫中,於是百度,中間出現了一些細節的問題,解決花了點時間,在此記錄一下,下次避免出現過的錯誤

這裡把連線一個數據庫的情況也記錄一下,好做對比

一、連線一個數據庫

1.啟動類

@SpringBootApplication
//掃描mapper對映類所在路徑
@MapperScan(basePackages = "com.xh.iot.repositories.mapper")
public class EmDataProcApplication {

    public static void main(String[] args) {
        SpringApplication.run(EmDataProcApplication.class, args);
    }
}

2.配置檔案application.properties

# MySql Database
spring.datasource.url=jdbc:mysql://10.200.9.168:3306/iothub_base?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=Xtjc0551Mysql!


# MyBaits,xml對映檔案所在目錄,resources下
mybatis.mapper-locations=classpath:mapping/*.xml

3.新增mapper對應的service和serviceImpl,最後在controller中新增service例項操作資料庫

二、連線多個數據庫

1.啟動類

//EnableAutoConfiguration註解,關閉springBoot關於mybatis的一些自動注入
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class, MybatisAutoConfiguration.class})
//加入定時器
@EnableScheduling
@SpringBootApplication
public class DataTransGsiotApplication {

    public static void main(String[] args) {
        SpringApplication.run(DataTransGsiotApplication.class, args);
    }
}

2.配置檔案application.properties,配置了2個庫的連線

#  MySql Database
#org_base庫
spring.datasource.org.jdbcUrl=jdbc:mysql://10.200.9.168:3306/org_base?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false
spring.datasource.org.driverClassName = com.mysql.cj.jdbc.Driver
spring.datasource.org.username=root
spring.datasource.org.password=Xtjc0551Mysql!
#gs_iot_v1庫
spring.datasource.gsiot.jdbcUrl=jdbc:mysql://10.200.9.169:3306/gs_iot_v1?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false
spring.datasource.gsiot.driverClassName = com.mysql.cj.jdbc.Driver
spring.datasource.gsiot.username=root
spring.datasource.gsiot.password=root

3.建立2個數據庫連線的配置類

GsIotDataSourceConfig類

package com.xh.iot.repositories.datasource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.xh.iot.repositories.mapper.gsiot",sqlSessionFactoryRef = "gsiotSqlSessionFactory")
public class GsIotDataSourceConfig {
    @Primary
    @Bean(name = "gsiotDataSource")
    @ConfigurationProperties("spring.datasource.gsiot")
    public DataSource masterDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "gsiotSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("gsiotDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mapping/gsiot/*.xml"));
        return sessionFactoryBean.getObject();
    }
}

 OrgDataSourceConfig類

package com.xh.iot.repositories.datasource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.xh.iot.repositories.mapper.org",sqlSessionFactoryRef = "orgSqlSessionFactory")
public class OrgDataSourceConfig {
    @Primary
    @Bean(name = "orgDataSource")
    @ConfigurationProperties("spring.datasource.org")
    public DataSource masterDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "orgSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("orgDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mapping/org/*.xml"));
        return sessionFactoryBean.getObject();
    }
}

注意:

1、注意多個數據庫的application.properties檔案,資料庫連線用jdbcUrl或者jdbc-url

2、如果有更多的資料庫連線,可以按照這種方式新增;不同的資料庫,需要不同的配置類,可以把這些配置類放在同一個目錄中
3、 每個配資類中,需要指明當前資料庫表的xml檔案和mapper對映檔案所在的包名,並且把xml和mapper放置到對應的目錄中
4、其他的比如:xml裡面sql語句的寫法和正常一個連線是一模一樣,新增mapper對應的service和serviceImpl,最後在controller中新增不同的service例項就可以操作對應資料庫了