Skip to content

Commit be53367

Browse files
committed
多数据源
1 parent 643a0c9 commit be53367

File tree

9 files changed

+258
-5
lines changed

9 files changed

+258
-5
lines changed

multipleSource/pom.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>xiaomo</artifactId>
7+
<groupId>info.xiaomo</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>multipleSource</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>info.xiaomo</groupId>
17+
<artifactId>core</artifactId>
18+
<version>1.0.0-SNAPSHOT</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-jdbc</artifactId>
23+
</dependency>
24+
</dependencies>
25+
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-maven-plugin</artifactId>
31+
</plugin>
32+
</plugins>
33+
</build>
34+
35+
</project>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package info.xiaomo.multipleSource;
2+
3+
import org.springframework.beans.factory.annotation.Qualifier;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
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.Primary;
10+
import org.springframework.jdbc.core.JdbcTemplate;
11+
12+
import javax.sql.DataSource;
13+
14+
/**
15+
* 把今天最好的表现当作明天最新的起点..~
16+
* いま 最高の表現 として 明日最新の始発..~
17+
* Today the best performance as tomorrow newest starter!
18+
* Created by IntelliJ IDEA.
19+
*
20+
* @author: xiaomo
21+
* @github: https://github.com/qq83387856
22+
* @email: hupengbest@163.com
23+
* @QQ_NO: 83387856
24+
* @Date: 2016/11/16 10:34
25+
* @Description: 多数据源(在配置文件中自定义字段,在这里取出并创建不同的数据源)
26+
* @Copyright(©) 2015 by xiaomo.
27+
**/
28+
29+
@SpringBootApplication
30+
public class MultipleSourceMain {
31+
32+
public static void main(String[] args) {
33+
SpringApplication.run(MultipleSourceMain.class, args);
34+
}
35+
36+
37+
/**
38+
* 第一个数据源
39+
* @return 数据源实例
40+
*/
41+
@Bean(name = "primaryDataSource")
42+
@Qualifier("primaryDataSource")
43+
@ConfigurationProperties(prefix = "spring.datasource.primary")
44+
public DataSource primaryDataSource() {
45+
return DataSourceBuilder.create().build();
46+
}
47+
48+
/**
49+
* 第二个数据源
50+
* @return 数据源实例
51+
*/
52+
@Bean(name = "secondaryDataSource")
53+
@Qualifier("secondaryDataSource")
54+
@Primary
55+
@ConfigurationProperties(prefix = "spring.datasource.secondary")
56+
public DataSource secondaryDataSource() {
57+
return DataSourceBuilder.create().build();
58+
}
59+
60+
/**
61+
* 第一个JDBC模板
62+
* @param dataSource dataSource
63+
* @return JDBC模板
64+
*/
65+
@Bean(name = "primaryJdbcTemplate")
66+
public JdbcTemplate primaryJdbcTemplate(
67+
@Qualifier("primaryDataSource") DataSource dataSource) {
68+
return new JdbcTemplate(dataSource);
69+
}
70+
71+
/**
72+
* 第二个JDBC模板
73+
* @param dataSource dataSource
74+
* @return JDBC模板
75+
*/
76+
@Bean(name = "secondaryJdbcTemplate")
77+
public JdbcTemplate secondaryJdbcTemplate(
78+
@Qualifier("secondaryDataSource") DataSource dataSource) {
79+
return new JdbcTemplate(dataSource);
80+
}
81+
82+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package info.xiaomo.multipleSource.controller;
2+
3+
import info.xiaomo.core.controller.Result;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.beans.factory.annotation.Qualifier;
6+
import org.springframework.jdbc.core.JdbcTemplate;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
/**
11+
* 把今天最好的表现当作明天最新的起点..~
12+
* いま 最高の表現 として 明日最新の始発..~
13+
* Today the best performance as tomorrow newest starter!
14+
* Created by IntelliJ IDEA.
15+
*
16+
* @author: xiaomo
17+
* @github: https://github.com/qq83387856
18+
* @email: hupengbest@163.com
19+
* @QQ_NO: 83387856
20+
* @Date: 2016/11/16 10:45
21+
* @Description: 用户实体类
22+
* @Copyright(©) 2015 by xiaomo.
23+
**/
24+
25+
@RestController
26+
public class MultipleSourceController {
27+
private final JdbcTemplate jdbcTemplate1;
28+
29+
private final JdbcTemplate jdbcTemplate2;
30+
31+
@Autowired
32+
public MultipleSourceController(@Qualifier("primaryJdbcTemplate") JdbcTemplate jdbcTemplate1, @Qualifier("secondaryJdbcTemplate") JdbcTemplate jdbcTemplate2) {
33+
this.jdbcTemplate1 = jdbcTemplate1;
34+
this.jdbcTemplate2 = jdbcTemplate2;
35+
this.jdbcTemplate1.update("DELETE FROM user ");
36+
this.jdbcTemplate2.update("DELETE FROM user ");
37+
}
38+
39+
40+
@RequestMapping("/")
41+
public Result index() {
42+
// 往第一个数据源中插入两条数据
43+
jdbcTemplate1.update("insert into user(name,age) values(?, ?)", "xiaomo", 20);
44+
jdbcTemplate2.update("insert into user(name,age) values(?, ?)", "xiaoming", 30);
45+
46+
int count1 = jdbcTemplate1.queryForObject("select count(1) from user", Integer.class);
47+
int count2 = jdbcTemplate2.queryForObject("select count(1) from user", Integer.class);
48+
return new Result(new Object[]{count1, count2});
49+
}
50+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package info.xiaomo.multipleSource.domain;
2+
3+
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
import lombok.ToString;
7+
8+
@Data
9+
@ToString(callSuper = false)
10+
@NoArgsConstructor
11+
public class User {
12+
13+
private Long id;
14+
15+
private String name;
16+
17+
private Integer age;
18+
19+
public User(Long id, String name, Integer age) {
20+
this.id = id;
21+
this.name = name;
22+
this.age = age;
23+
}
24+
25+
public User(String name, Integer age) {
26+
this.name = name;
27+
this.age = age;
28+
}
29+
30+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
logging.config=classpath:config/logback-dev.xml
2+
server.port=8080
3+
server.session.timeout=1800
4+
server.max-http-header-size=20971520
5+
6+
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
7+
spring.jackson.time-zone=GMT+8
8+
9+
#database1
10+
spring.datasource.primary.url=jdbc:mysql://115.29.137.34:3306/test1
11+
spring.datasource.primary.username=xiaomo
12+
spring.datasource.primary.password=123456
13+
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
14+
15+
#database2
16+
spring.datasource.secondary.url=jdbc:mysql://115.29.137.34:3306/test2
17+
spring.datasource.secondary.username=xiaomo
18+
spring.datasource.secondary.password=123456
19+
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
20+
21+
22+
#other
23+
banner.location=config/banner-girl.txt
24+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.::::.
2+
.::::::::.
3+
:::::::::::
4+
..:::::::::::'
5+
'::::::::::::'
6+
.::::::::::
7+
'::::::::::::::..
8+
..::::::::::::.
9+
``::::::::::::::::
10+
::::``:::::::::' .:::.
11+
::::' ':::::' .::::::::.
12+
.::::' :::: .:::::::'::::.
13+
.:::' ::::: .:::::::::' ':::::.
14+
.::' :::::.:::::::::' ':::::.
15+
.::' ::::::::::::::' ``::::.
16+
...::: ::::::::::::' ``::.
17+
```` ':. ':::::::::' ::::..
18+
'.:::::' ':'````..
19+
:: Spring Boot :: (v1.4.1.RELEASE)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<configuration scan="true">
4+
5+
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
6+
<encoder charset="UTF-8">
7+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level - %msg%n</pattern>
8+
</encoder>
9+
</appender>
10+
11+
<root level="INFO">
12+
<appender-ref ref="stdout"/>
13+
</root>
14+
15+
<logger name="info.xiaomo" level="DEBUG"/>
16+
17+
</configuration>

mybatis/src/main/java/info/xiaomo/mybatis/domain/User.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package info.xiaomo.mybatis.domain;
22

33

4-
import lombok.AllArgsConstructor;
54
import lombok.Data;
65
import lombok.NoArgsConstructor;
76
import lombok.ToString;
87

98
@Data
109
@ToString(callSuper = false)
11-
@AllArgsConstructor
1210
@NoArgsConstructor
1311
public class User {
1412

@@ -18,9 +16,6 @@ public class User {
1816

1917
private Integer age;
2018

21-
public User() {
22-
}
23-
2419
public User(Long id, String name, Integer age) {
2520
this.id = id;
2621
this.name = name;

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<module>mybatis</module>
3131
<module>freemarker</module>
3232
<module>velocity</module>
33+
<module>multipleSource</module>
3334
</modules>
3435
<packaging>pom</packaging>
3536

0 commit comments

Comments
 (0)