Reputation: 1
version
springboot version is 2.3.5.RELEASE
mybatis-plus 3.5.1
sharding-jdbc-spring-boot-starter 4.1.1
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
</parent>
<properties>
<mybatis-plus.version>3.5.1</mybatis-plus.version>
<sharding-jdbc>4.1.1</sharding-jdbc>
</properties>
bootstrap.properties
spring.shardingsphere.datasource.names=ds
spring.shardingsphere.datasource.ds.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds.jdbc-url=jdbc:mysql://xxx:3306/XXX?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false
spring.shardingsphere.datasource.ds.username=xxx
spring.shardingsphere.datasource.ds.password=xxx
spring.shardingsphere.sharding.tables.geo.actual-data-nodes=ds.geo_$->{0..4}
spring.shardingsphere.sharding.tables.geo.table-strategy.standard.sharding-column=area_code
spring.shardingsphere.sharding.tables.geo.table-strategy.standard.precise-algorithm-class-name=com.xxx.xxx.basic.web.config.shardingJdbc.GeoPreciseAlgorithm
spring.shardingsphere.sharding.tables.geo.table-strategy.standard.range-algorithm-class-name=com.xxx.xxx.basic.web.config.shardingJdbc.GeoRangeAlgorithm
spring.shardingsphere.props.sql.show=true
description
one database:ds.
logic table: geo.
sharding-column: area_code
geo is divided into 4 tables:geo_0 geo_1 geo_2 geo_3.
set custom Algorithm GeoPreciseAlgorithm and GeoRangeAlgorithm
public class GeoPreciseAlgorithm implements PreciseShardingAlgorithm<String> {
public GeoPreciseAlgorithm() {
}
@Override
public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<String> shardingValue) {
String areaCode = shardingValue.getValue();
GridType gridType = GridType.getGridTypeByAreaCode(areaCode);
if (ObjectUtil.isNull(gridType)) {
throw new BizException(ResponseEnum.EXCEPTION.getCode(), "params error!");
}
for (String availableTargetName : availableTargetNames) {
String tableSuffix = availableTargetName.substring(availableTargetName.lastIndexOf("_"));
if (Convert.toStr(gridType.getValue()).equals(tableSuffix)) {
return availableTargetName;
}
}
throw new UnsupportedOperationException();
}
}
public class GeoRangeAlgorithm implements RangeShardingAlgorithm<String> {
public GeoRangeAlgorithm() {
}
@Override
public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<String> shardingValue) {
Range<String> range = shardingValue.getValueRange();
Integer leftBoundaryValue = Convert.toInt(range.lowerEndpoint());
Integer rightBoundaryValue = Convert.toInt(range.upperEndpoint());
List<String> resultCollection = new ArrayList<>();
for (String availableTargetName : availableTargetNames) {
Integer point = Convert.toInt(availableTargetName.substring(availableTargetName.lastIndexOf("_")));
if (leftBoundaryValue.compareTo(point) > 0 || rightBoundaryValue.compareTo(point) < 0) {
continue;
}
resultCollection.add(availableTargetName);
}
if (CollectionUtil.isEmpty(resultCollection)) {
throw new UnsupportedOperationException();
}
return resultCollection;
}
}
error
I don't know why the data can be save into logic table? my custom Algorithm are no use please give an answer thanks
1.alter properties 2.alter mybatis-plus configuration
Upvotes: 0
Views: 103