package com.xiaopeng.order;
|
|
import com.xiaopeng.order.utils.ShardingQueryHelper;
|
import com.xiaopeng.order.utils.ShardingTableUtil;
|
import lombok.extern.slf4j.Slf4j;
|
|
/**
|
* 分表工具使用示例
|
* 演示如何使用 ShardingTableUtil 和 ShardingQueryHelper
|
*/
|
@Slf4j
|
public class ShardingUtilExample {
|
|
/**
|
* 示例1: 根据用户ID查询订单
|
*/
|
public void example1_queryOrderByUserId(Long userId) {
|
// 1. 获取对应的分表名
|
String tableName = ShardingTableUtil.getOrderTableName(userId);
|
log.info("用户ID {} 对应的订单表: {}", userId, tableName);
|
|
// 2. 构建查询SQL(在实际开发中,推荐使用MyBatis的动态SQL)
|
String sql = String.format("SELECT * FROM %s WHERE app_user_id = %d AND status = 1",
|
tableName, userId);
|
log.info("查询SQL: {}", sql);
|
}
|
|
/**
|
* 示例2: 根据订单ID查询订单详细信息
|
*/
|
public void example2_queryOrderDetails() {
|
Long orderId = 67890L;
|
|
// 查询订单详情表
|
String detailTableName = ShardingTableUtil.getOrderDetailTableName(orderId);
|
log.info("订单ID {} 对应的详情表: {}", orderId, detailTableName);
|
|
// 查询订单支付表
|
String paymentTableName = ShardingTableUtil.getOrderPaymentTableName(orderId);
|
log.info("订单ID {} 对应的支付表: {}", orderId, paymentTableName);
|
|
// 查询订单评价表
|
String evaluateTableName = ShardingTableUtil.getOrderEvaluateTableName(orderId);
|
log.info("订单ID {} 对应的评价表: {}", orderId, evaluateTableName);
|
|
// 使用QueryHelper构建查询
|
String detailSql = ShardingQueryHelper.OrderQueryBuilder.queryOrderDetailByOrderId(orderId);
|
log.info("订单详情查询SQL: {}", detailSql);
|
}
|
|
/**
|
* 示例3: 查询所有订单(全表扫描 - UNION ALL)
|
*/
|
public void example3_queryAllOrders() {
|
// 场景:查询所有状态为已完成的订单,按创建时间倒序,取前100条
|
String sql = ShardingQueryHelper.OrderQueryBuilder.queryAllOrders(
|
"status IN (9, 10)", // WHERE条件
|
"create_time DESC", // 排序
|
100 // 限制数量
|
);
|
|
log.info("全表查询SQL (已简化显示):");
|
log.info(sql.substring(0, Math.min(500, sql.length())) + "...");
|
}
|
|
/**
|
* 示例4: 统计订单数量
|
*/
|
public void example4_countOrders() {
|
// 统计所有已完成的订单数量
|
String countSql = ShardingQueryHelper.ShardingCountHelper.countAllOrders(
|
"status IN (9, 10)"
|
);
|
|
log.info("订单统计SQL:");
|
log.info(countSql);
|
}
|
|
/**
|
* 示例5: 批量获取表名
|
*/
|
public void example5_getAllTableNames() {
|
// 获取所有订单分表的名称
|
String[] allTables = ShardingTableUtil.getAllOrderTableNames();
|
|
log.info("所有订单分表(共{}个):", allTables.length);
|
for (String table : allTables) {
|
log.info(" - {}", table);
|
}
|
}
|
|
/**
|
* 示例6: 计算分表索引
|
*/
|
public void example6_calculateShardingIndex() {
|
Long[] userIds = {100L, 200L, 12345L, 67890L, 999999L};
|
|
log.info("用户ID与分表索引映射:");
|
for (Long userId : userIds) {
|
int index = ShardingTableUtil.calculateShardingIndex(userId, 20);
|
String tableName = ShardingTableUtil.getOrderTableName(userId);
|
log.info(" 用户ID: {} -> 索引: {} -> 表名: {}", userId, index, tableName);
|
}
|
}
|
|
/**
|
* 示例7: 通用分表查询
|
*/
|
public void example7_genericShardingQuery() {
|
// 使用通用方法构建查询
|
String sql = ShardingQueryHelper.buildSingleTableQuery(
|
"xp_order", // 基础表名
|
12345L, // 分片键值
|
20, // 分片数量
|
"status = 1 AND create_time > '2026-01-01'" // WHERE条件
|
);
|
|
log.info("通用分表查询SQL: {}", sql);
|
}
|
|
/**
|
* 示例8: 构建UNION ALL查询
|
*/
|
public void example8_buildUnionAllQuery() {
|
String unionSql = ShardingQueryHelper.buildUnionAllQuery(
|
"xp_order", // 基础表名
|
20, // 分片数量
|
"status = 1", // WHERE条件
|
"create_time DESC", // 排序
|
1000 // LIMIT
|
);
|
|
log.info("UNION ALL查询SQL (前500字符):");
|
log.info(unionSql.substring(0, Math.min(500, unionSql.length())) + "...");
|
}
|
|
/**
|
* 主方法 - 运行所有示例
|
*/
|
public static void main(String[] args) {
|
log.info("========================================");
|
log.info("分表工具使用示例");
|
log.info("========================================\n");
|
|
ShardingUtilExample example = new ShardingUtilExample();
|
|
log.info(">>> 示例1: 根据用户ID查询订单");
|
example.example1_queryOrderByUserId(8107912L);
|
// log.info("");
|
//
|
// log.info(">>> 示例2: 根据订单ID查询订单详细信息");
|
// example.example2_queryOrderDetails();
|
// log.info("");
|
//
|
// log.info(">>> 示例3: 查询所有订单(全表扫描)");
|
// example.example3_queryAllOrders();
|
// log.info("");
|
//
|
// log.info(">>> 示例4: 统计订单数量");
|
// example.example4_countOrders();
|
// log.info("");
|
//
|
// log.info(">>> 示例5: 批量获取表名");
|
// example.example5_getAllTableNames();
|
// log.info("");
|
//
|
// log.info(">>> 示例6: 计算分表索引");
|
// example.example6_calculateShardingIndex();
|
// log.info("");
|
//
|
// log.info(">>> 示例7: 通用分表查询");
|
// example.example7_genericShardingQuery();
|
// log.info("");
|
//
|
// log.info(">>> 示例8: 构建UNION ALL查询");
|
// example.example8_buildUnionAllQuery();
|
// log.info("");
|
//
|
// // 打印分表配置信息
|
// log.info("\n========================================");
|
// ShardingTableUtil.printShardingInfo();
|
//
|
// // 打印查询示例
|
// log.info("");
|
// ShardingQueryHelper.printQueryExamples();
|
//
|
// log.info("========================================");
|
// log.info("所有示例执行完成!");
|
// log.info("========================================");
|
}
|
}
|