SpringBoot-------实现多数据源Demo
之前SpringBoot出来时候就看了下Springboot,感觉的确精简掉了配置文件!
还是很方便的!没办法,我只是个菜鸟!
什么怎么启动Springboot什么的就不说了,
具体的Demo地址我都是放在github上面,因为只有Demo才能更直观的学习和进去!
对其中出现的注解:
这里时对jpa进行的多数据源
@Configuration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef="entityManagerFactorySecondary", transactionManagerRef="transactionManagerSecondary",//这些都是默认的,看源码就知道了 basePackages= { "springboot.domain.s" }) //设置Repository所在位置
这些注解是为了注册和配置一个数据源具体操作的实体类有
代码:
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 }
其实都是固定的形式,你想挖掘就自己点进去看看,
当然这是数据源中得一个,另外一个也是一样的,
说到这:我们肯定要有配置数据源的配置文件,不然讲毛用都没!
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先生
梦想一定要有,万一实现了!