In the process of doing the project will inevitably encounter this situation: a project needs two data in the database, I hope this article can give the small partners who encounter these problems a little help
The first step: the Mapper interface and XML files of the two data sources are placed under different folders respectively;
The second step: in the Application.yml file to add a dual data source, be sure to specify the main data source, or will be error
Spring:datasource:primary:driver-class-name:com.mysql.jdbc.driver Url:url address username: User name pas Sword: Password Secondary:driver-class-name:com.mysql.jdbc.driver url:url address username: User name password: password
Step three: Config class:
@Configuration @mapperscan (basepackages = "Mapper path to Data source 1: Com.dao.mapper.interface1", sqlsessiontemplateref = "Test1sqlsessiontemplate") public class datasource1config { @Bean (name = "Test1datasource") @ Configurationproperties (prefix = "spring.datasource.primary") @Primary public datasource testdatasource () { return datasourcebuilder.create (). build (); } @ Bean (name = "test1sqlsessionfactory") @Primary public sqlsessionfactory testsqlsessionfactory (@Qualifier ("Test1datasource") DataSource DataSource) throws Exception { Sqlsessionfactorybean bean = new sqlsessionfactorybEAN (); bean.setdatasource (DataSource); bean.setmapperlocations (New pathmatchingresourcepatternresolver (). GetResources (" classpath:mybatis/mapper/data Source 1/*.xml "));//Specify Mapper.xml path return bean.getobject (); } @Bean (name = " Test1transactionmanager ") @Primary public Datasourcetransactionmanager testtransactionmanager (@Qualifier ("Test1datasource") DataSource DataSource) { return new Datasourcetransactionmanager (DataSource); } @Bean (name = "Test1sqlsessiontemplate") @Primary public Sqlsessiontemplate testsqlsessiontemplate (@Qualifier ("Test1sqlsessionfactory") sqlsessionfactory sqlsessionfactory) throws Exception { return new sqlsessiontemplate (sqlsessionfactory); }}
@Configuration @mapperscan (basepackages = "Mapper path to Data source 2: Com.dao.mapper.interface2", sqlsessiontemplateref = "Test2sqlsessiontemplate") public class datasource2config { @Bean (name = "Test2datasource") @ Configurationproperties (prefix = "spring.datasource.secondary") public Datasource testdatasource () { return Datasourcebuilder.create (). Build (); } @Bean (name = " Test2sqlsessionfactory ") public sqlsessionfactory testsqlsessionfactory (@ Qualifier ("Test2datasource") datasource datasource) throws Exception { sqlsessionfactorybean bean = new sqlsessionfactorybean (); bean.setDataSource (DataSource); bean.setmapperlocations (new Pathmatchingresourcepatternresolver (). Getresources ("classpath:mybatis/mapper/data Source 2/*.xml"));//Specify Mapper.xml Path return bean.getobject (); } @Bean (name = "Test2transactionmanager") public Datasourcetransactionmanager testtransactionmanager (@Qualifier ("Test2datasource") DataSource DataSource) { return new Datasourcetransactionmanager (DataSource); } @Bean (name = "Test2sqlsessiontemplate") public SqlSessionTemplate Testsqlsessiontemplate (@Qualifier ("Test2sqlsessionfactory") sqlsessionfactory sqlsessionfactory) throws exception { &nBsp;return new sqlsessiontemplate (sqlsessionfactory); }}
At this point, the project can be run, but MySQL has a problem, is to configure a dual data source, when the connection pool is idle for more than 8 hours, the database connection will be automatically disconnected, in order to avoid this situation, you need to add the following configuration under the Yml file:
Datasource: primary: max-idle: 10 max-wait: 10000 min-idle: 5 initial-size: 5 validation-query: SELECT 1 test-on-borrow: false test-while-idle: true time-between-eviction-runs-millis: 18800 #就是这句话 secondary: max-idle: 10 max-wait: 10000 Min-idle: 5 initial-size: 5 validation-query: SELECT 1 test-on-borrow: false test-while-idle: true time-between-eviction-runs-millis: 18800 #就是这句话
The current configuration of the dual data source is complete.
Of course, the above configuration is for the use of the default data source configuration, but there are many children's shoes using Ali's Druid data source , the two principles are basically the same, just a slight change:
Spring:datasource:druid:primary:driver-class-name:com.mysql.jdbc.driver Url:url address Username: User Name Password: password secondary:driver-class-name:com.mysql.jdbc.driver url:url address username: User name PA ssWOrd: Password
Make the following changes in config:
Change Datasourcebuilder to Duriddatasourcebuilder
Springboot Configuring multiple data sources