Spring Boot 2.0多数据源配置方法实例详解

时间:2022-05-16 15:25:04

两个数据库实例,一个负责读,一个负责写。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
datasource-reader:
  type: com.alibaba.druid.pool.druiddatasource
  url: jdbc:mysql://192.168.43.61:3306/test?useunicode=true&characterencoding=utf8&autoreconnect=true&usessl=false
  username: icbc
  password: icbc
  driver-class-name: com.mysql.jdbc.driver
  continue-on-error: false
  sql-script-encoding: utf-8
 
datasource-writer:
  type: com.alibaba.druid.pool.druiddatasource
  url: jdbc:mysql://192.168.43.61:3306/hdfs?useunicode=true&characterencoding=utf8&autoreconnect=true&usessl=false
  username: icbc
  password: icbc
  driver-class-name: com.mysql.jdbc.driver
  continue-on-error: false
  sql-script-encoding: utf-8

读数据库配置

?
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
@configuration
@enabletransactionmanagement
@enablejparepositories(entitymanagerfactoryref = "entitymanagerfactoryprimary", transactionmanagerref = "transactionmanagerprimary", basepackages = {
    "cn.cib.repository.read"})
public class repositoryprimaryconfig {
  @autowired
  @qualifier("r_ds")
  private datasource r_ds;
  @bean(destroymethod = "", name = "entitymanagerprimary")
  @primary
  public entitymanager entitymanager() {
    return entitymanagerfactoryprimary().getobject().createentitymanager();
  }
  @bean(destroymethod = "", name = "entitymanagerfactoryprimary")
  @primary
  public localcontainerentitymanagerfactorybean entitymanagerfactoryprimary() {
    hibernatejpavendoradapter jpavendoradapter = new hibernatejpavendoradapter();
    localcontainerentitymanagerfactorybean factorybean = new localcontainerentitymanagerfactorybean();
    factorybean.setdatasource(r_ds);
    factorybean.setjpavendoradapter(jpavendoradapter);
    factorybean.setjpaproperties(hibernatepropertiesbuilder.hibernateproperties());
    factorybean.setpackagestoscan("cn.cib.repository.read", "cn.cib.entity.read");
    factorybean.setpersistenceunitname("read");
    return factorybean;
  }
  @bean(destroymethod = "", name = "transactionmanagerprimary")
  @primary
  platformtransactionmanager transactionmanagerprimary() {
    return new jpatransactionmanager(entitymanagerfactoryprimary().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
@configuration
@enabletransactionmanagement
@enablejparepositories(entitymanagerfactoryref = "entitymanagerfactorysecondary", transactionmanagerref = "transactionmanagersecondary", basepackages = {
    "cn.cib.repository.write"})
public class repositorysecondaryconfig {
  @autowired
  @qualifier("w_ds")
  private datasource w_ds;
  @bean(destroymethod = "", name = "entitymanagersecondary")
  public entitymanager entitymanager() {
    return entitymanagerfactorysecondary().getobject().createentitymanager();
  }
  @bean(destroymethod = "", name = "entitymanagerfactorysecondary")
  public localcontainerentitymanagerfactorybean entitymanagerfactorysecondary() {
    hibernatejpavendoradapter jpavendoradapter = new hibernatejpavendoradapter();
    localcontainerentitymanagerfactorybean factorybean = new localcontainerentitymanagerfactorybean();
    factorybean.setdatasource(w_ds);
    factorybean.setjpavendoradapter(jpavendoradapter);
    factorybean.setjpaproperties(hibernatepropertiesbuilder.hibernateproperties());
    factorybean.setpackagestoscan("cn.cib.repository.write","cn.cib.entity.write");
    factorybean.setpersistenceunitname("write");
    return factorybean;
  }
  @bean(destroymethod = "", name = "transactionmanagersecondary")
  platformtransactionmanager transactionmanagersecondary() {
    return new jpatransactionmanager(entitymanagerfactorysecondary().getobject());
  }
}

hibernate相关属性配置

?
1
2
3
4
5
6
7
8
9
10
public class hibernatepropertiesbuilder {
  public static properties hibernateproperties() {
    final properties hibernateproperties = new properties();
    hibernateproperties.setproperty("hibernate.dialect", "org.hibernate.dialect.mysql5dialect");
    hibernateproperties.setproperty("hibernate.hbm2ddl.auto", "update");
    hibernateproperties.setproperty("hibernate.show_sql", "true");
    hibernateproperties.setproperty("hibernate.format_sql", "true");
    return hibernateproperties;
  }
}

总结

以上所述是小编给大家介绍的spring boot 2.0多数据源配置方法实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://blog.csdn.net/kangkanglou/article/details/82683177