You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2019/08/30 10:10:05 UTC

[GitHub] [incubator-shardingsphere-example] toby1024 opened a new issue #170: How to sharding table by a column which type is Datetime or use ShardingAlgorithm which define myself in mybatis

toby1024 opened a new issue #170: How to sharding table by a column which type is Datetime or use ShardingAlgorithm which define myself in mybatis
URL: https://github.com/apache/incubator-shardingsphere-example/issues/170
 
 
   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
   
   ```java
   private static final String ORDER_TABLE = "illegal_result";
   
     @Override
     public Collection<String> doSharding(Collection<String> collection, HintShardingValue<String> shardingValue) {
   
       List<String> list = Lists.newArrayList(shardingValue.getValues());
       List<String> actualTable = Lists.newArrayList();
   
       // 页面上的查询条件会以json的方式传到shardingValue变量中
       String json = list.get(0);
       IllegalResultQueryCondition condition = JSON.parseObject(json, IllegalResultQueryCondition.class);
       // 查询条件没有orderId, 要查所有的分表
       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 {
         // 如果指定了orderId, 只查orderId所在的分表即可
         String tableSuffix = ShardingUtils.getDateIndex(condition.getCreatedAt());
         actualTable.add(ORDER_TABLE.concat(tableSuffix));
       }
   
       return actualTable;
     }
   ```
   
   and config in application.yml
   
   ```yaml
   
   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.
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services