您好,登錄后才能下訂單哦!
今天臨下班時遇到了一個需求,我的管理平臺需要從不同的數據庫中獲取數據信息,這就需要進行Spring的多數據源配置,對于這種配置,第一次永遠都是痛苦的,不過經歷了這次的折磨,今后肯定會對這種配置印象深刻。我們這里簡單回顧一下流程。
我們配置了兩個數據庫,一個是公司的數據庫,另一個是我本地的一個數據庫。首先是application.yml的配置(其中對于公司的數據庫我們采取了假的地址,而本機的數據庫是真是存在對應的表和庫的)
數據庫信息:
數據表信息:
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、創建總的DataSource配置文件以及兩個Repostory的配置文件PrimaryConfig以及SecondaryConfig
DataSourceConfig
@Configuration public class DataSourceConfig { @Bean(name = "primaryDataSource") @Qualifier("primaryDataSource") @ConfigurationProperties(prefix="spring.datasource.primary")//對應的數據庫配置信息 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= { "數據訪問層所在的包" }) //設置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= { "數據訪問層所在的包" }) //設置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、然后我對于本地數據庫新建實體類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 + '}'; } }
并創建對應的Repositoy,PeoplePersonDao并創建了一個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層掃描的包需要區分,對于實體類所在的包,不同的DataSource的配置中也需要區分開
對于這種套路性的東西,總結一遍是非常必要的,下次可以節省許多不必要的時間,對于內部原理,我將在完成對Ioc和Aop分析后反過來分析其原理。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。