基于版本
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>4.0.0-RC2</version>
</dependency>
定义KeyGenerator
public class UidGeneratorShardingKeyGenerator implements ShardingKeyGenerator {
public static Integer ID_START=18;
public Comparable<?> generateKey() {
return ID_START++;
}
public String getType() {
return "UidGenerator";
}
public Properties getProperties() {
Properties val = new Properties();
val.setProperty("name", "22");
return val;
}
public void setProperties(Properties properties) {
}
}
定义SPI配置
org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator文件内容如下:
com.wjg.apitest1.util.UidGeneratorShardingKeyGenerator
TableRule中配置
private static TableRuleConfiguration getOrderTableRuleConfiguration() {
TableRuleConfiguration result = new TableRuleConfiguration("t_order");
//这里的UidGenerator来自UidGeneratorShardingKeyGenerator中的getType()
result.setKeyGeneratorConfig(new KeyGeneratorConfiguration("UidGenerator", "order_id"));
return result;
}
版权声明:本文为wujingang原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。