(原)springboot jpa 配置多数据源(多数据源:大于等于2个数据库)

异常:dataSource或dataSourceClassName或jdbcUrl是必需的。【下面有介绍】

因为光速没有URL属性(但是确实有一个JDBCURL属性)。在这种情况下,您必须重写您的配置如下

spring.datasource.primary.url=jdbc:mysql://IP地址/drp_db

spring.datasource.primary.jdbc-url=jdbc:mysql://IP地址/drp_db

 进入正题:

//感谢老师!------------------  https://blog.csdn.net/junqilong/article/details/79423687


首先创建DataSource,只需要为其注入对应的JdbcTemplate

/***
 * 配置多数据源
 * @author free-knight
 *
 */
@Configuration
public class DataSourceConfig {

		/***
		 * 配置主数据源
		 * @return
		 */
	  	@Bean(name = "primaryDataSource")
	    @Qualifier("primaryDataSource")
	    @ConfigurationProperties(prefix="spring.datasource.primary")
	    public DataSource primaryDataSource() {
	        return DataSourceBuilder.create().build();
	    }
	  	
	   /****
	    * 配置数据源	
	    * @return
	    */
	    @Bean(name = "secondaryDataSource")
	    @Qualifier("secondaryDataSource")
	    @Primary
	    @ConfigurationProperties(prefix="spring.datasource.secondary")
	    public DataSource secondaryDataSource() {
	        return DataSourceBuilder.create().build();
	    }
	    
	    @Bean(name = "primaryJdbcTemplate")
	    public JdbcTemplate primaryJdbcTemplate(
	            @Qualifier("primaryDataSource") DataSource dataSource) {
	        return new JdbcTemplate(dataSource);
	    }
	    
	    @Bean(name = "secondaryJdbcTemplate")
	    public JdbcTemplate secondaryJdbcTemplate(
	            @Qualifier("secondaryDataSource") DataSource dataSource) {
	        return new JdbcTemplate(dataSource);
	    }
}

 其次创建spring.xml文件的多数据源信息

(原)springboot jpa 配置多数据源(多数据源:大于等于2个数据库)_第1张图片

然后,数据源的JPA配置(几个数据源,几个相应名称匹配的配置,自己理解,后期尽量改成批量方式):注意注释的部分  

/****
 * 配置主数据源
 * @author wangning
 *
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactoryPrimary",
        transactionManagerRef="transactionManagerPrimary",
        basePackages= { "com.primary.*.repository.*" }) //设置Repository所在位置
public class PrimaryConfig {
	
    @Autowired 
    @Qualifier("primaryDataSource")
    private DataSource primaryDataSource;
    
    @Autowired 
    private Properties jpaPrimaryProperties;
    
    @Primary
    @Bean(name = "entityManagerPrimary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
    }
    
    @Primary
    @Bean(name = "entityManagerFactoryPrimary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
////////////////////////////////////////
//这一部分 自己修改,网上内容不是这样,但是引用后,不显示有问题存在,所以自己改成如下
    	LocalContainerEntityManagerFactoryBean entityManagerFactory 
    				=  builder
		    				.dataSource(primaryDataSource)
		    				.packages("com.primary.*.entity.*")//设置实体类所在位置
		    				.persistenceUnit("primaryPersistenceUnit")//持久化单元创建一个默认即可,多个便要分别命名
		    				.build();
    	 entityManagerFactory.setJpaProperties(jpaPrimaryProperties);
    	 return entityManagerFactory;
/////////////////////////////////////////////////////////////
    }
  
    @Primary
    @Bean(name = "transactionManagerPrimary")
    public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
    }
/*****
 * 配置从数据源
 * @author free-knight
 *
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactorySecondary",
        transactionManagerRef="transactionManagerSecondary",
        basePackages= { "com.secondary.*.repository.*" }) //设置Repository所在位置
public class SecondaryConfig {
	
    @Autowired 
    @Qualifier("secondaryDataSource")
    private DataSource secondaryDataSource;
    
    @Autowired
    private Properties jpaSecondaryProperties;
    
    
    @Bean(name = "entityManagerSecondary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactorySecondary(builder).getObject().createEntityManager();
    }
    
    @Bean(name = "entityManagerFactorySecondary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
    	
    	LocalContainerEntityManagerFactoryBean entityManagerFactory 
					=  builder
							.dataSource(secondaryDataSource)
							.packages("com.secondary.*.entity.*")//设置实体类所在位置
							.persistenceUnit("secondaryPersistenceUnit")
							.build();
			entityManagerFactory.setJpaProperties(jpaSecondaryProperties);
			return entityManagerFactory;
    }
    
    @Bean(name = "transactionManagerSecondary")
    PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
    }

}

 

最后,分别在这两个包下创建各自的实体和数据访问接口

测试结果:

(原)springboot jpa 配置多数据源(多数据源:大于等于2个数据库)_第2张图片


以上是自己整理的,并测试过,可以直接用

-------------------------------------------------- -------------------------------------------------- ------------

文章中,有问题,可以在评论区评论,一起探讨编程中奥秘!

-------------------------------------------------- -------------------------------------------------- ------------

来都来了,代码看都看了,那就留个言呗,可以互动下!

-------------------------------------------------- -------------------------------------------------- ------------

转载声明:本文为博主原创文章,未经博主允许不得转载

你可能感兴趣的:(----------后台,Springboot,体系)