以前SpringBoot出來時候就看了下Springboot,感受的確精簡掉了配置文件!java
仍是很方便的!沒辦法,我只是個菜鳥!mysql
什麼怎麼啓動Springboot什麼的就不說了,git
具體的Demo地址我都是放在github上面,由於只有Demo才能更直觀的學習和進去!github
對其中出現的註解:web
這裏時對jpa進行的多數據源spring
@Configuration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef="entityManagerFactorySecondary", transactionManagerRef="transactionManagerSecondary",//這些都是默認的,看源碼就知道了 basePackages= { "springboot.domain.s" }) //設置Repository所在位置
這些註解是爲了註冊和配置一個數據源具體操做的實體類有
代碼:sql
1 package springboot; 2 import org.springframework.beans.factory.annotation.Autowired; 3 import org.springframework.beans.factory.annotation.Qualifier; 4 import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; 5 import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; 6 import org.springframework.context.annotation.Bean; 7 import org.springframework.context.annotation.Configuration; 8 import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 9 import org.springframework.orm.jpa.JpaTransactionManager; 10 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 11 import org.springframework.transaction.PlatformTransactionManager; 12 import org.springframework.transaction.annotation.EnableTransactionManagement; 13 14 import javax.persistence.EntityManager; 15 import javax.sql.DataSource; 16 import java.util.Map; 17 18 @Configuration 19 @EnableTransactionManagement 20 @EnableJpaRepositories( 21 entityManagerFactoryRef="entityManagerFactorySecondary", 22 transactionManagerRef="transactionManagerSecondary", 23 basePackages= { "springboot.domain.s" }) //設置Repository所在位置 24 public class SecondaryConfig { 25 26 @Autowired @Qualifier("secondaryDataSource") 27 private DataSource secondaryDataSource; 28 29 @Bean(name = "entityManagerSecondary") 30 public EntityManager entityManager(EntityManagerFactoryBuilder builder) { 31 return entityManagerFactorySecondary(builder).getObject().createEntityManager(); 32 } 33 34 @Bean(name = "entityManagerFactorySecondary") 35 public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) { 36 return builder 37 .dataSource(secondaryDataSource) 38 .properties(getVendorProperties(secondaryDataSource)) 39 .packages("springboot.domain.s") //設置實體類所在位置 40 .persistenceUnit("secondaryPersistenceUnit") 41 .build(); 42 } 43 44 @Autowired 45 private JpaProperties jpaProperties; 46 47 private Map<String, String> getVendorProperties(DataSource dataSource) { 48 return jpaProperties.getHibernateProperties(dataSource); 49 } 50 51 @Bean(name = "transactionManagerSecondary") 52 PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) { 53 return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject()); 54 } 55 56 }
其實都是固定的形式,你想挖掘就本身點進去看看,apache
固然這是數據源中得一個,另一個也是同樣的,springboot
說到這:咱們確定要有配置數據源的配置文件,否則講毛用都沒!app
application.properties
1 spring.datasource.primary.url=jdbc:mysql://localhost:3306/test1 2 spring.datasource.primary.username=root 3 spring.datasource.primary.password=123456 4 spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver 5 6 spring.datasource.secondary.url=jdbc:mysql://localhost:3306/test2 7 spring.datasource.secondary.username=root 8 spring.datasource.secondary.password=123456 9 spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver 10 server.port=80 11 spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop
講到配置文件中得二個數據源,那麼咱們怎麼獲取到這二個數據源呢,如何加載進數據源呢,-----------------------------
-------------------擦--------------------------擦----------------------摩擦---------------------------------
1 package springboot.datasource.config; 2 3 import javax.sql.DataSource; 4 5 import org.springframework.beans.factory.annotation.Qualifier; 6 import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 7 import org.springframework.boot.context.properties.ConfigurationProperties; 8 import org.springframework.context.annotation.Bean; 9 import org.springframework.context.annotation.Configuration; 10 import org.springframework.context.annotation.Primary; 11 12 @Configuration 13 public class DataSourceConfig { 14 15 16 /** 17 * 主數據源 18 * @return 19 */ 20 @Bean(name = "primaryDataSource") 21 @Qualifier("primaryDataSource") 22 @ConfigurationProperties(prefix="spring.datasource.primary")//讀取配置文件中註冊數據源,對@bean不熟悉的能夠看我Spring文章中一個有這篇文章 23 public DataSource primaryDataSource() { 24 return DataSourceBuilder.create().build(); 25 } 26 /** 27 * 子數據源 28 * @return 29 */ 30 @Bean(name = "secondaryDataSource") 31 @Qualifier("secondaryDataSource") 32 @Primary 33 @ConfigurationProperties(prefix="spring.datasource.secondary") 34 public DataSource secondaryDataSource() { 35 return DataSourceBuilder.create().build(); 36 } 37 38 39 }
剩下的部分,就是你該幹嗎幹嗎了,該建啥建啥。。。。。
貼下pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>tbnb.cn</groupId> 4 <artifactId>SpringBootMuchDataSource</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <parent> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-parent</artifactId> 9 <version>1.3.2.RELEASE</version> 10 <relativePath/> <!-- lookup parent from repository --> 11 </parent> 12 13 <properties> 14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 15 <java.version>1.7</java.version> 16 </properties> 17 18 <dependencies> 19 <dependency> 20 <groupId>org.springframework.boot</groupId> 21 <artifactId>spring-boot-starter</artifactId> 22 </dependency> 23 24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-test</artifactId> 27 <scope>test</scope> 28 </dependency> 29 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-starter-web</artifactId> 33 </dependency> 34 <dependency> 35 <groupId>mysql</groupId> 36 <artifactId>mysql-connector-java</artifactId> 37 <version>5.1.21</version> 38 </dependency> 39 40 <dependency> 41 <groupId>org.springframework.boot</groupId> 42 <artifactId>spring-boot-starter-data-jpa</artifactId> 43 </dependency> 44 </dependencies> 45 46 <build> 47 <plugins> 48 <plugin> 49 <groupId>org.springframework.boot</groupId> 50 <artifactId>spring-boot-maven-plugin</artifactId> 51 </plugin> 52 </plugins> 53 </build> 54 55 </project>
-----加油!Z先生