Spring boot Multiple data source configuration (multiple databases)

Source: Internet
Author: User

Recently in the use of Spring boot development project, which has a project to use a multi-data source configuration, online information or not too much, go a lot to find a suitable, write their own share, make a note, may be useful, the first blog, not to spray!!

Let's start with my business scenario, which uses two types of databases, one MySQL and the other SQL Server,

The first step is to configure the configuration information for multiple data sources in APPLICATION.YML.

MySQL Data source:

Spring
DataSource
DriverClassName:com.mysql.jdbc.Driver
Url:jdbc:mysql://192.168.28.230:3306/****?useunicode=true&characterencoding=utf-8&autoreconnect=true
Username: * * *
Password: * * *

SQL Server Data Source configuration
Custom
DataSource
Names:ds1
DS1:
DriverClassName:com.microsoft.sqlserver.jdbc.SQLServerDriver
Url:jdbc:sqlserver://ip:1433;databasename= database name
Username: * * *
Password: * * *

The second step is to write the configuration and load classes for the data source:
SQL Server Data source
@Configuration
@MapperScan (basepackages = rdsdatasourceconfig.package, Sqlsessionfactoryref = "Rdssessionfactory")
public class Rdsdatasourceconfig {
Static final String package = "Com.jyall.ehr.kaoqin";//This project is using MyBatis, this path is the packet structure of the scanned mapper
@Value ("${custom.datasource.ds1.url}")//The database configuration information in the configuration file in the first step
Private String Dburl;
@Value ("${custom.datasource.ds1.username}")//The same as the information in the configuration file
Private String DbUser;
@Value ("${custom.datasource.ds1.password}")//Same as profile information
Private String Dbpassword;

@Bean (name = "Rdsdatasource")
Public DataSource Rdsdatasource () {
DataSource DataSource = new DataSource ();
Datasource.setdriverclassname ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");
Datasource.seturl (Dburl);
Datasource.setusername (DbUser);
Datasource.setpassword (Dbpassword);
return dataSource;
}

@Bean (name = "Rdstransactionmanager")
Public Datasourcetransactionmanager Rdstransactionmanager (@Qualifier ("Rdsdatasource") DataSource Adsdatasource) {
return new Datasourcetransactionmanager (Rdsdatasource ());
}

@Bean (name = "Rdssessionfactory")
Public Sqlsessionfactory adssqlsessionfactory (@Qualifier ("Rdsdatasource") DataSource Adsdatasource) throws Exception {
Final Sqlsessionfactorybean sessionfactory = new Sqlsessionfactorybean ();
Sessionfactory.setdatasource (Adsdatasource);
return Sessionfactory.getobject ();
}
}

MySQL Data source
@Configuration
@MapperScan (basepackages = adsdatasourceconfig.package, Sqlsessionfactoryref = "Adssqlsessionfactory")
public class Adsdatasourceconfig {
Static final String package = "Com.jyall.ehr.mapper";//Scan the mapper package structure

@Value ("${spring.datasource.url}")//configuration in configuration file
Private String Dburl;
@Value ("${spring.datasource.username}")//configuration in configuration file
Private String DbUser;
@Value ("${spring.datasource.password}")//configuration in configuration file
Private String Dbpassword;

@Bean (name = "Adsdatasource")
@Primary//This annotation indicates the default data source configuration, which is the data source used in the default configuration
Public DataSource Adsdatasource () {
DataSource DataSource = new DataSource ();
Datasource.setdriverclassname ("Com.mysql.jdbc.Driver");
Datasource.seturl (Dburl);
Datasource.setusername (DbUser);
Datasource.setpassword (Dbpassword);
return dataSource;
}

@Bean (name = "Adstransactionmanager")
@Primary
Public Datasourcetransactionmanager Adstransactionmanager (@Qualifier ("Adsdatasource") DataSource Adsdatasource) {
return new Datasourcetransactionmanager (Adsdatasource);
}

@Bean (name = "Adssqlsessionfactory")
@Primary
Public Sqlsessionfactory adssqlsessionfactory (@Qualifier ("Adsdatasource") DataSource Adsdatasource) throws Exception {
Final Sqlsessionfactorybean sessionfactory = new Sqlsessionfactorybean ();
Sessionfactory.setdatasource (Adsdatasource);
return Sessionfactory.getobject ();
}
}
At this point the configuration of the multi-data source has been completed, the other configuration is the same as the configuration of the data source, not repeat ...

Spring boot Multiple data source configuration (multiple databases)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.