zoukankan      html  css  js  c++  java
  • SpirngBoot配置多数据库连接

    yml配置文件中格式如下

    spring:
      datasource:
       primary:
         jdbc-url: jdbc:mysql://127.0.0.1:3306/data1?useSSL=false&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&serverTimezone=Asia/Shanghai
         password: data1
         username: data1
       secondary:
         jdbc-url: jdbc:mysql://127.0.0.1:3306/data2?useSSL=false&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&serverTimezone=Asia/Shanghai
         username: data2
         password: data2

    程序中需要添加数据库指向

    @Configuration
    public class DataSourceConfig {
    
        @Configuration
        public class DataSourceConfiguration {
    
            @Primary
            @Bean
            @ConfigurationProperties(prefix = "spring.datasource.primary")
            public DataSource primaryDataSource() {
                return DataSourceBuilder.create().build();
            }
    
            @Bean
            @ConfigurationProperties(prefix = "spring.datasource.secondary")
            public DataSource secondaryDataSource() {
                return DataSourceBuilder.create().build();
            }
    
        }

    主数据库

    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(
            entityManagerFactoryRef="entityManagerFactoryPrimary",
            transactionManagerRef="transactionManagerPrimary",
            basePackages= { "tst.data.book" }) //设置Repository所在位置
    public class PrimaryConfig {
    
        @Autowired
        @Qualifier("primaryDataSource")
        private DataSource primaryDataSource;
    
        @Autowired
        private JpaProperties jpaProperties;
        @Autowired
        private HibernateProperties hibernateProperties;
    
        private Map<String, Object> getVendorProperties() {
            return hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
        }
    
        @Primary
        @Bean(name = "entityManagerPrimary")
        public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
            return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
        }
    
        @Primary
        @Bean(name = "entityManagerFactoryPrimary")
        public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
            return builder
                    .dataSource(primaryDataSource)
                    .packages("tst.data.book.a", "tst.data.book.b", "tst.data.book.c") //设置实体类所在位置
                    .persistenceUnit("primaryPersistenceUnit")
                    .properties(getVendorProperties())
                    .build();
        }
    
        @Primary
        @Bean(name = "transactionManagerPrimary")
        public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
            return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
        }
    
    }
         

    另一数据库

    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(
            entityManagerFactoryRef = "entityManagerFactorySecondary",
            transactionManagerRef = "transactionManagerSecondary",
            basePackages = {"tst.data.student"}) //设置Repository所在位置
    public class SecondaryConfig {
    
        @Autowired
        @Qualifier("secondaryDataSource")
        private DataSource secondaryDataSource;
    
        @Bean(name = "entityManagerSecondary")
        public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
            return entityManagerFactorySecondary(builder).getObject().createEntityManager();
        }
    
        @Bean(name = "entityManagerFactorySecondary")
        public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary(EntityManagerFactoryBuilder builder) {
            return builder
                    .dataSource(secondaryDataSource)
                    .properties(getVendorProperties(secondaryDataSource))
                    .packages("tst.data.student.a") //设置实体类所在位置
                    .persistenceUnit("secondaryPersistenceUnit")
                    .build();
        }
    
        @Resource
        private JpaProperties jpaProperties;
        @Autowired
        private HibernateProperties hibernateProperties;
    
        private Map<String, Object> getVendorProperties(DataSource secondaryDataSource) {
            return hibernateProperties.determineHibernateProperties(
                    jpaProperties.getProperties(), new HibernateSettings());
        }
    
        @Bean(name = "transactionManagerSecondary")
        PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
            return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
        }
    }     
  • 相关阅读:
    UIGestureRecognizer在多层视图中的触发问题
    mysql出现Waiting for table metadata lock的原因及解决方案
    SQL逆向工程
    自己总结的ruby on rails 查询方法
    hdu 1536 SG函数模板题
    spring 源码分析之BeanPostProcessor
    spring bean生命周期管理--转
    java.lang.Long cannot be cast to java.lang.Integer解决办法
    Mybatis之Oracle增删查改示例--转
    Anti-If: The missing patterns--转
  • 原文地址:https://www.cnblogs.com/yisheng/p/14874944.html
Copyright © 2011-2022 走看看