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 11:11:41 UTC
[GitHub] [incubator-shardingsphere] toby1024 opened a new issue #2958: How
to sharding table by a column which type is Datetime or use
ShardingAlgorithm which define myself in mybatis
toby1024 opened a new issue #2958: 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/issues/2958
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 = "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
```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