如何在springboot中配置两个数据库?

dy2hfwbg  于 2021-07-26  发布在  Java
关注(0)|答案(0)|浏览(279)

我对springboot都是新手,并且我已经阅读了有关如何创建springboot应用程序的som文档。
我已经在springboot中创建了一个应用程序,这个应用程序应该运行一个springbatch作业(我正在将springbatch中的一个旧作业重写为一个独立的springboot应用程序)。我用步骤等创建了作业的结构。现在所有的工作都是移动文件,这是可行的。我在开发期间使用嵌入式数据库,h2和springboot已经为springbatch生成了整个数据库。非常好:)
所以现在我的问题是,在其中一个步骤中,我必须在另一个数据库中获取和存储数据。我不知道(理解)我应该如何创建这个数据库和访问作业中的数据库。
所以在我的application.properties中

spring.h2.console.enabled=true
spring.h2.console.path=/h2

spring.datasource.url=jdbc:h2:mem:springdb
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

persons.h2.console.enabled=true
persons.h2.console.path=/h2

persons.datasource.url=jdbc:h2:mem:persons
persons.datasource.username=sa
persons.datasource.password=
persons.datasource.driverClassName=org.h2.Driver

在测试中,我将把数据库改为另一台服务器上的sql数据库。
我有一些实体(例如)

public class Person {

    private Long id;
    private String name;
    private String familyname;
    private Long birthDate;

    public Person () {
    }
...with getters and setters

在我的配置中

@Primary
    @Bean
    @ConfigurationProperties(prefix = "persons.datasource")
    public DataSource personsDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource batchDataSource() {
        return DataSourceBuilder.create().build();
    }

工作呢

@Autowired
    PersonItemWriter itemWriter;

    @Autowired
    PersonItemProcessor itemProcessor;

    @Autowired
    PersonItemReader workReader;

    @Bean(name = "personsImportJob")
    public Job personImportJob() {

        Step downloadFiles = stepBuilderFactory.get("download-files")
                .tasklet(downloadTasklet())
                .build();

        Step syncDbAndSavePersons = stepBuilderFactory.get("syncandsave-persons")
                .<File, Person>chunk(50)
                .reader(workReader)
                .processor(itemProcessor)
                .writer(itemWriter)
                .build();

        Step deleteFiles = stepBuilderFactory.get("delete-files")
                .tasklet(deleteTasklet())
                .build();

        Job job = jobBuilderFactory.get("personsimport-job")
                .incrementer(new RunIdIncrementer())
                .flow(downloadFiles)
                .next(syncDbAndSavePersons)
                .next(deleteFiles)
                .end()
                .build();

        return job;
    }

我的作者

@Component
public class PersonItemWriter implements ItemWriter<Person> {

    private static final Logger LOGGER = LoggerFactory.getLogger(PersonItemWriter.class);

    @Override
    public void write(List<? extends Person> list) throws Exception {
        LOGGER.info("Write Person to db");
    }

}

现在可以了,syncandsavepersons这个步骤现在什么都不做,但是我希望这个步骤可以访问另一个数据库并更新persons数据库的文章。
没有jpa我能做到吗?因为现有的作业不使用jpa,如果我必须使用jpa,那么会有很多代码更改,我希望避免这种情况。我只想把工作调动到最少,
如果我用这个运行我的应用程序,唯一创建的数据库就是springbatch的数据库。如何确保也创建了另一个数据库?或者当我使用h2嵌入式数据库时,这是不可能的?在添加第二个数据库之前,我的代码中根本没有数据源配置,但数据库还是创建了。我认为springboot刚刚创建了批处理数据源。所以也许我不需要那个配置?
更新:我通过删除te properties文件中数据库的属性来解决这个问题。我只剩下:

spring:
        datasource:
            initialization-mode: never
        h2:
            console:
                enabled: true

然后我创建了一个名为embeddeddatasourceconfig的类:

@Profile("default")
@Configuration
public class EmbeddedDataSourceConfig {

    @Bean
    @Primary
    public DataSource dataSource() {

        EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();

        return builder
                .setType(EmbeddedDatabaseType.H2)
                .addScript("classpath:/org/springframework/batch/core/schema-h2.sql")
                .build();
    }
   @Bean(name = "personsDataSource")
    public DataSource personsDataSource() {

        EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();

        return builder
                .setName("persons")
                .setType(EmbeddedDatabaseType.H2)
                .addScript("classpath:persons_schema.sql")
                .build();
    }

    @Bean(name = "personsTransactionManager")
    public PlatformTransactionManager personsTransactionManager(
            @Qualifier("personsDataSource") DataSource dataSource) {

        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "personsJdbcTemplate")
    public JdbcTemplate personsJdbcTemplate(@Qualifier("personsDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}

在工作中,我改为跟随

@Bean(name = "personsImportJob")
public Job personImportJob(@Qualifier("personsTransactionManager") PlatformTransactionManager personsTransactionManager) {

    Step downloadFiles = stepBuilderFactory.get("download-files")
            .tasklet(downloadTasklet())
            .build();

    Step syncDbAndSavePersons = stepBuilderFactory.get("syncandsave-persons")
            .<File, Person>chunk(50)
            .reader(workReader)
            .processor(itemProcessor)
            .writer(itemWriter)
            .transactionManager(personsTransactionManager)
            .build();
    ...
}

就这样。现在它在内存中生成两个h2数据库。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题