Apache ShardingSphere 数据分片示例配置

连鸿
2023-12-01

pom.xml:

<dependencies>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.29</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.2.2</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.shardingsphere/shardingsphere-jdbc-core-spring-boot-starter -->
    <dependency>
      <groupId>org.apache.shardingsphere</groupId>
      <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
      <version>5.2.1</version>
    </dependency>

    <dependency>
      <groupId>org.yaml</groupId>
      <artifactId>snakeyaml</artifactId>
      <version>1.33</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>3.5.2</version>
    </dependency>

  </dependencies>

application.yml:

spring:
  application:
    name: aolifu-shardingsphere
  shardingsphere:
    datasource:
      names: db_0
      db_0:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://localhost:3306/java?useUnicode=true&characterEncoding=UTF-8
        username: root
        password: root

    mode:
      type: Standalone
      repository:
        type: JDBC
    rules:
      sharding:
        tables:
          user:
            actual-data-nodes: db_0.user_$->{0..2}
            table-strategy:
              standard:
                sharding-column: id
                sharding-algorithm-name: user_inline
        sharding-algorithms:
          user_inline:
            type: inline
            props:
              algorithm-expression: user_$->{id % 3}

    props:
      sql:
        show: true
      sqlCommentParseEnabled: true

  main:
    allow-bean-definition-overriding: true


server:
  port: 10002

mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml
  type-aliases-package: com.aolifu.shardingsphere.entity

logging:
  level:
    com:
      aolifu:
        mapper: info




 类似资料: