欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-07 13:57:21
两个数据库实例,一个负责读,一个负责写。 datasource-reader: type: com.alibaba.druid.pool.druiddata...

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

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

读数据库配置

@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());
  }
}

写数据库配置

@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相关属性配置

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多数据源配置方法实例详解,希望对大家有所帮助