最近在学习springbatch,遇到了多个数据库相读写问题,问题类似于从A数据库读取数据,保存到B数据库,
1:先配置好数据库地址
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:mysql://127.0.0.1:3306/test?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-
password: root
password: roo
driver-class-name: com.mysql.jdbc.Driver
second-datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:mysql://127.0.0.1:3306/test1?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
2:利用springboot配置数据源
数据源
@Configuration public class DataSourceConfig { @Primary @Bean(name = "primaryDataSource") @Qualifier("primaryDataSource") @ConfigurationProperties(prefix = "spring.datasource") public DataSource primaryDatasource() { return DataSourceBuilder.create().build(); } // @Primary @Bean(name = "secondaryDataSource") @Qualifier("secondaryDataSource") @ConfigurationProperties(prefix = "spring.second-datasource") public DataSource secondaryDataSource() { return DataSourceBuilder.create().build(); } }
主数据库配置
@Configuration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef = "entityManagerFactoryPrimary",//配置连接工厂 entityManagerFactory transactionManagerRef = "transactionManagerPrimary", //配置 事物管理器 transactionManager basePackages = "com.ics.top.repository"//设置dao(repo)所在位置 ) public class PrimaryConfig { @Autowired private JpaProperties jpaProperties; @Autowired @Qualifier(