数据源管理(baomidou和druid)

在这里插入图片描述
                       星光下的赶路人star的个人主页

                      生命如此短暂,我们没有时间去争吵、道歉、伤心、仇恨、斤斤计较。我们只有时间去爱,一切稍纵即逝。

1、动态数据源切换

1.1 引入

在一个项目中,如果需要同时去查询不同的数据源,例如同时查询Mysql和Clickhouse或者同时查询一个数据源的两个库。
传统的实现方式十分的繁琐。这里使用baomidou框架进行动态数据源的切换。
这里以要查询mysql的两个库为例
在这里插入图片描述

1.2 添加依赖

baomidou必须结合SpringBoot一起使用。如果引入了druid连接池,需要注释掉,因为baomidou自带连接池。

 <dependency>
       <groupId>com.baomidou</groupId>      <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
        <version>2.5.8</version>
</dependenc

1.3 集成配置

在application.yml中删除之前MyBatis数据源的配置,增加以下配置:

spring:
  datasource:
    dynamic:
      primary: mybatis #设置默认的数据源或者数据源组,默认值即为master
      strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
      datasource:
        gmall:
          url: jdbc:mysql://hadoop104:3306/gmall?useSSL=false&useUnicode=true&characterEncoding=UTF-8
          username: root
          password: "000000"
          driver-class-name: com.mysql.cj.jdbc.Driver
        mybatis:
          url: jdbc:mysql://hadoop104:3306/Mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8
          username: root
          password: "000000"
          driver-class-name: com.mysql.cj.jdbc.Driver

1.4 使用

在Dao层的Mapper中使用@DS(数据源)来指定某个类的全部方法或某个方法使用某个指定的数据源。

@Mapper
//标准在类上,这个类的所有方法,都连接mybatis这个数据源
@DS("mybatis")
public interface EmployeeMapper
{
     //如果标准在方法上,这个方法去连指定的数据源
    // 局部优先
    @DS("gmall")
    List<BaseRegion> getRegions();

}

2、引入Druid连接池

如果不希望使用baomidou自带的连接池,我们可以更换连接池,例如更换为alibaba开源的Druid连接池。

2.1 pom添加依赖

<dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid-spring-boot-starter</artifactId>
      <version>1.2.15</version>
</dependency>

2.2 配置

spring:
  datasource:
    dynamic:
      primary: 221109 #设置默认的数据源或者数据源组,默认值即为221109
      strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
      datasource:
        221109:
          url: jdbc:mysql://hadoop102:3306/221109?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
          username: root
          password: "000000"
          driver-class-name: com.mysql.cj.jdbc.Driver
          druid:
            initial-size: 5
            max-active: 20
            max-wait: 60000
            min-idle: 5
            test-on-borrow: true
            test-on-return: false
            test-while-idle: true
        gmall:
          url: jdbc:mysql://hadoop102:3306/gmall_report?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
          username: root
          password: "000000"
          driver-class-name: com.mysql.cj.jdbc.Driver
          druid:
            initial-size: 5
            max-active: 20
            max-wait: 60000
            min-idle: 5
            test-on-borrow: true
            test-on-return: false
            test-while-idle: true
  autoconfigure:
    exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure

在这里插入图片描述
                      您的支持是我创作的无限动力

在这里插入图片描述
                      希望我能为您的未来尽绵薄之力

在这里插入图片描述
                      如有错误,谢谢指正;若有收获,谢谢赞美

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在 MybatisPlus 中,指定使用 Druid 数据源需要以下几个步骤: 1. 在 pom.xml 中添加 druid 和 mybatis-plus-boot-starter 依赖: ``` <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.22</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency> ``` 2. 在 application.yml 配置文件中添加 Druid 数据源的配置: ``` spring: datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8 username: root password: root driverClassName: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource filters: stat,wall maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false ``` 3. 在 MybatisPlus 的配置文件中指定使用 Druid 数据源: ``` @Configuration @MapperScan("com.example.mapper") public class MybatisPlusConfig { @Bean public DruidDataSource dataSource() { return new DruidDataSource(); } @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { return new MybatisPlusInterceptor(); } @Bean public MybatisSqlSessionFactoryBean sqlSessionFactory(DruidDataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); //其他配置 return sqlSessionFactoryBean; } @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } } ``` 在上述代码中,`dataSource()` 方法返回 Druid 数据源对象,`sqlSessionFactory()` 方法中将 Druid 数据源对象传入 MybatisSqlSessionFactoryBean 中,以便 MybatisPlus 使用该数据源

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星光下的赶路人star

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值