• sharding-jdbc


    版本4.0.0-4.1.1

    // 配置表规则
    TableRuleConfiguration tableRuleConfiguration = new TableRuleConfiguration("table_name","databasename.table_name_${0..3}");

    //设置分片策略,这里简单起见直接取模,也可以使用自定义算法来实现分片规则
    tableRuleConfiguration.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("applicationId", "table_name_${applicationId % 4}"));
    //tableRuleConfiguration.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "id"));
    // 配置分片规则
    ShardingRuleConfiguration shardingRuleConfiguration = new ShardingRuleConfiguration();
    shardingRuleConfiguration.getTableRuleConfigs().add(tableRuleConfiguration);
    shardingRuleConfiguration.getBindingTableGroups().add("table_name");
    // 配置默认分表策略
    shardingRuleConfiguration.setDefaultTableShardingStrategyConfig(
    new InlineShardingStrategyConfiguration("applicationId", "sale_bill_detail_${applicationId % 4}")
    );
    // 配置默认分库策略
    DataSource datasource = BeanUtil.getDuidDataSource(driverClass,url,user,password);

    Properties props=new Properties();
    //props.put("sql.show", "true");
    Map<String, DataSource> dataSourceMap = new HashMap<>();
    dataSourceMap.put("databasename", datasource);
    return ShardingDataSourceFactory.createDataSource(dataSourceMap,
    shardingRuleConfiguration, props);

    版本:3.0.0

    <!--sharding-jdbc-->
    <dependency>
    <groupId>io.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-namespace</artifactId>
    <version>3.0.0</version>
    </dependency>

    TableRuleConfiguration tableRuleConfiguration = new TableRuleConfiguration();
    tableRuleConfiguration.setLogicTable("sale_bill_detail");
    tableRuleConfiguration.setActualDataNodes("mydatabase_name.sale_bill_detail_${0..3}");
    ///设置分片策略,这里简单起见直接取模,也可以使用自定义算法来实现分片规则
    tableRuleConfiguration.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("applicationId", "table_name_${applicationId % 4}"));
    //tableRuleConfiguration.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "id"));
    // 配置分片规则
    ShardingRuleConfiguration shardingRuleConfiguration = new ShardingRuleConfiguration();
    shardingRuleConfiguration.getTableRuleConfigs().add(tableRuleConfiguration);
    shardingRuleConfiguration.getBindingTableGroups().add("table_name");
    // 配置默认分表策略
    shardingRuleConfiguration.setDefaultTableShardingStrategyConfig(
    new InlineShardingStrategyConfiguration("applicationId", "sale_bill_detail_${applicationId % 4}")
    );
    // 配置默认分库策略
    DataSource datasource = BeanUtil.getDuidDataSource(driverClass,url,user,password);


    Properties props=new Properties();
    //props.put("sql.show", "true");
    Map<String, DataSource> dataSourceMap = new HashMap<>();
    dataSourceMap.put("mydatabase_name", datasource);
    return ShardingDataSourceFactory.createDataSource(dataSourceMap,
    shardingRuleConfiguration, null, props);

  • 相关阅读:
    mysql精简单机版,免登录,可复制,不启动服务,与本机mysql无冲突
    C# Mutex在Release下有时无效的注意事项
    20192424王昭云 202120222 《网络与系统攻防技术》实验八实验报告
    20192424王昭云 202120222 《网络与系统攻防技术》实验八实验报告修改版
    城市CIM基础平台需求调查结果
    SpringBoot 注解学习记录
    cmake添加指定源文件的命令
    IIS 不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。
    正则表达式
    DOM知识点总结
  • 原文地址:https://www.cnblogs.com/andydao/p/14004754.html
Copyright © 2020-2023  润新知