First import MyBatis and so on, there is not much to say.
Here are the configuration of multiple data sources and mybatis, each of which corresponds to a set of MyBatis templates
Data source 1:
1 PackageCom.aaaaaaa.config.datasource;2 3 ImportJavax.sql.DataSource;4 5 Importorg.apache.ibatis.session.SqlSessionFactory;6 ImportOrg.mybatis.spring.SqlSessionFactoryBean;7 Importorg.mybatis.spring.SqlSessionTemplate;8 ImportOrg.mybatis.spring.annotation.MapperScan;9 ImportOrg.springframework.beans.factory.annotation.Qualifier;Ten ImportOrg.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; One Importorg.springframework.boot.context.properties.ConfigurationProperties; A ImportOrg.springframework.context.annotation.Bean; - Importorg.springframework.context.annotation.Configuration; - Importorg.springframework.context.annotation.Primary; the ImportOrg.springframework.core.io.support.PathMatchingResourcePatternResolver; - ImportOrg.springframework.jdbc.datasource.DataSourceTransactionManager; - - /** + * - * @desc support multi-data source configuration, copy a code to modify all appdb <br> + * Use this data source as the primary data source. You can only set @primary to one copy of the data source as the primary data source A * @authorJosnow at * @date January 12, 2018 PM 9:58:18 - * @version1.0.0 - */ - @Configuration -@MapperScan (basepackages = "com.aaaaaaa.mapper.appdb", Sqlsessiontemplateref = "Appdbsqlsessiontemplate") - Public classdatasourceconfigappdb { in -@Bean (name = "Appdbdatasource") to@ConfigurationProperties (prefix = "spring.datasource.appdb") + @Primary - PublicDataSource Testdatasource () { the returndatasourcebuilder.create (). build (); * } $ Panax Notoginseng@Bean (name = "Appdbsqlsessionfactory") - @Primary the PublicSqlsessionfactory testsqlsessionfactory (@Qualifier ("Appdbdatasource") DataSource DataSource) + throwsException { ASqlsessionfactorybean Bean =NewSqlsessionfactorybean (); the Bean.setdatasource (dataSource); +Bean.setmapperlocations (NewPathmatchingresourcepatternresolver (). Getresources ("Classpath:mapper/appdb/*.xml")); - returnBean.getobject (); $ } $ -@Bean (name = "Appdbtransactionmanager") - @Primary the PublicDatasourcetransactionmanager Testtransactionmanager (@Qualifier ("Appdbdatasource") DataSource DataSource) { - return NewDatasourcetransactionmanager (dataSource);Wuyi } the -@Bean (name = "Appdbsqlsessiontemplate") Wu @Primary - Publicsqlsessiontemplate testsqlsessiontemplate ( About@Qualifier ("Appdbsqlsessionfactory") sqlsessionfactory sqlsessionfactory)throwsException { $ return Newsqlsessiontemplate (sqlsessionfactory); - } - -}
This is how the first data source and MyBatis are configured.
The first data source is used by Appdbtransactionmanager to manage transactions, and @transactional (value = "Appdbtransactionmanager") annotations are used to indicate which transaction manager to use when using transactions.
The Distributed transaction manager is not used here, and the distributed transaction manager is a big topic, if you consider the distributed transaction can be self-modification.
The following configuration of the second data source and MyBatis
1 PackageCom.aaaaaaa.config.datasource;2 3 ImportJavax.sql.DataSource;4 5 Importorg.apache.ibatis.session.SqlSessionFactory;6 ImportOrg.mybatis.spring.SqlSessionFactoryBean;7 Importorg.mybatis.spring.SqlSessionTemplate;8 ImportOrg.mybatis.spring.annotation.MapperScan;9 ImportOrg.springframework.beans.factory.annotation.Qualifier;Ten ImportOrg.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; One Importorg.springframework.boot.context.properties.ConfigurationProperties; A ImportOrg.springframework.context.annotation.Bean; - Importorg.springframework.context.annotation.Configuration; - ImportOrg.springframework.core.io.support.PathMatchingResourcePatternResolver; the ImportOrg.springframework.jdbc.datasource.DataSourceTransactionManager; - - @Configuration -@MapperScan (basepackages = "Com.aaaaaaa.mapper.testdb", Sqlsessiontemplateref = "Testdbsqlsessiontemplate") + Public classDatasourceconfigtestdb { - +@Bean (name = "Testdbdatasource") A@ConfigurationProperties (prefix = "Spring.datasource.testdb") at PublicDataSource Testdatasource () { - returndatasourcebuilder.create (). build (); - } - -@Bean (name = "Testdbsqlsessionfactory") - PublicSqlsessionfactory testsqlsessionfactory (@Qualifier ("Testdbdatasource") DataSource DataSource) in throwsException { -Sqlsessionfactorybean Bean =NewSqlsessionfactorybean (); to Bean.setdatasource (dataSource); + Bean.setmapperlocations ( - NewPathmatchingresourcepatternresolver (). Getresources ("Classpath:mapper/testdb/*.xml")); the returnBean.getobject (); * } $ Panax Notoginseng@Bean (name = "Testdbtransactionmanager") - PublicDatasourcetransactionmanager Testtransactionmanager (@Qualifier ("Testdbdatasource") DataSource DataSource) { the return NewDatasourcetransactionmanager (dataSource); + } A the@Bean (name = "Testdbsqlsessiontemplate") + Publicsqlsessiontemplate testsqlsessiontemplate ( -@Qualifier ("Testdbsqlsessionfactory") sqlsessionfactory sqlsessionfactory)throwsException { $ return Newsqlsessiontemplate (sqlsessionfactory); $ } - -}
Take a look at the differences between the two data sources:
1: Data source 2 is actually from the data source 1copy a copy of the code, and all appdb changed to TestDB
2: In addition data source 1 has @primary annotations, and data source 2 does not have @primary annotations. Using @primary annotations is to use data source 1 as the default data source, and multiple data sources must specify a default data source, or an error will be initiated.
MyBatis multiple data sources that's so simple.
Springboot MyBatis Multi-Data source configuration