亲宝软件园·资讯

展开

springboot jpa多个数据源 spring boot springjpa 支持多个数据源的实例代码

ba5189tsl 人气:0
想了解spring boot springjpa 支持多个数据源的实例代码的相关内容吗,ba5189tsl在本文为您仔细讲解springboot jpa多个数据源的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:springboot,jpa多个数据源,spring,jpa,多数据源,spring,boot,jpa,下面大家一起来学习吧。

1.SpringBoot的程序启动类

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.boot.builder.SpringApplicationBuilder; 
import org.springframework.boot.context.web.SpringBootServletInitializer; 
import org.springframework.context.annotation.ComponentScan; 
@ComponentScan 
@EnableAutoConfiguration 
//@EnableJpaRepositories(basePackages = "com.sonychina.backend.repository") 
public class Application extends SpringBootServletInitializer { 
  public static void main(String[] args) { 
    SpringApplication app = new SpringApplication(Application.class); 
    app.run(args); 
    //SpringApplication.run(Application.class, args); 
  } 
  @Override 
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
    return application.sources(Application.class); 
  } 
} 

 2.双数据源配置类

import java.util.Map; 
import javax.sql.DataSource; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 
import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilder; 
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; 
import org.springframework.boot.context.properties.ConfigurationProperties; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.Primary; 
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 
import com.test.entity.statistic.SysUser; 
import com.test.repository.system.SystemRepository; 
@Configuration 
@EnableJpaRepositories(entityManagerFactoryRef="entityManagerFactoryPrimary", 
  basePackageClasses= {SystemRepository.class}) 
public class GlobalDataConfiguration { 
// @Autowired 
// private DBConfig dbConfig; 
  @Autowired 
  private JpaProperties jpaProperties; 
  @Bean(name="primaryDataSource") 
  @Primary 
  @ConfigurationProperties(prefix="datasource.primary") 
  public DataSource primaryDataSource() { 
    System.out.println("-------------------- primaryDataSource init ---------------------"); 
    return DataSourceBuilder.create().build(); 
  } 
  @Bean(name="secondaryDataSource") 
  @ConfigurationProperties(prefix="datasource.secondary") 
  public DataSource secondaryDataSource() { 
    System.out.println("-------------------- secondaryDataSource init ---------------------"); 
//   DataSourceBuilder factory = DataSourceBuilder  
//       .create(DBConfig.class.getClassLoader())  
//       .driverClassName(dbConfig.getDriver())  
//       .url(dbConfig.getUrl())  
//       .username(dbConfig.getUser())  
//       .password(dbConfig.getPassword());  
//   return factory.build(); 
    return DataSourceBuilder.create().build(); 
  } 
// @Bean(name = "entityManagerPrimary") 
// @Primary 
// public EntityManager entityManager(EntityManagerFactoryBuilder builder) { 
//   return customerEntityManagerFactory(builder).getObject().createEntityManager(); 
// } 
  @Bean(name="entityManagerFactoryPrimary") 
  @Primary 
  public LocalContainerEntityManagerFactoryBean customerEntityManagerFactory(EntityManagerFactoryBuilder builder) { 
    return builder.dataSource(primaryDataSource()) 
          .properties(getVendorProperties(primaryDataSource())) 
          .packages(SysUser.class) 
          .persistenceUnit("system") 
          .build(); 
  } 
  private Map<String, String> getVendorProperties(DataSource dataSource) { 
    return jpaProperties.getHibernateProperties(dataSource); 
  } 
} 

3.第二个jpa实体管理器

import java.util.Map; 
import javax.sql.DataSource; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Qualifier; 
import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilder; 
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 
import org.springframework.orm.jpa.JpaTransactionManager; 
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 
import org.springframework.transaction.PlatformTransactionManager; 
import org.springframework.transaction.annotation.EnableTransactionManagement; 
import com.test.entity.manage.Banner; 
import com.test.repository.manage.BannerRepository; 
@Configuration 
@EnableTransactionManagement 
@EnableJpaRepositories(entityManagerFactoryRef="entityManagerFactorySecondary", 
  transactionManagerRef="transactionManagerSecondary", 
  basePackageClasses= {BannerRepository.class}) 
public class SecondEMFBConfig { 
  @Autowired 
  private JpaProperties jpaProperties; 
  @Autowired@Qualifier("secondaryDataSource") 
  private DataSource dataSource; 
// @Bean(name = "entityManagerPrimary") 
// @Primary 
// public EntityManager entityManager(EntityManagerFactoryBuilder builder) { 
//   return customerEntityManagerFactory(builder).getObject().createEntityManager(); 
// } 
  @Bean(name="entityManagerFactorySecondary") 
  public LocalContainerEntityManagerFactoryBean customerEntityManagerFactory(EntityManagerFactoryBuilder builder) { 
    return builder.dataSource(dataSource) 
          .properties(getVendorProperties(dataSource)) 
          .packages(Banner.class) 
          .persistenceUnit("customers") 
          .build(); 
  } 
  private Map<String, String> getVendorProperties(DataSource dataSource) { 
    return jpaProperties.getHibernateProperties(dataSource); 
  } 
  @Bean(name = "transactionManagerSecondary") 
  PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) { 
    return new JpaTransactionManager(customerEntityManagerFactory(builder).getObject()); 
  } 
} 

4.repository类举例

import org.springframework.data.jpa.repository.JpaRepository; 
import org.springframework.data.jpa.repository.Modifying; 
import org.springframework.data.jpa.repository.Query; 
import com.test.entity.manage.Banner; 
public interface BannerRepository extends JpaRepository<Banner, Long> { 
  @Modifying 
  @Query("update Banner m set m.name=?1 where m.id=?2") 
  public void update(String bannerName, Long id); 
} 

1.5.注意:对@Primary修饰的LocalContainerEntityManagerFactoryBean可以不用指定TransactionManager,spring上下文自动使用默认的JpaTransactionManager,但是对于第二个或第三个等等必须指定TransactionManager。可以参考springboot官方文档中的相关章节。 

总结

以上所述是小编给大家介绍的spring boot springjpa 支持多个数据源的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

加载全部内容

相关教程
猜你喜欢
用户评论