之前在介紹使用JdbcTemplate和Spring-data-jpa時,都使用了單數據源。在單數據源的情況下,Spring Boot的配置非常簡單,只需要在application.properties文件中配置連接參數即可。但是往往隨著業務量發展,我們通常會進行數據庫拆分或是引入其他數據庫,從而我們需要配置多個數據源,下面基于之前的JdbcTemplate和Spring-data-jpa例子分別介紹兩種多數據源的配置方式。
多數據源配置
創建一個Spring配置類,定義兩個DataSource用來讀取application.properties中的不同配置。如下例子中,主數據源配置為spring.datasource.primary開頭的配置,第二數據源配置為spring.datasource.secondary開頭的配置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@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(); } } |
對應的application.properties配置如下:
1
2
3
4
5
6
7
8
|
spring.datasource.primary.url=jdbc:mysql://localhost:3306/test1 spring.datasource.primary.username=root spring.datasource.primary.password=root spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver spring.datasource.secondary.url=jdbc:mysql://localhost:3306/test2 spring.datasource.secondary.username=root spring.datasource.secondary.password=root spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver |
JdbcTemplate支持
對JdbcTemplate的支持比較簡單,只需要為其注入對應的datasource即可,如下例子,在創建JdbcTemplate的時候分別注入名為primaryDataSource和secondaryDataSource的數據源來區分不同的JdbcTemplate。
1
2
3
4
5
6
7
8
9
10
|
@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); } |
接下來通過測試用例來演示如何使用這兩個針對不同數據源的JdbcTemplate。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
@RunWith (SpringJUnit4ClassRunner. class ) @SpringApplicationConfiguration (Application. class ) public class ApplicationTests { @Autowired @Qualifier ( "primaryJdbcTemplate" ) protected JdbcTemplate jdbcTemplate1; @Autowired @Qualifier ( "secondaryJdbcTemplate" ) protected JdbcTemplate jdbcTemplate2; @Before public void setUp() { jdbcTemplate1.update( "DELETE FROM USER " ); jdbcTemplate2.update( "DELETE FROM USER " ); } @Test public void test() throws Exception { // 往第一個數據源中插入兩條數據 jdbcTemplate1.update( "insert into user(id,name,age) values(?, ?, ?)" , 1 , "aaa" , 20 ); jdbcTemplate1.update( "insert into user(id,name,age) values(?, ?, ?)" , 2 , "bbb" , 30 ); // 往第二個數據源中插入一條數據,若插入的是第一個數據源,則會主鍵沖突報錯 jdbcTemplate2.update( "insert into user(id,name,age) values(?, ?, ?)" , 1 , "aaa" , 20 ); // 查一下第一個數據源中是否有兩條數據,驗證插入是否成功 Assert.assertEquals( "2" , jdbcTemplate1.queryForObject( "select count(1) from user" , String. class )); // 查一下第一個數據源中是否有兩條數據,驗證插入是否成功 Assert.assertEquals( "1" , jdbcTemplate2.queryForObject( "select count(1) from user" , String. class )); } } |
完整示例:Chapter3-2-3
Spring-data-jpa支持
對于數據源的配置可以沿用上例中DataSourceConfig的實現。
新增對第一數據源的JPA配置,注意兩處注釋的地方,用于指定數據源對應的Entity實體和Repository定義位置,用@Primary區分主數據源。
新增對第二數據源的JPA配置,內容與第一數據源類似,具體如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
@Configuration @EnableTransactionManagement @EnableJpaRepositories ( entityManagerFactoryRef= "entityManagerFactorySecondary" , transactionManagerRef= "transactionManagerSecondary" , basePackages= { "com.didispace.domain.s" }) //設置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( "com.didispace.domain.s" ) //設置實體類所在位置 .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()); } } |
完成了以上配置之后,主數據源的實體和數據訪問對象位于:com.didispace.domain.p,次數據源的實體和數據訪問接口位于:com.didispace.domain.s。
分別在這兩個package下創建各自的實體和數據訪問接口
主數據源下,創建User實體和對應的Repository接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@Entity public class User { @Id @GeneratedValue private Long id; @Column (nullable = false ) private String name; @Column (nullable = false ) private Integer age; public User(){} public User(String name, Integer age) { this .name = name; this .age = age; } // 省略getter、setter } public interface UserRepository extends JpaRepository<User, Long> { } |
從數據源下,創建Message實體和對應的Repository接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@Entity public class Message { @Id @GeneratedValue private Long id; @Column (nullable = false ) private String name; @Column (nullable = false ) private String content; public Message(){} public Message(String name, String content) { this .name = name; this .content = content; } // 省略getter、setter } public interface MessageRepository extends JpaRepository<Message, Long> { } |
接下來通過測試用例來驗證使用這兩個針對不同數據源的配置進行數據操作。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
@RunWith (SpringJUnit4ClassRunner. class ) @SpringApplicationConfiguration (Application. class ) public class ApplicationTests { @Autowired private UserRepository userRepository; @Autowired private MessageRepository messageRepository; @Test public void test() throws Exception { userRepository.save( new User( "aaa" , 10 )); userRepository.save( new User( "bbb" , 20 )); userRepository.save( new User( "ccc" , 30 )); userRepository.save( new User( "ddd" , 40 )); userRepository.save( new User( "eee" , 50 )); Assert.assertEquals( 5 , userRepository.findAll().size()); messageRepository.save( new Message( "o1" , "aaaaaaaaaa" )); messageRepository.save( new Message( "o2" , "bbbbbbbbbb" )); messageRepository.save( new Message( "o3" , "cccccccccc" )); Assert.assertEquals( 3 , messageRepository.findAll().size()); } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnn6.net/html/java/2017723/202385.html