springBoot 和 jpa整合mysql数据源和oracle数据源完成数据转移

首先我们要选择相应的springBoot版本和相应的jar包


        org.springframework.boot
        spring-boot-starter-parent
        1.5.9.RELEASE
         
    
     
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
        
            mysql
            mysql-connector-java
            8.0.13
            runtime
        
        
        
            org.projectlombok
            lombok
            true
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            com.alibaba
            fastjson
            1.2.31
        
        
            ojdbc
            ojdbc-sdk
            1.0
            system
            ${project.basedir}/src/main/resources/lib/ojdbc14.jar
        

这里选择1.5.9 版本的springBoot 是因为我这种配置方式2.0以上有方法被取代,大家可以按照我这种方式。
这里的最后一个jar包是oracle 的驱动包 因为无法通过maven仓库下载通过这种方式打入maven仓库 包 目录如下:

springBoot 和 jpa整合mysql数据源和oracle数据源完成数据转移_第1张图片

配置文件内容如下

如下是配置mysql和oracle数据源,并且配置不同数据源的方言

server:
  port: 8086
# 多数据源配置
#primary
spring:
  primary:
    datasource:
      url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useOldAliasMetadataBehavior=true&serverTimezone=UTC
      username: root
      password: root
      driver-class-name: com.mysql.cj.jdbc.Driver
  #secondary
  secondary:
    datasource:
      url: jdbc:oracle:thin:@192.168.2.145:1521:orcl
      username: 你的密码
      password: 你的帐号
      driver-class-name: oracle.jdbc.driver.OracleDriver

  jpa:
    hibernate:
      primary-dialect: org.hibernate.dialect.MySQL5Dialect
      secondary-dialect: org.hibernate.dialect.OracleDialect
    open-in-view: true
    show-sql: true

接下来通过配置让spring管理这2个数据源

配置代码如下:

@Configuration
public class ProjectConfiguration {

    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.primary.datasource")
    public DataSource primaryDatasource() {
        return DataSourceBuilder.create().build();
    }

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

代码说明:@Primary这个注解是说明前缀spring.primary.datasource 的数据源为默认数据源

接下来开始配置把不同的表影射到不同的数据库中

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryPrimary",//配置连接工厂 entityManagerFactory
        transactionManagerRef = "transactionManagerPrimary", //配置 事物管理器  transactionManager
        basePackages = {"com.zrar.datatransfer.mysqlentity"}//设置持久层所在位置
)
@ComponentScan("com.zrar.datatransfer.oracleentity")
public class PrimaryConfiguration {
    @Autowired
    private JpaProperties jpaProperties;

    @Autowired
    @Qualifier("primaryDataSource")
    private DataSource primaryDataSource;// 自动注入配置好的数据源

    @Value("${spring.jpa.hibernate.primary-dialect}")
    private String primaryDialect;// 获取对应的数据库方言


    /**
     * @param builder
     * @return
     */

    @Bean(name = "entityManagerPrimary")
    @Primary
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
    }

    @Bean(name = "entityManagerFactoryPrimary")
    @Primary
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {

        return builder
                //设置数据源
                .dataSource(primaryDataSource)
                //设置数据源属性
                .properties(getVendorProperties(primaryDataSource))
                //设置实体类所在位置.扫描所有带有 @Entity 注解的类
                .packages("com.zrar.datatransfer.mysqlentity")
                // Spring会将EntityManagerFactory注入到Repository之中.有了 EntityManagerFactory之后,
                // Repository就能用它来创建 EntityManager 了,然后 EntityManager 就可以针对数据库执行操作
                .persistenceUnit("primaryPersistenceUnit")
                .build();

    }

    private Map getVendorProperties(DataSource dataSource) {
        Map map = new HashMap<>();
        map.put("hibernate.dialect", primaryDialect);// 设置对应的数据库方言
        jpaProperties.setProperties(map);
        return jpaProperties.getHibernateProperties(dataSource);
    }


    /**
     * 配置事物管理器
     *
     * @param builder
     * @return
     */


    @Bean(name = "transactionManagerPrimary")
    @Primary
    PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
    }


}

下面是第2个数据库的表映射

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactorySecondary",//配置连接工厂 entityManagerFactory
        transactionManagerRef = "transactionManagerSecondary", //配置 事物管理器  transactionManager
        basePackages = {"com.zrar.datatransfer.oracleentity"}//设置持久层所在位置
)
@ComponentScan("com.zrar.datatransfer.oracleentity")
public class SecondConfiguration {
    @Autowired
    private JpaProperties jpaProperties;

    @Autowired
    @Qualifier("secondaryDataSource")
    private DataSource secondaryDataSource;

    @Value("${spring.jpa.hibernate.secondary-dialect}")
    private String secondaryDialect;


    @Bean(name = "entityManagerSecondary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactorySecondary(builder).getObject().createEntityManager();
    }

    @Bean(name = "entityManagerFactorySecondary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(secondaryDataSource)
                .properties(getVendorProperties(secondaryDataSource))
                .packages("com.zrar.datatransfer.oracleentity")
                .persistenceUnit("secondaryPersistenceUnit")
                .build();
    }

    private Map getVendorProperties(DataSource dataSource) {
        Map map = new HashMap<>();
        map.put("hibernate.dialect", secondaryDialect);
        jpaProperties.setProperties(map);
        return jpaProperties.getHibernateProperties(dataSource);
    }

    @Bean(name = "transactionManagerSecondary")
    PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());

    }
}

说明:@ComponentScan(“com.zrar.datatransfer.oracleentity”) 这个注解大家可以省略 我这个怕扫描不到加上去的

项目目录如下

springBoot 和 jpa整合mysql数据源和oracle数据源完成数据转移_第2张图片

这样我们就可以使用不同的dao来操作相应的表。

你可能感兴趣的:(笔记)