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

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

电商项目part02 电商后台多数据源_第1张图片

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

方法1:jdk自带的dynamicdatasource

方法2:Mybatis 方式

方法3:dynamicdatasource框架

	
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.2.3version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-aopartifactId>
        dependency>

        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>dynamic-datasource-spring-boot-starterartifactId>
            <version>3.5.0version>
        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);
    }

你可能感兴趣的:(电商实战,java)