Spring boot multi-data Source Configuration Example

Source: Internet
Author: User

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

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.