Mybattis-Plus多数据源配置

该博客介绍了如何配置Spring Boot应用以实现动态数据源,并利用Druid进行数据源切换。内容包括YML配置,如设置主数据源、多个数据源的URL、用户名和密码等,以及Druid的连接池配置和监控过滤器。同时,展示了启动类排除DruidDataSourceAutoConfigure注解的使用,以及在Service层通过@DS注解选择数据源。
摘要由CSDN通过智能技术生成

一、YML配置

server:
  port: ${AA.server.port:8080}
spring:
  autoconfigure:
    exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
  application:
    name: dips
  datasource:
    dynamic:
    ## primary表示主数据源,不设置注解DS默认使用dips
      primary: dips
      strict: false
      datasource:
        dips:
          url: jdbc:oracle:thin:@${AA.oracle.datasource.ip}:${AA.oracle.datasource.port}:${AA.oracle.datasource.server}
          username: ${AA.oracle.datasource.username}
          password: ${AA.oracle.datasource.password}
          driver-class-name: oracle.jdbc.driver.OracleDriver
        mes:
          url: jdbc:oracle:thin:@${AA.oracle.mesdatasource.ip}:${AA.oracle.mesdatasource.port}/${AA.oracle.mesdatasource.server}
          username: ${AA.oracle.mesdatasource.username}
          password: ${AA.oracle.mesdatasource.password}
          driver-class-name: oracle.jdbc.driver.OracleDriver
      durid:
        initial-size: 10
        max-active: 500
        min-idle: 10
        max-wait: 60000
        #是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。
        pool-prepared-statements: true
        keep-alive: true
        max-pool-prepared-statement-per-connection-size: 100
        #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
        time-between-eviction-runs-millis: 60000
        #配置一个连接在池中最小生存的时间,单位是毫秒
        min-evictable-idle-time-millis: 300000
        #配置扩展插件:监控统计用的filter:stat  日志用的filter:log4j  防御sql注入的filter:wall
        filters: stat,wall
        #其它配置项
        validation-query: SELECT 1 FROM DUAL
        test-on-borrow: false
        test-on-return: false
        test-while-idle: true
        # WebStatFilter配置,说明请参考Druid Wiki,配置_配置WebStatFilter
        #是否启用StatFilter默认值true
        web-stat-filter.enabled: true
        web-stat-filter.url-pattern: /*
        web-stat-filter.exclusions: "*.js , *.gif ,*.jpg ,*.png ,*.css ,*.ico , /druid/*"
        web-stat-filter.session-stat-max-count: 1000
        web-stat-filter.profile-enable: true
        web-stat-filter.session-stat-enable: false
        # StatViewServlet配置
        #展示Druid的统计信息,StatViewServlet的用途包括:1.提供监控信息展示的html页面2.提供监控信息的JSON API
        #是否启用StatViewServlet默认值true
        stat-view-servlet.enabled: true
        stat-view-servlet.url-pattern: /druid/*
        stat-view-servlet.reset-enable: true
        stat-view-servlet.login-username: admin
        stat-view-servlet.login-password: 123456

二、启动类排除注解

@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)

三、pom文件引入

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

四、Service层使用注解@DS(“数据源名称”)

@DS(“mes”)可用在方法上,也用注解注解在一个类上。作用区域大小,则数据源区域大小不同。但设置为一个数据源后,里面包含的方法不能请求其他数据源的数据。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis-Plus支持多数据源配置,可以通过配置多个数据源来实现在不同的数据库中进行数据操作。具体配置步骤如下: 1. 在application.properties文件中配置多个数据源的连接信息,例如: ``` # 数据源1 spring.datasource.url=jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver # 数据源2 spring.datasource.test2.url=jdbc:mysql://localhost:3306/db2?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.test2.username=root spring.datasource.test2.password=root spring.datasource.test2.driver-class-name=com.mysql.jdbc.Driver ``` 2. 在Mybatis-Plus的配置文件中配置多个SqlSessionFactory,例如: ``` @Configuration public class MybatisPlusConfig { @Bean(name = "test1SqlSessionFactory") public SqlSessionFactory test1SqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/test1/*.xml")); return bean.getObject(); } @Bean(name = "test2SqlSessionFactory") public SqlSessionFactory test2SqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/test2/*.xml")); return bean.getObject(); } } ``` 3. 在Mybatis-Plus的配置文件中配置多个MapperScannerConfigurer,例如: ``` @Configuration public class MybatisPlusConfig { @Bean(name = "test1MapperScannerConfigurer") public MapperScannerConfigurer test1MapperScannerConfigurer() { MapperScannerConfigurer configurer = new MapperScannerConfigurer(); configurer.setBasePackage("com.example.mapper.test1"); configurer.setSqlSessionFactoryBeanName("test1SqlSessionFactory"); return configurer; } @Bean(name = "test2MapperScannerConfigurer") public MapperScannerConfigurer test2MapperScannerConfigurer() { MapperScannerConfigurer configurer = new MapperScannerConfigurer(); configurer.setBasePackage("com.example.mapper.test2"); configurer.setSqlSessionFactoryBeanName("test2SqlSessionFactory"); return configurer; } } ``` 4. 在Mapper接口中使用@Mapper注解指定对应的SqlSessionFactory,例如: ``` @Mapper public interface Test1Mapper { // ... } @Mapper public interface Test2Mapper { // ... } ``` 这样就可以在不同的Mapper接口中使用不同的数据源进行数据操作了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值