ShardingJDBC ShardingKeyGenerator使用

基于版本

        <dependency>
            <groupId>org.apache.shardingspheregroupId>
            <artifactId>sharding-jdbc-coreartifactId>
            <version>4.0.0-RC2version>
        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配置

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;
    }

你可能感兴趣的:(ShardingJDBC)