shardingjdbc分库分表测试

发布时间:2020-05-19 11:24:21 作者:Mos 阅读量:63767

springboot + mybatis-plus + mysql + sharding-jdbc

sharding中文官方文档

测试代码-码云仓库

测试代码-github仓库

部分配置:

pom.xml


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.0.M3</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.shardingjdbc</groupId>
  <artifactId>shardingjdbc</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>shardingjdbc</name>
  <description>shardingjdbc project for Spring Boot</description>

  <properties>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-undertow</artifactId>
    </dependency>



    <!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus -->
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus</artifactId>
      <version>2.1.9</version>
    </dependency>
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatisplus-spring-boot-starter</artifactId>
      <version>1.0.5</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.47</version>
    </dependency>

     <!--https://mvnrepository.com/artifact/org.apache.shardingsphere/sharding-jdbc-->
    <dependency>
      <groupId>io.shardingsphere</groupId>
      <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
      <version>3.0.0.M4</version>
    </dependency>


  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>
application.yml



server:
  # 端口
  port: 1818
  undertow:
    # 编码
    uri-encoding: UTF-8
spring:
  main:
    allow-bean-definition-overriding: true
  resources:
    # 静态资源文件夹
    static-locations: classpath:/
  # 序列化
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
    serialization:
      #忽略无法转换的对象
      fail_on_empty_beans: false
    deserialization:
      #允许对象忽略json中不存在的属性
      fail_on_unknown_properties: false
    #设置空如何序列化
    defaultPropertyInclusion: ALWAYS
    parser:
      #允许出现特殊字符和转义符
      allow_unquoted_control_chars: true
      #允许出现单引号
      allow_single_quotes: true

# 分库
sharding:
  jdbc:
    datasource:
      names: shard1,shard2
      shard1:
        type: org.apache.commons.dbcp.BasicDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/shard1
        username: root
        password: root
      shard2:
        type: org.apache.commons.dbcp.BasicDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/shard2
        username: root
        password: root
    config:
      sharding:
        # 默认数据源(未设置分表策略的表将默认用此数据源)
        default-datasource-name: shard1
        # 默认分库策略
        default-database-strategy:
          standard:
            sharding-column: id
            precise-algorithm-class-name: com.shardingjdbc.config.sharding.DataBasePreciseRule
        # 分表策略
        tables:
          # 分库分表
          table_one:
            actual-data-nodes: shard$->{1..2}.table_one_$->{1..2}
            table-strategy:
              standard:
                sharding-column: id
                precise-algorithm-class-name: com.shardingjdbc.config.sharding.DataTablePreciseRule


# mybatis-plus 的分页插件
pagehelper:
  helper-dialect: mysql
  params: count=countSql
  reasonable: false
  support-methods-arguments: true

# mybatis-plus
mybatis-plus:
  mapper-locations: /mapper/**/**.xml
  typeAliasesPackage: com.shardingjdbc.domain
  global-config:
    id-type: 3
    field-strategy: 2
    db-column-underline: true
    refresh-mapper: true
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: false
    call-setters-on-nulls: true



其他代码详见git仓库:

测试代码-码云仓库

测试代码-github仓库


支付宝打赏 微信打赏
©2021 i847.cn
部分内容转自网络,如有损害您的权益,致邮联系:jiang2008wen#126.com,一经证实,立即删除!     我要留言
备案号:蜀ICP备18020563号-1