电商项目part02 电商后台多数据源

电商后台项目需要访问的数据源

在这里插入图片描述

多数据源方法(读写分离)

方法1:jdk自带的dynamicdatasource

方法2:Mybatis 方式

方法3:dynamicdatasource框架

	<!--Druid连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>3.5.0</version>
        </dependency>
spring:
  datasource:
    dynamic:
      #设置默认的数据源或者数据源组,默认值即为master
      primary: master
      #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
      strict: false
      datasource:
        master:
          url: jdbc:mysql://127.0.0.1:3306/datasource1?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&useSSL=false
          username: root
          password: 123456
          initial-size: 1
          min-idle: 1
          max-active: 20
          test-on-borrow: true
          driver-class-name: com.mysql.cj.jdbc.Driver
        slave_1:
          url: jdbc:mysql://127.0.0.1:3306/datasource2?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&useSSL=false
          username: root
          password: root
          initial-size: 1
          min-idle: 1
          max-active: 20
          test-on-borrow: true
          driver-class-name: com.mysql.cj.jdbc.Driver
	@GetMapping(value = "select")
    @DS("slave_1")
    public List<Friend> select(){
    
    
        return friendService.list();
    }


    @GetMapping(value = "insert")
    @DS("master")
    public void in(){
    
    
        Friend friend = new Friend();
        friend.setName("loulan");
        friendService.save(friend);
    }

猜你喜欢

转载自blog.csdn.net/Forbidden_City/article/details/132331462