spring boot2.x多数据源配置

perfect start 3月前 ⋅ 254 阅读

jpa多数据源配置和mybatis数据源配置

  • spring boot2.x

maven依赖

<dependencies>
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-yaml</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jooq</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
</dependencies>

配置文件(如上)

@Configuration
public class DataSourceConfig {

	@Bean
   @Primary
   @ConfigurationProperties("spring.datasource.primary")
   public DataSourceProperties firstDataSourceProperties() {
       return new DataSourceProperties();
	}
   
	@Bean
   @Primary
   @ConfigurationProperties("spring.datasource.primary.configuration")
   public HikariDataSource firstDataSource() {
       return firstDataSourceProperties().initializeDataSourceBuilder()
               .type(HikariDataSource.class).build();
   }

	
	@Bean
   @ConfigurationProperties("spring.datasource.secondary")
   public DataSourceProperties secondDataSourceProperties() {
       return new DataSourceProperties();
   }

   @Bean
   @ConfigurationProperties("spring.datasource.secondary.configuration")
   public HikariDataSource secondDataSource() {
       return secondDataSourceProperties().initializeDataSourceBuilder()
               .type(HikariDataSource.class).build();
   }
	
   
   @Bean(name = "mybatisDataSourceProperties")
   @Qualifier("mybatisDataSourceProperties")
   @ConfigurationProperties("spring.datasource.mybatis")
   public DataSourceProperties mybatisDataSourceProperties() {
       return new DataSourceProperties();
   }
   
   @Bean(name = "myBatisDataSource")
   @Qualifier("myBatisDataSource")
   @ConfigurationProperties(prefix="spring.datasource.mybatis")
   public DataSource myBatisDataSource() {
   	System.out.println("myBatisDataSource");
   	return mybatisDataSourceProperties().initializeDataSourceBuilder().build();
   }
}

@Configuration
@MapperScan(basePackages = {MyBatisDataSourceConfig.MAPPER_PACKAGE})
public class MyBatisDataSourceConfig {
	public static final String MAPPER_PACKAGE = "zyw.okgoes.dao";
	 
   public static final String MAPPER_XML_PACKAGE = "classpath:mybatis/mapper/*.xml";

   public static final String MYBATIS_BEAN_PACKAGE = "zyw.okgoes.entities";
   
   public static final String CONFIG_LOCATION = "classpath:mybatis/mybatis.cfg.xml";
   
   @Autowired
   @Qualifier("myBatisDataSource")
   private DataSource dataSource;
   
   @Bean(name = "sqlSessionFactory")
   public SqlSessionFactory sqlSessionFactory() throws Exception {
       SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
       sqlSessionFactoryBean.setDataSource(dataSource);
       PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
       //设置扫描 mybatis-config.xml
       sqlSessionFactoryBean.setConfigLocation(resolver.getResource(CONFIG_LOCATION));
       //设置扫描mapper.xml
       Resource[] resources = resolver.getResources(MAPPER_XML_PACKAGE);
       sqlSessionFactoryBean.setMapperLocations(resources);
       //设置扫描实体类
       sqlSessionFactoryBean.setTypeAliasesPackage(MYBATIS_BEAN_PACKAGE);
       return sqlSessionFactoryBean.getObject();
   }

   @Bean
   @Primary
   public SqlSessionTemplate sqlSessionTemplate() throws Exception {
       return new SqlSessionTemplate(sqlSessionFactory());
   }

   @Primary
   @Bean(name = "transactionManager")
   public DataSourceTransactionManager transactionManager() {
       return new DataSourceTransactionManager(dataSource);
   }
}

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
       entityManagerFactoryRef = "entityManagerFactoryPrimary",
       transactionManagerRef = "transactionManagerPrimary",
       basePackages = {"zyw.okgoes.jpa.dao.primary"})
public class PrimaryJpaDataSourceConfig {
   
   @Bean
   @Primary
   public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(
           EntityManagerFactoryBuilder builder, @Qualifier("firstDataSource") DataSource dataSource) {
       return builder
               .dataSource(dataSource)
               .packages("zyw.okgoes.jpa.entities.primary")
               .persistenceUnit("first")
               .build();
   }
   @Bean(name="transactionManagerPrimary")
   @Qualifier("transactionManagerPrimary")
   public PlatformTransactionManager primaryTransactionManager(
           @Qualifier("entityManagerFactoryPrimary") LocalContainerEntityManagerFactoryBean entityManagerFactory) {
       return new JpaTransactionManager(entityManagerFactory.getObject());
   }
}

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
       entityManagerFactoryRef = "entityManagerFactorySecondary",
       transactionManagerRef = "transactionManagerSecondary",
       basePackages = {"zyw.okgoes.jpa.dao.secondary"})
public class SecondaryJpaDataSourceConfig {
	@Bean
   public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary(
           EntityManagerFactoryBuilder builder, @Qualifier("secondDataSource") DataSource dataSource) {
       return builder
               .dataSource(dataSource)
               .packages("zyw.okgoes.jpa.entities.secondary")
               .persistenceUnit("second")
               .build();
   }

   

   @Bean(name = "transactionManagerSecondary")
   @Qualifier("transactionManagerSecondary")
   public PlatformTransactionManager backupTransactionManager(
           @Qualifier("entityManagerFactorySecondary") LocalContainerEntityManagerFactoryBean entityManagerFactory) {
       return new JpaTransactionManager(entityManagerFactory.getObject());
   }
}

全部评论: 0

    我有话说: