服务器之家

服务器之家 > 正文

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

时间:2021-05-31 13:52     来源/作者:__HelloWorld__

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

?
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

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部