Skip to content

Commit b584926

Browse files
committed
springboot-data-mybatis-multiple-datasource
1 parent 0d92fb9 commit b584926

25 files changed

+1098
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.4.1</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.wdbyte</groupId>
12+
<artifactId>springboot-data-mybatis-multiple-datasource</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>demo</name>
15+
<description>Demo project for Spring Boot</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.mybatis.spring.boot</groupId>
28+
<artifactId>mybatis-spring-boot-starter</artifactId>
29+
<version>2.1.4</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>mysql</groupId>
34+
<artifactId>mysql-connector-java</artifactId>
35+
<scope>runtime</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.projectlombok</groupId>
39+
<artifactId>lombok</artifactId>
40+
<optional>true</optional>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-test</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
</dependencies>
48+
49+
<build>
50+
<plugins>
51+
<plugin>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-maven-plugin</artifactId>
54+
<configuration>
55+
<excludes>
56+
<exclude>
57+
<groupId>org.projectlombok</groupId>
58+
<artifactId>lombok</artifactId>
59+
</exclude>
60+
</excludes>
61+
</configuration>
62+
</plugin>
63+
</plugins>
64+
</build>
65+
66+
</project>
6 KB
Binary file not shown.
Binary file not shown.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.wdbyte;
2+
3+
import org.mybatis.spring.annotation.MapperScan;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
/**
8+
* <p>
9+
* Spring Boot 启动类
10+
* </p>
11+
*/
12+
@SpringBootApplication
13+
public class BootApplication {
14+
15+
public static void main(String[] args) {
16+
SpringApplication.run(BootApplication.class, args);
17+
}
18+
19+
}
20+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.wdbyte.config;
2+
3+
import javax.sql.DataSource;
4+
5+
import org.apache.ibatis.session.SqlSessionFactory;
6+
import org.mybatis.spring.SqlSessionFactoryBean;
7+
import org.mybatis.spring.SqlSessionTemplate;
8+
import org.mybatis.spring.annotation.MapperScan;
9+
import org.springframework.beans.factory.annotation.Qualifier;
10+
import org.springframework.boot.context.properties.ConfigurationProperties;
11+
import org.springframework.boot.jdbc.DataSourceBuilder;
12+
import org.springframework.context.annotation.Bean;
13+
import org.springframework.context.annotation.Configuration;
14+
import org.springframework.context.annotation.Primary;
15+
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
16+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
17+
18+
/**
19+
* 主数据源配置
20+
*
21+
* @author niujinpeng
22+
* @website: https://www.wdbyte.com
23+
* @date 2020/12/19
24+
*/
25+
@Configuration
26+
@MapperScan(basePackages = {"com.wdbyte.mapper.primary"}, sqlSessionFactoryRef = "sqlSessionFactory")
27+
public class PrimaryDataSourceConfig {
28+
29+
@Bean(name = "dataSource")
30+
@ConfigurationProperties(prefix = "spring.datasource.primary")
31+
@Primary
32+
public DataSource dataSource() {
33+
return DataSourceBuilder.create().build();
34+
}
35+
36+
@Bean(name = "sqlSessionFactory")
37+
@Primary
38+
public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
39+
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
40+
bean.setDataSource(dataSource);
41+
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
42+
return bean.getObject();
43+
}
44+
45+
@Bean(name = "transactionManager")
46+
@Primary
47+
public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) {
48+
return new DataSourceTransactionManager(dataSource);
49+
}
50+
51+
@Bean(name = "sqlSessionTemplate")
52+
@Primary
53+
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
54+
return new SqlSessionTemplate(sqlSessionFactory);
55+
}
56+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.wdbyte.config;
2+
3+
import javax.sql.DataSource;
4+
5+
import org.apache.ibatis.session.SqlSessionFactory;
6+
import org.mybatis.spring.SqlSessionFactoryBean;
7+
import org.mybatis.spring.SqlSessionTemplate;
8+
import org.mybatis.spring.annotation.MapperScan;
9+
import org.springframework.beans.factory.annotation.Qualifier;
10+
import org.springframework.boot.context.properties.ConfigurationProperties;
11+
import org.springframework.boot.jdbc.DataSourceBuilder;
12+
import org.springframework.context.annotation.Bean;
13+
import org.springframework.context.annotation.ComponentScan;
14+
import org.springframework.context.annotation.Configuration;
15+
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
16+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
17+
18+
/**
19+
* 第二个数据源配置
20+
*
21+
* @author niujinpeng
22+
* @website: https://www.wdbyte.com
23+
* @date 2020/12/19
24+
*/
25+
@Configuration
26+
@MapperScan(basePackages = {"com.wdbyte.mapper.datasource2"}, sqlSessionFactoryRef = "sqlSessionFactory2")
27+
public class SecondDataSourceConfig {
28+
29+
@Bean(name = "dataSource2")
30+
@ConfigurationProperties(prefix = "spring.datasource.datasource2")
31+
public DataSource dataSource() {
32+
return DataSourceBuilder.create().build();
33+
}
34+
35+
@Bean(name = "sqlSessionFactory2")
36+
public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource2") DataSource dataSource) throws Exception {
37+
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
38+
bean.setDataSource(dataSource);
39+
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
40+
return bean.getObject();
41+
}
42+
43+
@Bean(name = "transactionManager2")
44+
public DataSourceTransactionManager transactionManager(@Qualifier("dataSource2") DataSource dataSource) {
45+
return new DataSourceTransactionManager(dataSource);
46+
}
47+
48+
@Bean(name = "sqlSessionTemplate2")
49+
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory) {
50+
return new SqlSessionTemplate(sqlSessionFactory);
51+
}
52+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.wdbyte.controller;
2+
3+
import java.util.List;
4+
5+
import com.wdbyte.domain.Book;
6+
import com.wdbyte.service.BookService;
7+
import com.wdbyte.domain.Response;
8+
import com.wdbyte.utils.ResponseUtill;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
/**
14+
* <p>
15+
*
16+
* @Author niujinpeng
17+
* @Date 2019/1/21 14:16
18+
*/
19+
@RestController
20+
public class BookController {
21+
22+
@Autowired
23+
private BookService bookService;
24+
25+
@GetMapping(value = "/books")
26+
public Response selectAll() throws Exception {
27+
List<Book> books = bookService.selectAll();
28+
return ResponseUtill.success(books);
29+
}
30+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.wdbyte.controller;
2+
3+
import com.wdbyte.domain.Response;
4+
import com.wdbyte.utils.ResponseUtill;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.ResponseBody;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
/**
12+
* <p>
13+
* 测试控制器
14+
*
15+
* @Author niujinpeng
16+
* @Date 2018/12/4 14:41
17+
*/
18+
@Slf4j
19+
@RestController
20+
public class HelloController {
21+
22+
@Value("${bootapp.description}")
23+
private String description;
24+
25+
@RequestMapping("/")
26+
public String index() {
27+
log.info(description);
28+
return "Greetings from Spring Boot!";
29+
}
30+
31+
@RequestMapping(value = "/er")
32+
@ResponseBody
33+
public Response error(Integer num) throws Exception {
34+
num = num / num;
35+
return ResponseUtill.success(num);
36+
}
37+
38+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.wdbyte.controller;
2+
3+
import java.util.List;
4+
5+
import com.wdbyte.domain.Response;
6+
import com.wdbyte.domain.User;
7+
import com.wdbyte.service.UserService;
8+
import com.wdbyte.utils.ResponseUtill;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.ResponseBody;
12+
import org.springframework.web.bind.annotation.RestController;
13+
14+
/**
15+
* <p>
16+
*
17+
* @Author niujinpeng
18+
* @Date 2018/12/19 17:17
19+
*/
20+
@RestController
21+
public class UserController {
22+
23+
@Autowired
24+
private UserService userService;
25+
26+
@ResponseBody
27+
@GetMapping(value = "/users")
28+
public Response selectAll() {
29+
List<User> userList = userService.selectAll();
30+
return ResponseUtill.success(userList);
31+
}
32+
}

0 commit comments

Comments
 (0)