1. Application.properties
#\u4e3b\u5e93\u914d\u7f6espring.datasource.primary.url=jdbc:mysql://Mysql.test.xxxxx.com:3306/test?useunicode=true&characterencoding=utf8&usessl=falseSpring.datasource.primary.username=Rootspring.datasource.primary.password=rootSpring.datasource.primary.driver-class-name=Com.mysql.jdbc.Driverspring.datasource.primary.type=Com.zaxxer.hikari.HikariDataSourcespring.datasource.secondary.url=jdbc:mysql://Mysql.log.test.xxxx.com:3306/test_log?useunicode=true&characterencoding=utf8&usessl=falseSpring.datasource.secondary.username=Rootspring.datasource.secondary.password=Rootspring.datasource.secondary.driver-class-name=Com.mysql.jdbc.Driverspring.datasource.secondary.type=Com.zaxxer.hikari.HikariDataSource
2. Configuration Classes
2.1 DataSource Configuration
ImportJavax.sql.DataSource;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.beans.factory.annotation.Qualifier;Importorg.springframework.boot.autoconfigure.jdbc.DataSourceProperties;Importorg.springframework.boot.context.properties.ConfigurationProperties;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;Importorg.springframework.context.annotation.Primary;ImportCom.zaxxer.hikari.HikariDataSource; @Configuration Public classdatasourceconfig {@Bean @Qualifier ("Primarydatasourceproperties") @Primary @ConfigurationProperties (prefix = "Spring.datasource.primary") Publicdatasourceproperties primarydatasourceproperties () {return Newdatasourceproperties (); } @Autowired @Qualifier ("Primarydatasourceproperties") Privatedatasourceproperties primarydatasourceproperties; @Bean @Qualifier ("Primarydatasource") @Primary PublicDataSource Primarydatasource () {returnPrimarydatasourceproperties.initializedatasourcebuilder (). Type (Hikaridatasource.class). build (); } //////////////////////@Bean @Qualifier ("Secondarydatasourceproperties") @ConfigurationProperties (prefix = "Spring.datasource.secondary") Publicdatasourceproperties secondarydatasourceproperties () {return Newdatasourceproperties (); } @Autowired @Qualifier ("Secondarydatasourceproperties") Privatedatasourceproperties secondarydatasourceproperties; @Bean (Name= "Secondarydatasource") @Qualifier ("Secondarydatasource") PublicDataSource Secondarydatasource () {returnSecondarydatasourceproperties.initializedatasourcebuilder (). Type (Hikaridatasource.class). build (); }}
2.2 Main Library Configuration
Importorg.apache.ibatis.session.SqlSessionFactory;ImportOrg.mybatis.spring.SqlSessionFactoryBean;ImportOrg.mybatis.spring.annotation.MapperScan;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.beans.factory.annotation.Qualifier;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;Importorg.springframework.context.annotation.Primary;/** Configuration of the main library*/@Configuration @mapperscan (basepackages= "Com.xxxx.xxxx.mapper", Sqlsessionfactoryref = "Primarysqlsessionfactory") Public classdatabaseconfig {@Autowired @Qualifier ("Primarydatasource") PrivateDataSource Primarydatasource; @Bean ("Primarysqlsessionfactory") @Qualifier ("Primarysqlsessionfactory") @Primary PublicSqlsessionfactory Sqlsessionfactory ()throwsException {Sqlsessionfactorybean Sessionfactorybean=NewSqlsessionfactorybean (); Sessionfactorybean.setdatasource (Primarydatasource); returnSessionfactorybean.getobject (); }} //class Databaseconfig
2.3 Log Library Configuration
ImportJavax.sql.DataSource;Importorg.apache.ibatis.session.SqlSessionFactory;ImportOrg.mybatis.spring.SqlSessionFactoryBean;ImportOrg.mybatis.spring.annotation.MapperScan;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.beans.factory.annotation.Qualifier;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;/** Configuration of the main library*/@Configuration @mapperscan (basepackages= "Com.xxxx.xxxx.logmapper", Sqlsessionfactoryref = "Secondarysqlsessionfactory") Public classlogdatabaseconfig{@Autowired @Qualifier ("Secondarydatasource") PrivateDataSource Secondarydatasource; @Bean (Name= "Secondarysqlsessionfactory") PublicSqlsessionfactory Sqlsessionfactory ()throwsException {Sqlsessionfactorybean Sessionfactorybean=NewSqlsessionfactorybean (); Sessionfactorybean.setdatasource (Secondarydatasource); returnSessionfactorybean.getobject (); }} //class Logdatabaseconfig
3. Use
Spring boot multi-data Source Configuration Example