美文网首页
SpringBoot多数据源配置

SpringBoot多数据源配置

作者: inverseli | 来源:发表于2018-09-25 15:55 被阅读0次

    项目参考

    http://blog.didispace.com/springbootmultidatasource/

    项目结构

    project
    项目实例地址

    https://github.com/Inverseli/SpringBoot-Learning/tree/master/learning3-2-3

    note

    • 继承了Repository(JpaRepository,CrudRepository)的自定义接口,就相当于标注了@Repository注解,会自动加入Spring的IOC容器中。
    • application.properties进行多数据源配置
    spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/book_manager1
    spring.datasource.primary.username=root
    spring.datasource.primary.password=root
    spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
    
    spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/book_manager2
    spring.datasource.secondary.username=root
    spring.datasource.secondary.password=root
    spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
    # hibernate settings
    spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop
    spring.jpa.database=mysql
    spring.jpa.generate-ddl=true
    spring.jpa.show-sql=true
    spring.jpa.hibernate.ddl-auto=update
    spring.jpa.hibernate.nameing.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
    
    • EntityManagers
    package com.inverseli.learning;
    
    import java.util.Map;
    import javax.annotation.Resource;
    import javax.persistence.EntityManager;
    import javax.sql.DataSource;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
    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.context.annotation.Primary;
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    import org.springframework.orm.jpa.JpaTransactionManager;
    import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
    import org.springframework.transaction.PlatformTransactionManager;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    
    /**  
     * @date:2018年9月25日 上午11:37:47    
     * @author liyuhao
     * @version 1.0   
     * @since JDK 1.8  
     * @description:  
     */
    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(
            entityManagerFactoryRef = "entityManagerFactoryPrimary",
            transactionManagerRef = "transactionManagerPrimary",
            basePackages = {"com.inverseli.learning.domain.p"})
    public class PrimaryConfig {
     
        @Resource
        @Qualifier("primaryDataSource")
        private DataSource primaryDataSource;
     
        @Primary
        @Bean(name = "entityManagerPrimary")
        public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
            return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
        }
     
        @Resource
        private JpaProperties jpaProperties;
        // 此处会因为版本的原因代码可能会不同
        private Map<String, Object> getVendorProperties() {
            // application.properties hibernate settings
            return jpaProperties.getHibernateProperties(new HibernateSettings()); 
        }
     
        /**
         * 设置实体类所在位置
         */
        @Primary //主次之分
        @Bean(name = "entityManagerFactoryPrimary")
        public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
            return builder
                    .dataSource(primaryDataSource)
                    .packages("com.inverseli.learning.domain.p")
                    .persistenceUnit("primaryPersistenceUnit")
                    .properties(getVendorProperties())
                    .build();
        }
        
     
        @Primary 
        @Bean(name = "transactionManagerPrimary")
        public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
            return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
        }
    }
    

    相关文章

      网友评论

          本文标题:SpringBoot多数据源配置

          本文链接:https://www.haomeiwen.com/subject/cxdloftx.html