• 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

      成功!~~

  • 相关阅读:
    Oracle数据库的左连接和右连接(+)
    Web文件上传模块 Plupload
    增加反向链接的35个技巧
    google map api 与jquery结合使用(1)控件,监听器[转帖]
    教你在windows 7/xp 下安装使用mencoder
    Oracle 全文索引
    提高关键词排名的28个SEO技巧
    二叉树遍历及C语言实现
    小额担保业务管理系统详细设计介绍
    C#与数据结构二叉树的遍历
  • 原文地址:https://www.cnblogs.com/hmcjsc/p/15325448.html
Copyright © 2020-2023  润新知