Springboot+jpa+mybatis Multi-data source support

Source: Internet
Author: User

Import Org.springframework.beans.factory.annotation.qualifier;import Org.springframework.boot.autoconfigure.jdbc.datasourcebuilder;import Org.springframework.boot.context.properties.configurationproperties;import Org.springframework.context.annotation.bean;import Org.springframework.context.annotation.configuration;import Org.springframework.context.annotation.primary;import javax.sql.datasource;/** * Com.ehaoyao.paycenter.job.common.config * Data Source Configuration class * @author PF * @create 2018-05-10 16:17 **/@Configurationpublic class Dat Asourceconfig {@Bean (name = "Paycenterdatasource") @Qualifier ("Paycenterdatasource") @Primary @ConfigurationP Roperties (prefix= "Spring.datasource.paycenter") public datasource Paycenterdatasource () {return datasourcebuild    Er.create (). build (); } @Bean (name = "Erpdatasource") @Qualifier ("Erpdatasource") @ConfigurationProperties (prefix= "spring.datasource.er P ") Public DataSource Erpdatasource () {return datasourcebuilder.create (). build (); }}

1, configuration DataSource, code as described above.

Package com.ehaoyao.paycenter.job.common.config;/** * Payment Center Data Source Configuration class * * @author PF * Created by Dell on 2018-05-04. */import Com.alibaba.druid.pool.druiddatasource;import Org.apache.ibatis.session.sqlsessionfactory;import Org.mybatis.spring.sqlsessionfactorybean;import Org.mybatis.spring.annotation.mapperscan;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.beans.factory.annotation.qualifier;import Org.springframework.beans.factory.annotation.value;import Org.springframework.context.annotation.bean;import Org.springframework.context.annotation.configuration;import org.springframework.context.annotation.Primary; Import Org.springframework.core.io.support.pathmatchingresourcepatternresolver;import Org.springframework.jdbc.datasource.datasourcetransactionmanager;import Org.springframework.transaction.annotation.enabletransactionmanagement;import javax.sql.DataSource;/** * Com.ehaoyao.paycenter.job.common.config * Payment Center Data Source Configuration class * * @author PF * @create 2018-05-04 10:26 **/@Configuration @mapperscan (basepackages = "Com.ehaoyao.paycenter.persistence.pay.mapper.paycenter", Sqlsessionfactoryref = "Paycentersqlsessionfactory") @EnableTransactionManagementpublic class Paycenterdatasourceconfig {static final String mapper_location = "classpath:mappings/com/ehaoyao/paycenter/    Persistence/pay/mapper/paycenter/*.xml ";    @Autowired @Qualifier ("Paycenterdatasource") private DataSource Paycenterdatasource; @Bean (name = "Paycentertransactionmanager") @Primary public Datasourcetransactionmanager Mastertransactionmanager ()    {return new Datasourcetransactionmanager (Paycenterdatasource); } @Bean (name = "Paycentersqlsessionfactory") @Primary public sqlsessionfactory paycentersqlsessionfactory (@Qualif IER ("Paycenterdatasource") DataSource Paycenterdatasource) throws Exception {final Sqlsessionfactorybea        n sessionfactory = new Sqlsessionfactorybean (); Sessionfactory.setdatasource (Paycenterdatasource); Sessionfactory.setmapperlocations (New Pathmatchingresourcepatternresolver (). Getresources (PayCenterDataSour        ceconfig.mapper_location));    return Sessionfactory.getobject (); }}

2, the master data source Sessionfactory, TransactionManager and other configuration, code as above.

Package com.ehaoyao.paycenter.job.common.config;/** * ERP Data Source Configuration class * * @author PF * Created by Dell on 2018-05-04. */import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.beans.factory.annotation.qualifier;import Org.springframework.beans.factory.annotation.value;import Org.springframework.boot.autoconfigure.orm.jpa.jpaproperties;import Org.springframework.boot.orm.jpa.entitymanagerfactorybuilder;import Org.springframework.context.annotation.Bean ; Import Org.springframework.context.annotation.configuration;import Org.springframework.data.jpa.repository.config.enablejparepositories;import Org.springframework.jdbc.datasource.datasourcetransactionmanager;import Org.springframework.orm.jpa.jpatransactionmanager;import Org.springframework.orm.jpa.localcontainerentitymanagerfactorybean;import Org.springframework.transaction.platformtransactionmanager;import Org.springframework.transaction.annotation.enabletransactionmanagement;import Javax.persistence.EntityManAger;import javax.sql.datasource;import java.util.map;/** * com.ehaoyao.paycenter.job.common.config * ERP Data Source Configuration class * @ Author PF * @create 2018-05-04 10:27 **/@Configuration @enabletransactionmanagement@enablejparepositories (Entityman Agerfactoryref= "Erpentitymanagerfactory", transactionmanagerref= "Erptransactionmanager", basePackages= {"Co M.ehaoyao.paycenter.persistence.pay.repository "}) public class Erpdatasourceconfig {@Autowired @Qualifier (" Erpdatas    Ource ") Private DataSource Erpdatasource; @Bean (name = "Entitymanager") public entitymanager Entitymanager (Entitymanagerfactorybuilder builder) {return E    Rpentitymanagerfactory (builder). GetObject (). Createentitymanager (); } @Bean (name = "Erpentitymanagerfactory") public Localcontainerentitymanagerfactorybean erpentitymanagerfactory (Ent Itymanagerfactorybuilder builder) {return builder. DataSource (Erpdatasource). Proper Ties (Getvendorproperties (ErpdatasouRCE). Packages ("Com.ehaoyao.paycenter.persistence.pay.entity.erp"). Persistenceunit ("Erpper    Sistenceunit "). Build ();    } @Autowired private Jpaproperties jpaproperties;    Private Map getvendorproperties (DataSource DataSource) {return jpaproperties.gethibernateproperties (DataSource); } @Bean (name = "Erptransactionmanager") public Platformtransactionmanager transactionmanagerprimary (entitymanagerf    Actorybuilder builder) {return new Jpatransactionmanager (Erpentitymanagerfactory (builder). GetObject ()); }}

3, configuration slave data source Sessionfactory, TransactionManager and other configuration, code as above.

I keep for reference, the master data source used by the Mybatis,slave JPA (although the use of a lot, but mainly to record multiple data sources, and Springboot under the JPA configuration related)

Springboot+jpa+mybatis Multi-data source support

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.