Skip to content

Commit c32fadd

Browse files
many db source
1 parent 105a3d4 commit c32fadd

File tree

3 files changed

+87
-4
lines changed

3 files changed

+87
-4
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.common;
2+
3+
import org.springframework.beans.factory.annotation.Qualifier;
4+
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
5+
import org.springframework.boot.context.properties.ConfigurationProperties;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.context.annotation.Primary;
9+
import org.springframework.jdbc.core.JdbcTemplate;
10+
import org.springframework.transaction.annotation.EnableTransactionManagement;
11+
12+
import javax.sql.DataSource;
13+
14+
/**
15+
* Created by zhuzhengping on 2017/3/8.
16+
*/
17+
@Configuration
18+
@EnableTransactionManagement
19+
public class DBBeanConfig_ManyDB {
20+
21+
@Bean(name = "oneDataSource")
22+
@Qualifier("oneDataSource")
23+
@Primary
24+
@ConfigurationProperties(prefix = "spring.datasource.one")
25+
public DataSource oneDataSource(){
26+
return DataSourceBuilder.create().build();
27+
}
28+
29+
@Bean(name = "twoDataSource")
30+
@Qualifier("twoDataSource")
31+
@ConfigurationProperties(prefix = "spring.datasource.two")
32+
public DataSource twoDataSource(){
33+
return DataSourceBuilder.create().build();
34+
}
35+
36+
@Bean(name = "oneJdbcTemplate")
37+
public JdbcTemplate oneJdbcTemplate(@Qualifier("oneDataSource") DataSource dataSource){
38+
return new JdbcTemplate(dataSource);
39+
}
40+
41+
@Bean(name = "twoJdbcTemplate")
42+
public JdbcTemplate twoJdbcTemplate(@Qualifier("twoDataSource") DataSource dataSource) {
43+
return new JdbcTemplate(dataSource);
44+
}
45+
}

src/main/resources/application.yml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,17 @@ logging:
1616
# type-aliases-package: com.compont
1717
spring:
1818
datasource:
19-
url: jdbc:mysql://localhost:3306/blog
20-
username: root
21-
password: 123
22-
driver-class-name: com.mysql.jdbc.Driver
19+
one:
20+
url: jdbc:mysql://localhost:3306/blog
21+
username: root
22+
password: 123
23+
driver-class-name: com.mysql.jdbc.Driver
24+
#数据库连接2
25+
two:
26+
url: jdbc:mysql://localhost:3306/dbgirl
27+
username: root
28+
password: 123
29+
driver-class-name: com.mysql.jdbc.Driver
2330
#hibernate映射配置
2431
jpa:
2532
properties:
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.example;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.jdbc.core.JdbcTemplate;
7+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
8+
9+
import javax.annotation.Resource;
10+
11+
/**
12+
* Created by zhuzhengping on 2017/3/8.
13+
*/
14+
@RunWith(SpringJUnit4ClassRunner.class)
15+
@SpringBootTest
16+
public class JdbcManyDBTest {
17+
18+
@Resource(name = "oneJdbcTemplate")
19+
protected JdbcTemplate jdbcTemplate1;
20+
21+
@Resource(name = "twoJdbcTemplate")
22+
protected JdbcTemplate jdbcTemplate2;
23+
24+
@Test
25+
public void test(){
26+
jdbcTemplate1.update("INSERT INTO tp_author(id,real_name,nick_name) VALUES (?,?,?)",3,"tt","tom");
27+
28+
jdbcTemplate1.update("INSERT INTO dbgirl.girl(id,age,cupsize) VALUES (?,?,?)",10,"88","tom");
29+
30+
}
31+
}

0 commit comments

Comments
 (0)