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 * 数据源配置类 * @author PF * @create 2018-05-10 16:17 **/ @Configuration public class DataSourceConfig { @Bean(name = "payCenterDataSource") @Qualifier("payCenterDataSource") @Primary @ConfigurationProperties(prefix="spring.datasource.paycenter") public DataSource paycenterDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "erpDataSource") @Qualifier("erpDataSource") @ConfigurationProperties(prefix="spring.datasource.erp") public DataSource erpDataSource() { return DataSourceBuilder.create().build(); } }
1、配置dataSource,代码如上所述。
package com.ehaoyao.paycenter.job.common.config;/** * 支付中心数据源配置类 * * @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 * 支付中心数据源配置类 * * @author PF * @create 2018-05-04 10:26 **/ @Configuration @MapperScan(basePackages = "com.ehaoyao.paycenter.persistence.pay.mapper.paycenter", sqlSessionFactoryRef = "payCenterSqlSessionFactory") @EnableTransactionManagement public 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(@Qualifier("payCenterDataSource") DataSource payCenterDataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(payCenterDataSource); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources(PayCenterDataSourceConfig.MAPPER_LOCATION)); return sessionFactory.getObject(); } }
2、master数据源的sessionFactory、transactionManager等配置,代码如上。
package com.ehaoyao.paycenter.job.common.config;/** * ERP数据源配置类 * * @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数据源配置类 * @author PF * @create 2018-05-04 10:27 **/ @Configuration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef="erpEntityManagerFactory", transactionManagerRef="erpTransactionManager", basePackages= { "com.ehaoyao.paycenter.persistence.pay.Repository" }) public class ErpDataSourceConfig { @Autowired @Qualifier("erpDataSource") private DataSource erpDataSource; @Bean(name = "entityManager") public EntityManager entityManager(EntityManagerFactoryBuilder builder) { return erpEntityManagerFactory(builder).getObject().createEntityManager(); } @Bean(name = "erpEntityManagerFactory") public LocalContainerEntityManagerFactoryBean erpEntityManagerFactory (EntityManagerFactoryBuilder builder) { return builder .dataSource(erpDataSource) .properties(getVendorProperties(erpDataSource)) .packages("com.ehaoyao.paycenter.persistence.pay.entity.erp") .persistenceUnit("erpPersistenceUnit") .build(); } @Autowired private JpaProperties jpaProperties; private Map getVendorProperties(DataSource dataSource) { return jpaProperties.getHibernateProperties(dataSource); } @Bean(name = "erpTransactionManager") public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(erpEntityManagerFactory(builder).getObject()); } }
3、配置slave数据源的sessionFactory、transactionManager等配置,代码如上。
自己留着备查用的,master数据源用的mybatis,slave用的jpa(虽然用法很搞,但是主要为了记录多数据源,及springboot下jpa配置相关)
原文地址:https://www.cnblogs.com/falcon-fei/p/9021121.html
时间: 2024-11-17 01:56:51