溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

多數據源怎么利用spring boot進行配置

發布時間:2020-11-27 15:52:06 來源:億速云 閱讀:152 作者:Leah 欄目:編程語言

本篇文章給大家分享的是有關多數據源怎么利用spring boot進行配置,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

多數據源配置

創建一個Spring配置類,定義兩個DataSource用來讀取application.properties中的不同配置。如下例子中,主數據源配置為spring.datasource.primary開頭的配置,第二數據源配置為spring.datasource.secondary開頭的配置。

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

對應的application.properties配置如下:

spring.datasource.primary.url=jdbc:mysql://localhost:3306/test1
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/test2
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver

JdbcTemplate支持

對JdbcTemplate的支持比較簡單,只需要為其注入對應的datasource即可,如下例子,在創建JdbcTemplate的時候分別注入名為primaryDataSource和secondaryDataSource的數據源來區分不同的JdbcTemplate。

@Bean(name = "primaryJdbcTemplate")
public JdbcTemplate primaryJdbcTemplate(
    @Qualifier("primaryDataSource") DataSource dataSource) {
  return new JdbcTemplate(dataSource);
}
@Bean(name = "secondaryJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(
    @Qualifier("secondaryDataSource") DataSource dataSource) {
  return new JdbcTemplate(dataSource);
}

接下來通過測試用例來演示如何使用這兩個針對不同數據源的JdbcTemplate。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class ApplicationTests {
  @Autowired
  @Qualifier("primaryJdbcTemplate")
  protected JdbcTemplate jdbcTemplate1;
  @Autowired
  @Qualifier("secondaryJdbcTemplate")
  protected JdbcTemplate jdbcTemplate2;
  @Before
  public void setUp() {
    jdbcTemplate1.update("DELETE FROM USER ");
    jdbcTemplate2.update("DELETE FROM USER ");
  }
  @Test
  public void test() throws Exception {
    // 往第一個數據源中插入兩條數據
    jdbcTemplate1.update("insert into user(id,name,age) values(?, ?, ?)", 1, "aaa", 20);
    jdbcTemplate1.update("insert into user(id,name,age) values(?, ?, ?)", 2, "bbb", 30);
    // 往第二個數據源中插入一條數據,若插入的是第一個數據源,則會主鍵沖突報錯
    jdbcTemplate2.update("insert into user(id,name,age) values(?, ?, ?)", 1, "aaa", 20);
    // 查一下第一個數據源中是否有兩條數據,驗證插入是否成功
    Assert.assertEquals("2", jdbcTemplate1.queryForObject("select count(1) from user", String.class));
    // 查一下第一個數據源中是否有兩條數據,驗證插入是否成功
    Assert.assertEquals("1", jdbcTemplate2.queryForObject("select count(1) from user", String.class));
  }
}

完整示例:Chapter3-2-3

Spring-data-jpa支持

對于數據源的配置可以沿用上例中DataSourceConfig的實現。

新增對第一數據源的JPA配置,注意兩處注釋的地方,用于指定數據源對應的Entity實體和Repository定義位置,用@Primary區分主數據源。

新增對第二數據源的JPA配置,內容與第一數據源類似,具體如下:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    entityManagerFactoryRef="entityManagerFactorySecondary",
    transactionManagerRef="transactionManagerSecondary",
    basePackages= { "com.didispace.domain.s" }) //設置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("com.didispace.domain.s") //設置實體類所在位置
        .persistenceUnit("secondaryPersistenceUnit")
        .build();
  }
  @Autowired
  private JpaProperties jpaProperties;
  private Map<String, String> getVendorProperties(DataSource dataSource) {
    return jpaProperties.getHibernateProperties(dataSource);
  }
  @Bean(name = "transactionManagerSecondary")
  PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
    return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
  }
}

完成了以上配置之后,主數據源的實體和數據訪問對象位于:com.didispace.domain.p,次數據源的實體和數據訪問接口位于:com.didispace.domain.s。

分別在這兩個package下創建各自的實體和數據訪問接口

主數據源下,創建User實體和對應的Repository接口

@Entity
public class User {
  @Id
  @GeneratedValue
  private Long id;
  @Column(nullable = false)
  private String name;
  @Column(nullable = false)
  private Integer age;
  public User(){}
  public User(String name, Integer age) {
    this.name = name;
    this.age = age;
  }
  // 省略getter、setter
}

public interface UserRepository extends JpaRepository<User, Long> {
}

從數據源下,創建Message實體和對應的Repository接口

@Entity
public class Message {
  @Id
  @GeneratedValue
  private Long id;
  @Column(nullable = false)
  private String name;
  @Column(nullable = false)
  private String content;
  public Message(){}
  public Message(String name, String content) {
    this.name = name;
    this.content = content;
  }
  // 省略getter、setter
}
public interface MessageRepository extends JpaRepository<Message, Long> {
}

接下來通過測試用例來驗證使用這兩個針對不同數據源的配置進行數據操作。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class ApplicationTests {
  @Autowired
  private UserRepository userRepository;
  @Autowired
  private MessageRepository messageRepository;
  @Test
  public void test() throws Exception {
    userRepository.save(new User("aaa", 10));
    userRepository.save(new User("bbb", 20));
    userRepository.save(new User("ccc", 30));
    userRepository.save(new User("ddd", 40));
    userRepository.save(new User("eee", 50));
    Assert.assertEquals(5, userRepository.findAll().size());
    messageRepository.save(new Message("o1", "aaaaaaaaaa"));
    messageRepository.save(new Message("o2", "bbbbbbbbbb"));
    messageRepository.save(new Message("o3", "cccccccccc"));
    Assert.assertEquals(3, messageRepository.findAll().size());
  }
}

以上就是多數據源怎么利用spring boot進行配置,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女