liujie
2026-03-09 bbdd5f4f286fc593c5c30bd1bccd1f39af135dfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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("========================================");
    }
}