您好,登錄后才能下訂單哦!
今天臨下班時遇到了一個需求,我的管理平臺需要從不同的數(shù)據(jù)庫中獲取數(shù)據(jù)信息,這就需要進行Spring的多數(shù)據(jù)源配置,對于這種配置,第一次永遠都是痛苦的,不過經(jīng)歷了這次的折磨,今后肯定會對這種配置印象深刻。我們這里簡單回顧一下流程。
我們配置了兩個數(shù)據(jù)庫,一個是公司的數(shù)據(jù)庫,另一個是我本地的一個數(shù)據(jù)庫。首先是application.yml的配置(其中對于公司的數(shù)據(jù)庫我們采取了假的地址,而本機的數(shù)據(jù)庫是真是存在對應的表和庫的)
數(shù)據(jù)庫信息:
數(shù)據(jù)表信息:
1、application.yml
datasource: primary: url: jdbc:mysql://companyurl.com:5002/db1 username: unameq password: passwd1 driver-class-name: com.mysql.jdbc.Driver secondary: url: jdbc:mysql://localhost:3306/django_test username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver jpa: database-platform: org.hibernate.dialect.MySQL5Dialect hibernate: ddl-auto: update show-sql: true
2、創(chuàng)建總的DataSource配置文件以及兩個Repostory的配置文件PrimaryConfig以及SecondaryConfig
DataSourceConfig
@Configuration public class DataSourceConfig { @Bean(name = "primaryDataSource") @Qualifier("primaryDataSource") @ConfigurationProperties(prefix="spring.datasource.primary")//對應的數(shù)據(jù)庫配置信息 public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "secondaryDataSource") @Qualifier("secondaryDataSource") @Primary @ConfigurationProperties(prefix="spring.datasource.secondary") public DataSource secondaryDataSource() { return DataSourceBuilder.create().build(); } }
PrimaryConfig
@Configuration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef="entityManagerFactoryPrimary", transactionManagerRef="transactionManagerPrimary", basePackages= { "數(shù)據(jù)訪問層所在的包" }) //設置Repository所在位置 public class PrimaryConfig { @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("實體類所在的包") //設置實體類所在位置 .persistenceUnit("primaryPersistenceUnit") .build(); } @Autowired private JpaProperties jpaProperties; private Map<String, String> getVendorProperties(DataSource dataSource) { return jpaProperties.getHibernateProperties(dataSource); } @Primary @Bean(name = "transactionManagerPrimary") public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject()); } }
SecondaryConfig
@Configuration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef="entityManagerFactorySecondary", transactionManagerRef="transactionManagerSecondary", basePackages= { "數(shù)據(jù)訪問層所在的包" }) //設置Repository所在位置 public class SecondaryConfig { @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("實體類所在的包") //設置實體類所在位置 .persistenceUnit("secondaryPersistenceUnit") .build(); } @Autowired private JpaProperties jpaProperties; private Map<String, String> getVendorProperties(DataSource dataSource) { return jpaProperties.getHibernateProperties(dataSource); } @Bean(name = "transactionManagerSecondary") PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject()); } }
3、然后我對于本地數(shù)據(jù)庫新建實體類PeoplePerson
@Entity @Table(name = "people_person") public class PeoplePerson implements Serializable { @Id @GeneratedValue private Integer id; @Column(name = "name") private String name; @Column(name = "age") private Integer age; public PeoplePerson() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "PeoplePerson{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
并創(chuàng)建對應的Repositoy,PeoplePersonDao并創(chuàng)建了一個findAll的方法
@Transactional@Repositorypublic interface PeoplePersonDao extends JpaRepository<PeoplePerson, Long> { List<PeoplePerson> findAll(); }
4、最后,在test包中進行測試
@Autowired private PeoplePersonDao peoplePersonDao; @Test public void testMultiDataSource() { List<PeoplePerson> list = peoplePersonDao.findAll(); for (int i = 0; i < list.size(); i++) { logger.info(list.get(i).toString()); } }
測試結果
一些坑
不僅僅是dao層掃描的包需要區(qū)分,對于實體類所在的包,不同的DataSource的配置中也需要區(qū)分開
對于這種套路性的東西,總結一遍是非常必要的,下次可以節(jié)省許多不必要的時間,對于內(nèi)部原理,我將在完成對Ioc和Aop分析后反過來分析其原理。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。