springboot多数据源配置-使用druid配置多数据源

Service层代码

@DataSource(value = MultiDataSourceConst.SLAVE)
    public List<User> getUserList() {
        List<User> userList = userRepository.findAll();
        return userList;
    }

application.yml配置

server:
  port: 3389
  servlet:
    # 项目contextPath
    context-path: /
  tomcat:
    # tomcat的URI编码
    uri-encoding: UTF-8
    # tomcat最大线程数,默认为200
    max-threads: 800
    # Tomcat启动初始化的线程数,默认值25
    min-spare-threads: 30
    max-http-header-size: 8192

spring:
  # 模板引擎
  thymeleaf:
    mode: HTML
    encoding: utf-8
    # 禁用缓存
    cache: false
  # 资源信息
  messages:
    # 国际化资源文件路径
    basename: i18n/messages
  jackson:
    time-zone: GMT+8
    date-format: yyyy-MM-dd HH:mm:ss
  # 文件上传
  servlet:
     multipart:
       max-file-size:  30MB
       max-request-size:  30MB
  # 服务模块
  devtools:
    restart:
      # 热部署开关
      enabled: true

  http:
    encoding:
      force: true
      charset: utf-8
      enabled: true
  multi:
      datasource:
        master:
          url: jdbc:mysql://localhost:3306/courtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
          driver-class-name: com.mysql.jdbc.Driver
          username: root
          password: root
        slave:
          url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
          driver-class-name: com.mysql.jdbc.Driver
          username: root
          password: root

  jpa:
    database: MYSQL
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    show-sql: true
    hibernate:
      ddl-auto: update

pom.xml依赖

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

注:默认是master主数据库

工具类下载 https://download.csdn.net/download/samurai77/11868976

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值