spring-boot 配置多数据源多orm框架混用

解决问题

配置多数据源 mysql+oracle
配置多个orm映射框架 jpa+mybatis

配置多数据源

application.properties 增加

spring.datasource.validation-query=select 1
spring.datasource.test-on-borrow=true
spring.datasource.test-while-idle=true

datasource.mysql.driverClassName=com.mysql.jdbc.Driver
datasource.mysql.url=jdbc:mysql://localhost:3306/database_name?createDatabaseIfNotExist=true&useUnicode=true&autoReconnect=true
datasource.mysql.username=username
datasource.mysql.password=password

datasource.oracle.driverClassName=oracle.jdbc.OracleDriver
datasource.oracle.url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl
datasource.oracle.username=username
datasource.oracle.password=password

增加数据源配置类

@Configuration
public class DataSourceConfig {

    @Bean(name = "mysqlDS")
    @Primary   //默认数据源
    @ConfigurationProperties(prefix = "datasource.mysql")
    public DataSource mysqlDataSource() {
        return DataSourceBuilder.create().build();
    }

  @Bean(name = "oracleDS")
  @ConfigurationProperties(prefix = "datasource.oracle")
  public DataSource oracleDataSource() {
    return DataSourceBuilder.create().build();
  }
}

这样多数据源就配置好了。

配置mybatis

增加mybatis配置类, 使用oracle数据源.


@Configuration
@MapperScan(basePackages ="com.wjm.mybatis.dao",sqlSessionFactoryRef="oracleSessionFactory")
public class MybatisConfig {

    @Bean(name="oracleSessionFactory")\
    @Autowired
    @Qualifier("oracleDS")
    public SqlSessionFactory sqlSessionFactory(DataSource datasource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(datasource);
        return sessionFactory.getObject();
    }
}

这样在com.wjm.mybatis.dao包下的类使用mybatis+oracle.

配置JPA

增加jpa配置类


@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "mysqlEntityManagerFactory", transactionManagerRef = "mysqlTransactionManager", basePackages = { "com.wjm.jpa.dao" })
public class JpaConfig {
    @Bean(name = "mysqlEntityManagerFactory")
    @Autowired
    @Qualifier("mysqlDS")
        public EntityManagerFactory  entityManagerFactory(DataSource datasource) {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);
        vendorAdapter.setShowSql(true);
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setDataSource(datasource);
        factory.setPackagesToScan("com.wjm.model");
        Properties jpaProperties = new Properties();
        jpaProperties.put("hibernate.ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy");//命名策略
        factory.setJpaProperties(jpaProperties);
        factory.afterPropertiesSet();
        return factory.getObject();

    }

    @Bean(name = "mysqlTransactionManager")
    public PlatformTransactionManager transactionManagerSecondary() {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory());
        return txManager;
    }

}

这样就配置好了Jpa+mysql数据源了。com.wjm.jpa.dao包下的类使用jpa
要管理的类放到com.wjm.model

你可能感兴趣的:(杂七杂八,j2ee,springboot,数据源,orm框架,配置)