sharding+mybatisplus单库分表部署

sharding+mybatisplus单库分表部署

sharding和Mybatisplus对版本兼容问题非常敏感,以下版本是在冲浪中得知,测试无错,若还有报错,请降低springboot版本,本次springboot测试为 2.4.3版本

  1. pom.xml

    <!-- 这个druid连接池非常重要,自己测试时候 ,这是必须品 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.21</version>
    </dependency>
    <!-- springboot sharding start -->
    <dependency>
        <groupId>org.apache.shardingsphere</groupId>
        <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
        <version>4.0.0-RC2</version>
    </dependency>
    <!-- for spring namespace -->
    <dependency>
        <groupId>org.apache.shardingsphere</groupId>
        <artifactId>sharding-jdbc-spring-namespace</artifactId>
        <version>4.0.0-RC2</version>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.3.1.tmp</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    
  2. application.yml

    spring:
      shardingsphere:
        props:
          sql.show: true
        datasource:
          #指定库
          names: jsctest
          #设置数据库信息
          jsctest:
            type: com.alibaba.druid.pool.DruidDataSource
            url: jdbc:mysql://localhost:3306/jsctest?useJDBCCompliantTimezoneShift=true&serverTimezone=UTC&useUncode=true&characterEncoding=UTF-8
            driver-class-name: com.mysql.cj.jdbc.Driver
            username: root
            password: 123
        sharding:
        #表
          tables:
          #虚拟表名test
            test:
            #真实表名逻辑,此处为test0,test1,test2
              actual-data-nodes: jsctest.test$->{0..2}
              #指定分表策略
              table-strategy:
                inline:
                #指定策略字段
                  sharding-column: id
                  #test+字段Id%3是真实表名,假如字段Id为2,则插入test1表
                  algorithm-expression: test$->{id % 3}
    
    #指定mapper,pojo包
    mybatis-plus:
      mapper-locations: classpath:mapper/*.xml
      type-aliases-package: com.example.test.test.Pojo
    
  3. Pojo实体类

    /**
     * @Author: 王居三木超
     * @Description: TODO
     * @DateTime: 2021/9/23 17:02
     **/
    @Data
    @TableName("test")//虚拟表名
    public class testPojo implements Serializable {
        //雪花Id
        @TableId(type = IdType.ASSIGN_ID)
        @JsonFormat(shape = JsonFormat.Shape.STRING)
        private Long id;
        private String name;
    }
    
  4. Dao

    /**
     * @Author: 王居三木超
     * @Description: TODO
     * @DateTime: 2021/9/23 17:01
     **/
    @Mapper
    @Repository
    public interface test extends BaseMapper<testPojo> {
    }
    
  5. Test测试类

    @SpringBootTest
    class TestApplicationTests {
    
        @Autowired
        test test;
    
        @Test
        void contextLoads() {
            for (int i = 0; i < 100; i++) {
                testPojo testPojo = new testPojo();
                testPojo.setName(i + "");
                test.insert(testPojo);
            }
        }
    }
    
  6. 表test0

    image-20210923181013876

    表test1

    表test2

    image-20210923181027662

    成功!~~

posted @ 2021-09-23 18:26  王居三木  阅读(365)  评论(0编辑  收藏  举报