SpringBoot中怎么配置Jpa多数据源,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
创新互联建站专注于潢川网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供潢川营销型网站建设,潢川网站制作、潢川网页设计、潢川网站官网定制、小程序开发服务,打造潢川网络公司原创品牌,更为您提供潢川网站排名全网营销落地服务。
1.application.yml文件中,配置数据库源。这里primary是主库,secondary是从库。
server: port: 8089 # 多数据源配置 #primary spring: primary: datasource: url: jdbc:MySQL://127.0.0.1:3306/database1?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai username: root password: ****** driver-class-name: com.mysql.jdbc.Driver #secondary secondary: datasource: url: jdbc:mysql://127.0.0.1:3306/database1?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai username: root password: ****** driver-class-name: com.mysql.jdbc.Driver jpa: hibernate: primary-dialect: org.hibernate.dialect.MySQL5Dialect secondary-dialect: org.hibernate.dialect.MySQL5Dialect open-in-view: true show-sql: true |
2.创建一个Spring配置类,其中spring.primary.datasource的路径参考yml文件的配置。
@Configuration public class DataSourceConfig { @Bean(name = "primaryDataSource") @Qualifier("primaryDataSource") @ConfigurationProperties(prefix="spring.primary.datasource") public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "secondaryDataSource") @Qualifier("secondaryDataSource") @Primary @ConfigurationProperties(prefix="spring.secondary.datasource") public DataSource secondaryDataSource() { return DataSourceBuilder.create().build(); } } |
3.分别创建主库、从库的配置类。
注意:entity包和dao包的配置,以及@Primary注解指定主库。
主库配置类:
@Configuration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef = "entityManagerFactoryPrimary", transactionManagerRef = "transactionManagerPrimary", basePackages = {"com.xxx.xxx.dao.primary"}) //设置Repository所在位置 public class PrimaryConfig { @Autowired private JpaProperties jpaProperties; @Autowired @Qualifier("primaryDataSource") private DataSource primaryDataSource; @Primary @Bean(name = "entityManagerPrimary") public EntityManager entityManager(EntityManagerFactoryBuilder builder) { return entityManagerFactoryPrimary(builder).getObject().createEntityManager(); } @Primary @Bean(name = "entityManagerFactoryPrimary") public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) { return builder .dataSource(primaryDataSource) .properties(getVendorProperties(primaryDataSource)) .packages("com.infinitus.yunxiao_data.entity.primary") //设置实体类所在位置 .persistenceUnit("primaryPersistenceUnit") .build(); } private Map getVendorProperties(DataSource dataSource) { return jpaProperties.getHibernateProperties(dataSource); } @Primary @Bean(name = "transactionManagerPrimary") public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject()); } } |
从库的配置类:
@Configuration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef = "entityManagerFactorySecondary", transactionManagerRef = "transactionManagerSecondary", basePackages = {"com.infinitus.yunxiao_data.dao.secondary"}) //设置Repository所在位置 public class SecondaryConfig { @Autowired private JpaProperties jpaProperties; @Autowired @Qualifier("secondaryDataSource") private DataSource secondaryDataSource; @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.xxx.xxx.entity.secondary") //设置实体类所在位置 .persistenceUnit("primaryPersistenceUnit") .build(); } private Map getVendorProperties(DataSource dataSource) { return jpaProperties.getHibernateProperties(dataSource); } @Bean(name = "transactionManagerSecondary") PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject()); } } |
4.分别创建主、从库dao类。
主dao:
@Repository public interface PrimaryRepository extends JpaRepository |
从dao:
@Repository public interface SecondaryRepository extends JpaRepository |
5.分别创建主、从库entity类。
主entity:
@Entity @Table(name = "holiday_scheme") @EntityListeners(AuditingEntityListener.class) public class PrimaryEntity extends AbstractPersistable |
从entity:
@Entity @Table(name = "active_dashboards") @EntityListeners(AuditingEntityListener.class) public class SecondaryEntity extends AbstractPersistable |
6.controller请求获取不同数据库的数据。
@RestController @RequestMapping("/database") public class MailController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired PrimaryRepository primaryRepository; @Autowired SecondaryRepository secondaryRepository; @RequestMapping("/primary") @ResponseBody public String primary() { return primaryRepository.queryList().toString(); } @RequestMapping("/secondary") @ResponseBody public String secondary() { return secondaryRepository.queryList().toString(); } } |
注意
下面提两个在配置多数据源时遇到的坑点,一不注意就掉坑了。
1.Application类不需要配置@EnableJpaRepositories注解,会报如下错误。
A component required a bean named 'entityManagerFactory' that could not be f |
2.注意检查dao类,获取数据的方法上格式是否正确,有没有某个字段是表中不存在的,避免启动异常。如下,SecondaryEntity表中是不存在job_name字段的,所以注释掉才能启动成功等。
//@Query(value = "SELECT p FROM SecondaryEntity p where p.job_name = ?1") //ListqueryOdcRecord(String job_name);
关于SpringBoot中怎么配置Jpa多数据源问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。
当前名称:SpringBoot中怎么配置Jpa多数据源
URL标题:http://lswzjz.com/article/gjgesi.html