来自不同数据源中相似表的自动装配库

时间:2019-03-06 11:14:52

标签: spring-boot jpa

我有同一应用程序的两个版本。他们正在使用类似的数据库架构。我想运行一个junit测试,比较两端的两个表。我正在使用Springboot和JPA。我可以在同一测试中自动连接两个数据源。我可以为主要数据源中的一张表自动装配CrudRepository。但是,如何在同一Junit测试中同时为位于第二个数据源上的同一表自动装配相似的存储库?我想在同一堂课上做类似的事情:

const createArrayColor = (labels) => {
    const combi = [
        ["bronze","gold","new","silver"],
        ["bronze","gold","silver"],
        ["gold","new","silver"],
        ["bronze","gold","new"],
        ["bronze","new","silver"],
        ["gold","silver"],
        ["bronze","gold"],
        ["gold","new"],
        ["bronze","silver"],
        ["new","silver"],
        ["bronze","new"],
        ["gold"],
        ["silver"],
        ["bronze"],
        ["new"],
    ];

    const color = [
        ["#614E1A","#c49c48", "#f4f3f3", "#A5A49B"],
        ["#614E1A", "#c49c48", "#A5A49B"],
        ["#c49c48", "#f4f3f3", "#A5A49B"],
        ["#614E1A", "#c49c48", "#f4f3f3"],
        ["#614E1A", "#f4f3f3", "#A5A49B"],
        ["#c49c48", "#A5A49B"],
        ["#614E1A", "#c49c48"],
        ["#c49c48", "#f4f3f3"],
        ["#614E1A", "#A5A49B"],
        ["#f4f3f3", "#A5A49B"],
        ["#614E1A", "#f4f3f3"],
        ["#c49c48"],
        ["#A5A49B"],
        ["#614E1A"],
        ["#f4f3f3"],
    ];

    let i = 0;
    let foundColorValue;
    combi.forEach((combiArray, arrayIndex) => {
        combiArray.forEach( (combiLabel, labelIndex)=>{
              if(combiLabel === labels && foundColorValue === undefined){
                  
                 foundColorValue = color[arrayIndex][labelIndex];
                
              }
            

        });
        
      

    });
    return foundColorValue;

}

   let foundColor =  createArrayColor('silver');
   console.log('found ' + foundColor);

我在这里检查了许多教程和答案,但是我没有找到明确的示例。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您可以创建两个EntityManager

@Bean("em1")
public LocalContainerEntityManagerFactoryBean em1(
        EntityManagerFactoryBuilder builder) {
    return builder
            .dataSource(customDataSource1())
            .packages(DataSource.class)
            .build();
}

@Bean("em2")
public LocalContainerEntityManagerFactoryBean em2(
        EntityManagerFactoryBuilder builder) {
    return ...;
}

然后您可以在存储库中注入实体管理器

    @Repository
    class CustomerRepositoryOracle{
        @PersistenceContext(name="em1")
        private EntityManager entityManager;
    }

    @Repository
    class CustomerRepositoryPostgreSQL{
        @PersistenceContext(name="em2")
        private EntityManager entityManager;
    }