自己测试用的 配置类
package com.shardingjdbc.shardingjdbcstu.config;
import lombok.AllArgsConstructor;
import org.apache.shardingsphere.core.yaml.config.sharding.YamlTableRuleConfiguration;
import org.apache.shardingsphere.shardingjdbc.spring.boot.sharding.SpringBootShardingRuleConfigurationProperties;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Map;
@AllArgsConstructor
@Component
public class TestConfig {
private final SpringBootShardingRuleConfigurationProperties shardingRuleConfigurationProperties;
@PostConstruct
public void init(){
Map<String, YamlTableRuleConfiguration> tables =
shardingRuleConfigurationProperties.getTables();
YamlTableRuleConfiguration order = tables.get("order");
String actualDataNodesTo = order.getActualDataNodes();
StringBuilder stringBuilder = new StringBuilder("ds0.order$->{[");
stringBuilder.append("202101,202102");
stringBuilder.append("]}");
order.setActualDataNodes(stringBuilder.toString());
String actualDataNodes = order.getActualDataNodes();
System.out.println(actualDataNodesTo);
System.out.println(actualDataNodes);
System.out.println(tables);
}
}
yml
spring:
shardingsphere:
props:
sql:
show: true
datasource:
names: ds0
ds0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/ksd_order_db?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: 123456
maxPoolSize: 100
minPoolSize: 5
sharding:
default-data-source-name: ds0
tables:
order:
actual-data-nodes: ds0.order$->{0..1}
table-strategy:
standard:
shardingColumn: userid
preciseAlgorithmClassName: com.shardingjdbc.shardingjdbcstu.algorithm.TenantShardingAlgorithm
分表节点配置的事0-1 test
@Select("select * from order where userid = #{userid} limit #{pageNo},#{pageSize}")
List<Order> findOrders(
@Param("userid") Long userid,
@Param("pageNo") Integer pageNo,
@Param("pageSize") Integer pageSize
);
@Select("select * from order limit #{pageNo},#{pageSize}")
List<Order> findOrdersTo(
@Param("pageNo") Integer pageNo,
@Param("pageSize") Integer pageSize
);
执行结果 最后一次查询应该是order0与order1,但是并没有这样执行,而是根据自己config的配置来的
|