服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - Spring Boot 2.0多数据源配置方法实例详解

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

2021-05-31 13:52__HelloWorld__ Java教程

这篇文章主要介绍了Spring Boot 2.0多数据源配置方法实例详解,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

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

?
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

延伸 · 阅读

精彩推荐