apache / shardingsphere

Distributed SQL transaction & query engine for data sharding, scaling, encryption, and more - on any database.
Apache License 2.0
19.8k stars 6.7k forks source link

How to sharding table by a column which type is Datetime or use ShardingAlgorithm which define myself in mybatis #2958

Closed toby1024 closed 5 years ago

toby1024 commented 5 years ago

sharding-jdbc version: 4.0.0-RC2 springboot version: 2.1.6 mybatis version: 3.5.2 mybatis springstarter: 2.1.0 DB: mysql

I want sharding my table with a column which type is datetime, and I try code MyShardingAlgorithm implements HintShardingAlgorithm

private static final String ORDER_TABLE = "t-order";

  @Override
  public Collection<String> doSharding(Collection<String> collection, HintShardingValue<String> shardingValue) {

    List<String> list = Lists.newArrayList(shardingValue.getValues());
    List<String> actualTable = Lists.newArrayList();

    String json = list.get(0);
    IllegalResultQueryCondition condition = JSON.parseObject(json, IllegalResultQueryCondition.class);

    if (StringUtils.isEmpty(condition.getCreatedAt())) {

      for (int i = 1; i < 13; i++) {
        String tabIndex = i < 10 ? "0".concat(String.valueOf(i)) : String.valueOf(i);
        actualTable.add(ORDER_TABLE.concat(tabIndex));
      }
    } else {

      String tableSuffix = ShardingUtils.getDateIndex(condition.getCreatedAt());
      actualTable.add(ORDER_TABLE.concat(tableSuffix));
    }

    return actualTable;
  }

and config in application.yml


spring:
  application.name: illegal
  shardingsphere:
    datasource:
      ds0:
        driver-class-name: com.mysql.jdbc.Driver
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://${DB_URL}:3306/demo
        username:${DB_USER}
        password: ${DB_PASS}
      names: ds0
    sharding:
      binding-tables: t_order
      default-database-strategy:
        hint:
          algorithmClassName: com.demo.config.MyShardingAlgorithm
      tables:
        t_order:
          actual-data-nodes: ds0.t_order$->{1..9}, ds0.t_order$->{10..12}
          key-generator:
            column: id
            type: SNOWFLAKE
          table-strategy:
            hint:
              algorithmClassName: com.demo.config.MyShardingAlgorithm
    props:
      sql:
        show: true

but it looks like not worked. when I insert into table one recode , it will insert into all tables, also by query. please show me what I mistake. I find example with jdbc in github, but I don't know how to use in mybatis, has example show how to use by mybatis? thanks.

sunbufu commented 5 years ago

You must use HintManager If you wanna use HintShardingAlgorithm. Hint-example is an example for hint, maybe you should look at it first.

toby1024 commented 5 years ago

You must use HintManager If you wanna use HintShardingAlgorithm. Hint-example is an example for hint, maybe you should look at it first.

Yes,I was read the example, it's for jdbc. Now I want to use it in mybatis, and I don't konw how to mixin the HintManager into mybatis.

sunbufu commented 5 years ago

HintManager can work with myabtis too. You can call HintManager before call mybatis mapper like this.

// call HintManager's method.
HintManager.getInstance().addTableShardingValue("t_order", 1L);
// call maybtis method.
orderMapper.selectById(id);
toby1024 commented 5 years ago

HintManager can work with myabtis too. You can call HintManager before call mybatis mapper like this.

// call HintManager's method.
HintManager.getInstance().addTableShardingValue("t_order", 1L);
// call maybtis method.
orderMapper.selectById(id);

Thanks