E-commerce project part02 E-commerce backend multiple data sources

Data sources that e-commerce backend projects need to access

Insert image description here

Multiple data source method (read-write separation)

Method 1: jdk’s own dynamic datasource

Method 2: Mybatis method

Method 3: dynamicdatasource framework

	<!--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);
    }

Guess you like

Origin blog.csdn.net/Forbidden_City/article/details/132331462