Multi-data source configuration in Springboot

Source: Internet
Author: User

Application.properties

Spring.datasource.primary.driver-class-name=oracle.jdbc.driver.oracledriverspring.datasource.primary.url=jdbc : oracle:thin:@10.110.110.110:1521:orclspring.datasource.primary.username= hbenergyspring.datasource.primary.password=hbenergyspring.datasource.secondary.url=jdbc:sqlserver:// 10.110.110.110;databasename=dbspring.datasource.secondary.username=spring.datasource.secondary.password= Spring.datasource.secondary.driver-class-name=com.microsoft.sqlserver.jdbc.sqlserverdriverspring.jpa.show-sql= truespring.jpa.hibernate.ddl-auto=updatespring.jpa.hibernate.dialect= org.hibernate.dialect.oracledialectspring.jpa.hibernate.naming.physical-strategy= Org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

Pom.xml

       <Dependency>            <groupId>Com.oracle</groupId>            <Artifactid>Ojdbc6</Artifactid>            <version>11.2.0.4.0-atlassian-hosted</version>        </Dependency>        <Dependency>            <groupId>Com.microsoft.sqlserver</groupId>            <Artifactid>Sqljdbc4</Artifactid>            <version>4.0</version>        </Dependency>        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-jdbc</Artifactid>        </Dependency>        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-data-jpa</Artifactid>        </Dependency>
Datasourceconfig.java   Configuration of JDBC
@Configuration Public classdatasourceconfig {@Bean (name= "Primarydatasource") @Qualifier ("Primarydatasource") @ConfigurationProperties (prefix= "Spring.datasource.primary")     PublicDataSource Primarydatasource () {returndatasourcebuilder.create (). build (); } @Bean (Name= "Secondarydatasource") @Qualifier ("Secondarydatasource") @Primary @ConfigurationProperties (prefix= "Spring.datasource.secondary")     PublicDataSource Secondarydatasource () {returndatasourcebuilder.create (). build (); } @Bean (Name= "Primaryjdbctemplate")     PublicJdbcTemplate primaryjdbctemplate (@Qualifier ("Primarydatasource") DataSource DataSource) {return NewJdbcTemplate (DataSource); } @Bean (Name= "Secondaryjdbctemplate")     PublicJdbcTemplate secondaryjdbctemplate (@Qualifier ("Secondarydatasource") DataSource DataSource) {return NewJdbcTemplate (DataSource); }}
Primaryconfig.java Configuration of JPA
@Configuration @enabletransactionmanagement@enablejparepositories (entitymanagerfactoryref= "Entitymanagerfactoryprimary", Transactionmanagerref= "Transactionmanagerprimary", Basepackages= { "com.repository" }) Public classprimaryconfig {@Autowired @Qualifier ("Primarydatasource")    PrivateDataSource Primarydatasource; @Primary @Bean (Name= "Entitymanagerprimary")     PublicEntitymanager Entitymanager (Entitymanagerfactorybuilder builder) {returnentitymanagerfactoryprimary (builder). GetObject (). Createentitymanager (); } @Primary @Bean (name= "Entitymanagerfactoryprimary")     PublicLocalcontainerentitymanagerfactorybean entitymanagerfactoryprimary (Entitymanagerfactorybuilder builder) {returnBuilder. DataSource (Primarydatasource). Properties (Getvendorproperties (PRIMARYDATASOURC e). Packages ("com.entity"). Persistenceunit ("Primarypersistenceunit"). build (); } @AutowiredPrivatejpaproperties jpaproperties; PrivateMap<string, string>getvendorproperties (DataSource DataSource) {returnjpaproperties.gethibernateproperties (DataSource); } @Primary @Bean (name= "Transactionmanagerprimary")     PublicPlatformtransactionmanager transactionmanagerprimary (Entitymanagerfactorybuilder builder) {return NewJpatransactionmanager (Entitymanagerfactoryprimary (builder). GetObject ()); }}
Secondconfig.java to JPA Configuration
@Configuration @enabletransactionmanagement@enablejparepositories (entitymanagerfactoryref= "Entitymanagerfactorysecondary", Transactionmanagerref= "Transactionmanagersecondary", Basepackages= { "com.repository" })//Set Repository location Public classsecondaryconfig {@Autowired @Qualifier ("Secondarydatasource")    PrivateDataSource Secondarydatasource; @Bean (Name= "Entitymanagersecondary")     PublicEntitymanager Entitymanager (Entitymanagerfactorybuilder builder) {returnentitymanagerfactorysecondary (builder). GetObject (). Createentitymanager (); } @Bean (Name= "Entitymanagerfactorysecondary")     PublicLocalcontainerentitymanagerfactorybean entitymanagerfactorysecondary (Entitymanagerfactorybuilder builder) { /c2>returnBuilder. DataSource (Secondarydatasource). Properties (Getvendorproperties (Secondarydatas Ource)). Packages ("com.entity")//set where entity classes are located. Persistenceunit ("Secondarypersistenceunit"). build (); } @AutowiredPrivatejpaproperties jpaproperties; PrivateMap<string, string>getvendorproperties (DataSource DataSource) {returnjpaproperties.gethibernateproperties (DataSource); } @Bean (Name= "Transactionmanagersecondary") Platformtransactionmanager transactionmanagersecondary (Entitymanagerfactorybuilder builder) {return NewJpatransactionmanager (Entitymanagerfactorysecondary (builder). GetObject ()); }}

JDBC Calls:

@Autowired    @Qualifier ("Primaryjdbctemplate")    private JdbcTemplate JdbcTemplate;

Multi-data source configuration in Springboot

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.