shardingConfig2

This commit is contained in:
2025-01-02 17:40:54 +08:00
parent 4157bac93f
commit f4998e1073
4 changed files with 220 additions and 172 deletions

View File

@@ -6,6 +6,7 @@ import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
@@ -105,6 +106,12 @@ public class ShardingConfig {
// 配置表的切分策略
shardingRuleConfig.setTableRuleConfigs(addTableRuleConfigs());
// 配置表绑定规则
List<Set<String>> sets = new ArrayList<>();
sets.add(regionTables);
shardingRuleConfig.setBindingTableGroups(bindingTableGroups(sets));
// 配置是否显示sql
Properties props = new Properties();
props.put("sql.show", showSql);
@@ -193,4 +200,38 @@ public class ShardingConfig {
});
return sets;
}
/**
* 绑定表的分片规则
*/
public static Set<String> bindingTableGroups(List<Set<String>> sets) {
if (sets != null && !sets.isEmpty()) {
Set<String> tableGroups = new HashSet<>();
for (Set<String> set : sets) {
String tableNames = bindingTable(set.toArray(new String[0]));
if (StringUtils.isNotBlank(tableNames)) {
tableGroups.add(tableNames);
}
}
return tableGroups;
}
return null;
}
/**
* 批量绑定表规则
*
* @param tables 批量绑定的标规则
* @return tableNames
*/
private static String bindingTable(String... tables) {
StringBuilder tableNames = new StringBuilder();
if (tables != null && tables.length != 0) {
for (String table : tables) {
tableNames.append(table).append(",");
}
tableNames.deleteCharAt(tableNames.length() - 1);
}
return tableNames.toString();
}
}

View File

@@ -101,14 +101,14 @@ public class CourseCollectServiceImpl extends ServiceImpl<CourseCollectDao, Cour
@CachePut(value = "app:courseCollect", key = "#userId")
public Result selectByUserId(Integer page, Integer limit, Long userId,Integer classify) {
Page<Course> pages=new Page<>(page,limit);
IPage<Course> courseIPage = baseMapper.selectCourseByCollect(pages, userId,classify);
List<Course> courses = courseIPage.getRecords();
if (courses != null && courses.size() > 0) {
for (Course course : courses) {
course.setCourseClassification(courseClassificationDao.selectById(course.getClassifyId()));
}
}
return Result.success().put("data",courseIPage);
// IPage<Course> courseIPage = baseMapper.selectCourseByCollect(pages, userId,classify);
// List<Course> courses = courseIPage.getRecords();
// if (courses != null && courses.size() > 0) {
// for (Course course : courses) {
// course.setCourseClassification(courseClassificationDao.selectById(course.getClassifyId()));
// }
// }
return Result.success().put("data",null);
}