springBoot 2.x整合Mybatis-Plus 3.x配置多数据源

一、引入POM文件

 
        
            com.baomidou
            mybatis-plus-boot-starter
            3.1.2
        

        
            mysql
            mysql-connector-java
        

        
            com.oracle
            ojdbc6
            11.2.0.3
        
 
        
            com.baomidou
            dynamic-datasource-spring-boot-starter
            2.5.4
        

        
            com.alibaba
            druid-spring-boot-starter
            1.1.20
        

二、yml配置文件

##日志记录
logging:
  config: classpath:log/logback-spring.xml
##mybatisPlus配置
mybatis-plus:
  mapper-locations: classpath:mapper/*.xml
  global-config:
    db-config:
      id-type: input
##动态数据源配置
spring:
  datasource:
    dynamic:
      primary: master
      datasource:
        master:
          username: *****
          password: *****
          driver-class-name: oracle.jdbc.driver.OracleDriver
          url: jdbc:oracle:thin:@127.0.0.1:1521:orcl
        slaver1:
          username: *****
          password: *****
          driver-class-name: oracle.jdbc.driver.OracleDriver
          url: jdbc:oracle:thin:@127.0.0.1:1521:orcl
        slaver2:
          username: *****
          password: *****
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://localhost:3306/exam?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
    type: com.alibaba.druid.pool.DruidDataSource

三、动态数据源排除datasource影响,启动类增加注解

@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
public class DymicApplication {

    public static void main(String[] args) {
        SpringApplication.run(DymicApplication.class, args);
    }
}

四、使用@DS切换数据源,可配置Mapper层或者Service层

//切换主库
@Mapper
@DS("master")
public interface IedgeInfoMapper extends BaseMapper {
}
//切换从库
@Mapper
@DS("slaver2")
public interface IProducteMapper extends BaseMapper {
}

四、数据源为oracle时候Mybatis-Plus相关配置

  • yml文件配置(使用sequence主键)
mybatis-plus:
  global-config:
    db-config:
      id-type: input
  • mybatis-plus 3.x 推荐bean注入方式配置oracleKeyGenerator
@Configuration
public class MybatisPlusConfig {

    /**
     * 分页查询拦截器(mybatis-Plus通用配置,与数据源无关)
     *
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    /**
     * Sequence主键自增
     *
     * @return 返回oracle自增类
     * @author localhost
     * @date 2019/1/2
     */
    @Bean
    public OracleKeyGenerator oracleKeyGenerator() {
        return new OracleKeyGenerator();

    }
}
  • 相关实体类配置
@TableName("表名")
@KeySequence(value = "序列名", clazz = Long.class)
public class EdgeRoad {
    @TableId(value = "id", type = IdType.INPUT)
    private Long id;
    private String edgeCode;
   ...
}

五、数据源为mysql时Mybatis-Plus相关配置

  • 实体类配置即可
@TableName("表名")
public class Product  {
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;
    ...
}

六、Mysql为最新版本时候,需要注意的一些配置

  • driver-class-name
driver-class-name: com.mysql.cj.jdbc.Driver
  • 配置时区
url: jdbc:mysql://localhost:3306/exam?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8

七、完整示例代码

Github传送门

八、Mybatis-plus推荐阅读

官方文档

你可能感兴趣的:(springBoot 2.x整合Mybatis-Plus 3.x配置多数据源)