【问题标题】:Spring boot Single entity to multiple databaseSpring Boot 单个实体到多个数据库
【发布时间】:2018-01-27 18:01:42
【问题描述】:

是否可以通过 spring boot 将单个实体保存到多个数据库(DB1 和 DB2)。例如,在发布数据时,我有两个具有相同表的 MYSQL DB,我需要将人员详细信息同时保存到两个数据库中。?或任何其他方式做spring。我已经创建了两个数据库连接 如果实体不同意味着我可以保存数据,但实体相同意味着我不能做吗?

public class Person {
private Long id;
private String name;
private String city;
}

我的桌子CREATE TABLEperson( idBIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, nameVARCHAR(255) DEFAULT NULL, cityVARCHAR(255) DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=INNODB DEFAULT CHARSET=latin1

对于名为 DB1 和 DB2 的两个 DBS 我的连接配置是

@Configuration
 @EnableJpaRepositories(basePackages = 
 {"com.onlinetutorialspoint.repository.db1"},
    entityManagerFactoryRef = "db1EntityManager",
    transactionManagerRef = "db1TransactionManager")
  public class DB1_DataSource {
@Autowired
private Environment env;
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean db1EntityManager() {
    LocalContainerEntityManagerFactoryBean em = new 
    LocalContainerEntityManagerFactoryBean();
    em.setDataSource(db1Datasource());
    em.setPackagesToScan(new String[]
   {"com.onlinetutorialspoint.model.db1"});
    em.setPersistenceUnitName("db1EntityManager");
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    HashMap<String, Object> properties = new HashMap<>();
    properties.put("hibernate.dialect",
            env.getProperty("hibernate.dialect"));
    properties.put("hibernate.show-sql",
            env.getProperty("jdbc.show-sql"));
    em.setJpaPropertyMap(properties);
    return em;
}
@Primary
@Bean
public DataSource db1Datasource() {
    DriverManagerDataSource dataSource
            = new DriverManagerDataSource();
    dataSource.setDriverClassName(
            env.getProperty("jdbc.driver-class-name"));
    dataSource.setUrl(env.getProperty("db1.datasource.url"));
    dataSource.setUsername(env.getProperty("db1.datasource.username"));
    dataSource.setPassword(env.getProperty("db1.datasource.password"));
    return dataSource;
}
@Primary
@Bean
public PlatformTransactionManager db1TransactionManager() {
    JpaTransactionManager transactionManager
            = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(
            db1EntityManager().getObject());
    return transactionManager;
}
}

第二个数据库

@Configuration
@EnableJpaRepositories(basePackages = 
{"com.onlinetutorialspoint.repository.db2"},
    entityManagerFactoryRef = "db2EntityManager",
    transactionManagerRef = "db2TransactionManager")
public class DB2_DataSource {
@Autowired
private Environment env;
@Bean
public LocalContainerEntityManagerFactoryBean db2EntityManager() {
    LocalContainerEntityManagerFactoryBean em
            = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(db2Datasource());
    em.setPackagesToScan(
            new String[]{"com.onlinetutorialspoint.model.db2"});
    em.setPersistenceUnitName("db2EntityManager");
    HibernateJpaVendorAdapter vendorAdapter
            = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    HashMap<String, Object> properties = new HashMap<>();
    properties.put("hibernate.dialect",
            env.getProperty("hibernate.dialect"));
    properties.put("hibernate.show-sql",
            env.getProperty("jdbc.show-sql"));
    em.setJpaPropertyMap(properties);
    return em;
}
@Bean
public DataSource db2Datasource() {
    DriverManagerDataSource dataSource
            = new DriverManagerDataSource();
    dataSource.setDriverClassName(
            env.getProperty("jdbc.driver-class-name"));
    dataSource.setUrl(env.getProperty("db2.datasource.url"));
    dataSource.setUsername(env.getProperty("db2.datasource.username"));
    dataSource.setPassword(env.getProperty("db2.datasource.password"));
    return dataSource;
}
@Bean
public PlatformTransactionManager db2TransactionManager() {
    JpaTransactionManager transactionManager
            = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(
            db2EntityManager().getObject());
    return transactionManager;
}
}

我的休息控制器

@RestController
 public class CustomerController {
 @Autowired
 private PersonRepository personRepositorydb1;
 @Autowired
 private PersonRepository personRepositorydb2;

 @RequestMapping("/save")
 public Person savePersonDetails()
 public savePersonDetails(@RequestBody Person person ){
     personRepositorydb1.savePerson(person);
    return personRepositorydb2.savePerson(person);
}
}

如果我调用两个存储库意味着它得到错误模型名称已经被引入 我的仓库是

import com.onlinetutorialspoint.model.db1.Person;
@Repository
public interface PersonRepository extends CrudRepository<Person, Long>{
}

import com.onlinetutorialspoint.model.db2.Person;
@Repository
public interface PersonRepository extends CrudRepository<Person, Long>{
}

【问题讨论】:

  • 当然有可能。
  • 如何?你能举个例子
  • 您如何保存到一个类似地保存到另一个。你必须创建两个连接
  • 是的,我有两个连接,因为我没有问题
  • 您遇到了什么异常?你能把stacktrace放上去吗

标签: spring spring-boot


【解决方案1】:

也许您可以在此答案中使用相同的方法

Spring Data + JPA with multiple datasources but only one set of Repositories

这里有一个关于如何使用 AbstractRoutingDataSource 的示例

https://www.endpoint.com/blog/2016/11/16/connect-multiple-jpa-repositories-using

【讨论】:

  • 我在springboot中需要它
  • 是的,我明白了,我放的链接也可以在 spring boot 中复制。看一下spring boot中导入的依赖github.com/spring-projects/spring-boot/blob/master/…接口CrudRepository是来自spring数据,不是来自spring boot。 AbstractRoutingDataSource 也来自核心 spring 框架,它可以与 spring boot 一起使用。我注意到我提供的链接是否可以解决您的问题。
  • 我在这里做一个测试github.com/balax85/SpringProjectExample我添加了一个新的数据源。进入核心模块,您可以在包 it.balax85.examples.core.conf 中找到两个 db 连接。我复制了一个名为 CommissionRepository 的现有存储库,它位于 it.balax85.examples.core.repository 我在 it.balax85.examples.core.test 中添加了相同的存储库(更改名称,否则 spring 不会创建两个存储库)。这两个 repo 都用到了 it.balax85.examples.core.dao.CommissionDao。我只检查了更改是否可以编译和运行 spring-boot 但我没有例外。你能看看吗?
  • 请注意,上面关于如何使用 AbstractRoutingDataSource 的文章已移至:endpointdev.com/blog/2016/11/…
猜你喜欢
  • 2019-02-14
  • 1970-01-01
  • 2018-06-02
  • 1970-01-01
  • 1970-01-01
  • 2019-01-13
  • 2019-04-13
  • 2015-12-22
  • 2019-07-28
相关资源
最近更新 更多