pom.xml配置
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--一般基础通用配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- Druid 数据连接池依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
<scope>runtime</scope>
</dependency>
application.yml配置 ``` spring: datasource: test: # master 数据源配置(test库) url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8 username: root password: root driverClassName: com.mysql.cj.jdbc.Driver testB: # second 数据源配置(testb库) url: jdbc:mysql://127.0.0.1:3306/testb?serverTimezone=GMT%2B8 username: root password: root driverClassName: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/master/*.xml
type-aliases-package: web.entity #对应实体类路径
3. config下面配置主数据源类MasterDataSource.java
package web.config;
import com.alibaba.druid.pool.DruidDataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
- @author : dafeng.guo
@date : 10:18 2020/12/17 **/ @Configuration @MapperScan(basePackages = “web.dao.test”, sqlSessionFactoryRef = “masterSqlSessionFactory”) public class MasterDataSource {
/**
指定mapper和dao位置进行数据隔离 / private static final String MAPPER_LOCATION=”classpath:mapper/master/.xml”;
@Value(“${spring.datasource.test.url}”) private String url; @Value(“${spring.datasource.test.username}”) private String username; @Value(“${spring.datasource.test.password}”) private String password; @Value(“${spring.datasource.test.driverClassName}”) private String driverClassName;
@Bean(name = “masterDatasSource”) @Primary public DataSource masterDataSource(){ DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setUrl(url); druidDataSource.setUsername(username); druidDataSource.setPassword(password); druidDataSource.setDriverClassName(driverClassName); return druidDataSource; }
@Bean(name = “masterDataSourceTransaction”) @Primary public DataSourceTransactionManager masterDataSourceTransactionManager(){ return new DataSourceTransactionManager(masterDataSource()); }
@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDatasSource") DataSource masterDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(MasterDataSource.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
4. 第二数据源和主数据源类似
package web.config;
import com.alibaba.druid.pool.DruidDataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
- @author : dafeng.guo
@date : 10:18 2020/12/17 **/ @Configuration @MapperScan(basePackages = “web.dao.testB”, sqlSessionFactoryRef = “secondSqlSessionFactory”) public class SecondDataSource {
/**
指定mapper和dao位置进行数据隔离 / private static final String PACKAGE=”web.dao.testB”; private static final String MAPPER_LOCATION=”classpath:mapper/second/.xml”;
@Value(“${spring.datasource.testB.url}”) private String url; @Value(“${spring.datasource.testB.username}”) private String username; @Value(“${spring.datasource.testB.password}”) private String password; @Value(“${spring.datasource.testB.driverClassName}”) private String driverClassName;
@Bean(name = “secondDatasSource”) public DataSource secondDataSource(){ DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setUrl(url); druidDataSource.setUsername(username); druidDataSource.setPassword(password); druidDataSource.setDriverClassName(driverClassName); return druidDataSource; }
@Bean(name = “secondDataSourceTransaction”) public DataSourceTransactionManager secondDataSourceTransactionManager(){ return new DataSourceTransactionManager(secondDataSource()); }
@Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory secondSqlSessionFactory(@Qualifier("secondDatasSource") DataSource secondDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(secondDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(SecondDataSource.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
5. 整体目录截图
![image.png](https://cdn.nlark.com/yuque/0/2021/png/2774495/1610616654045-296fed2c-edb6-49fa-8897-e0db9eace37d.png#crop=0&crop=0&crop=1&crop=1&height=416&id=VdqdB&margin=%5Bobject%20Object%5D&name=image.png&originHeight=831&originWidth=1915&originalType=binary&ratio=1&rotation=0&showTitle=false&size=188362&status=done&style=none&title=&width=957.5)
6. 数据库表结构数据
![image.png](https://cdn.nlark.com/yuque/0/2021/png/2774495/1610616719836-e4476df0-1719-457f-bf02-2c641a62298f.png#crop=0&crop=0&crop=1&crop=1&height=323&id=sp69L&margin=%5Bobject%20Object%5D&name=image.png&originHeight=646&originWidth=1590&originalType=binary&ratio=1&rotation=0&showTitle=false&size=137182&status=done&style=none&title=&width=795)
7. 测试控制层代码
package web.controller;
import web.entity.Student; import lombok.extern.slf4j.Slf4j; import web.entity.Teacher; import web.service.StudentService; import org.springframework.web.bind.annotation.*; import web.service.TeacherService;
import javax.annotation.Resource; import java.io.IOException;
/**
- (Student)表控制层 *
- @author dafeng.guo
@since 2020-12-09 14:56:22 / @RestController @Slf4j public class StudentController { /*
服务对象 */ @Resource private StudentService studentService; @Resource private TeacherService teacherService;
/**
- 通过主键查询单条数据 *
- @return 单条数据 */ @GetMapping(“/selectOne”) public String selectOne(Integer id) throws IOException { Student student = studentService.queryById(id); Teacher teacher = teacherService.queryById(id); StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(student.toString()).append(teacher.toString()); return stringBuffer.toString(); } } ```
- 发请求结果