1. pom.xml配置

    1. <!--web-->
    2. <dependency>
    3. <groupId>org.springframework.boot</groupId>
    4. <artifactId>spring-boot-starter-web</artifactId>
    5. </dependency>
    6. <dependency>
    7. <groupId>org.springframework.boot</groupId>
    8. <artifactId>spring-boot-starter-actuator</artifactId>
    9. </dependency>
    10. <dependency>
    11. <groupId>org.springframework.boot</groupId>
    12. <artifactId>spring-boot-starter</artifactId>
    13. </dependency>
    14. <!--一般基础通用配置-->
    15. <dependency>
    16. <groupId>org.springframework.boot</groupId>
    17. <artifactId>spring-boot-devtools</artifactId>
    18. <scope>runtime</scope>
    19. <optional>true</optional>
    20. </dependency>
    21. <dependency>
    22. <groupId>org.projectlombok</groupId>
    23. <artifactId>lombok</artifactId>
    24. <optional>true</optional>
    25. </dependency>
    26. <dependency>
    27. <groupId>org.springframework.boot</groupId>
    28. <artifactId>spring-boot-starter-test</artifactId>
    29. <scope>test</scope>
    30. </dependency>
    31. <dependency>
    32. <groupId>org.mybatis.spring.boot</groupId>
    33. <artifactId>mybatis-spring-boot-starter</artifactId>
    34. <version>1.3.2</version>
    35. </dependency>
    36. <!-- Druid 数据连接池依赖 -->
    37. <dependency>
    38. <groupId>com.alibaba</groupId>
    39. <artifactId>druid</artifactId>
    40. <version>1.0.18</version>
    41. </dependency>
    42. <dependency>
    43. <groupId>mysql</groupId>
    44. <artifactId>mysql-connector-java</artifactId>
    45. <version>8.0.15</version>
    46. <scope>runtime</scope>
    47. </dependency>
  2. 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 #对应实体类路径

  1. 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()); }

  1. @Bean(name = "masterSqlSessionFactory")
  2. @Primary
  3. public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDatasSource") DataSource masterDataSource)
  4. throws Exception {
  5. final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
  6. sessionFactory.setDataSource(masterDataSource);
  7. sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
  8. .getResources(MasterDataSource.MAPPER_LOCATION));
  9. return sessionFactory.getObject();
  10. }

}

  1. 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()); }

  1. @Bean(name = "secondSqlSessionFactory")
  2. public SqlSessionFactory secondSqlSessionFactory(@Qualifier("secondDatasSource") DataSource secondDataSource)
  3. throws Exception {
  4. final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
  5. sessionFactory.setDataSource(secondDataSource);
  6. sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
  7. .getResources(SecondDataSource.MAPPER_LOCATION));
  8. return sessionFactory.getObject();
  9. }

}

  1. 5. 整体目录截图
  2. ![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)
  3. 6. 数据库表结构数据
  4. ![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)
  5. 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(); } } ```
  1. 发请求结果

image.png