当前位置:   article > 正文

Spring Data Jpa 配置多数据源_datajpa 配置双数据源

datajpa 配置双数据源

1.配置数据库连接信息

spring:
  datasource:
    db1:  # 1.0 Datasource
      url: jdbc:mysql://127.0.0.1:3306/test1?useSSL=false&serverTimezone=GMT%2b8&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&cachePrepStmts=true&useServerPrepStmts=true
      username: root
      password: 123456
      driverClassName: com.mysql.cj.jdbc.Driver
    db2:  # 2.0 Datasource
      url: jdbc:mysql://127.0.0.1:3306/test2?useSSL=false&serverTimezone=GMT%2b8&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&cachePrepStmts=true&useServerPrepStmts=true
      username: root
      password: 123456
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2.编写数据源配置类

@ConfigurationProperties(prefix = "spring.datasource.db1")
@Component
@Data
public class Db1Properties {
    private String url;
    private String username;
    private String password;
    private String driverClassName;
}
@ConfigurationProperties(prefix = "spring.datasource.db2")
@Component
@Data
public class Db2Properties {
    private String url;
    private String username;
    private String password;
    private String driverClassName;
}

@Configuration
@Slf4j
public class DataSourceConfig {

    @Autowired
    private Db1Properties db1Properties;

    @Autowired
    private Db2Properties db2Properties;

    @Bean(name = "db1DataSource")
    @Qualifier("db1DataSource")
    @Primary
    public DataSource db1DataSource() {
        log.info("db1DataSource 初始化 .....");
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(db1Properties.getUrl());
        dataSource.setUsername(db1Properties.getUsername());
        dataSource.setPassword(db1Properties.getPassword());
        dataSource.setDriverClassName(db1Properties.getDriverClassName());
        return dataSource;
    }

    @Bean(name = "db2DataSource")
    @Qualifier("db2DataSource")
    public DataSource db2DataSource() {
        log.info("db2DataSource 初始化 .....");
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(db2Properties.getUrl());
        dataSource.setUsername(db2Properties.getUsername());
        dataSource.setPassword(db2Properties.getPassword());
        dataSource.setDriverClassName(db2Properties.getDriverClassName());
        return dataSource;
    }


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

3.编写数据库配置

以下两个是需要修改的配置信息

  • 设置@EnableJpaRepositories注解里的basePackages属性配置jpa持久化类的包路径
  • 设置db1EntityManagerFactory方法中的packages方法设置实体类所在的包路径
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "db1EntityManagerFactory",
        transactionManagerRef = "db1TransactionManager",
        basePackages = {"com.ljm.db1todb2.db1.repository"})    // 指定该数据源操作的DAO接口包
public class Db1Config {

    @Autowired
    @Qualifier("db1DataSource")
    private DataSource db1DataSource;


    @Primary
    @Bean(name = "db1EntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean db1EntityManagerFactory(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(db1DataSource)
                .packages("com.ljm.db1todb2.db1.model")         //设置实体类所在位置
                .persistenceUnit("db1PersistenceUnit")
                .build();
    }

    @Bean(name = "db1TransactionManager")
    public PlatformTransactionManager db1TransactionManager(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(db1EntityManagerFactory(builder).getObject());
    }

}

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "db2EntityManagerFactory",
        transactionManagerRef = "db2TransactionManager",
        // 指定该数据源操作的DAO接口包
        basePackages = {"com.ljm.db1todb2.db2.repository"})
public class Db2Config {

    @Autowired
    @Qualifier("db2DataSource")
    private DataSource db2DataSource;

    @Bean(name = "db2EntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean db2EntityManagerFactory(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(db2DataSource)
                //设置实体类所在位置
                .packages("com.ljm.db1todb2.db2.model")
                .persistenceUnit("db2PersistenceUnit")
                .build();
    }


    @Bean(name = "db2TransactionManager")
    public PlatformTransactionManager db2TransactionManager(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(db2EntityManagerFactory(builder).getObject());
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

4.目录结构

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/656259
推荐阅读