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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
| package com.mysql.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.mysql.entity.UserInfo; import com.mysql.entity.UserOrder; import com.mysql.mapper.UserInfoMapper; import com.mysql.mapper.UserOrderMapper; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit;
@Slf4j @Service public class ShardingService extends ServiceImpl<UserInfoMapper, UserInfo> { @Autowired private UserInfoMapper userInfoMapper; @Autowired private UserOrderMapper userOrderMapper;
private final ThreadPoolExecutor executor = new ThreadPoolExecutor( 10, 50, 60L, TimeUnit.SECONDS, new java.util.concurrent.LinkedBlockingQueue<>(1000), r -> new Thread(r, "sharding-async-" + System.currentTimeMillis()) );
@Transactional public void batchInsertUsers(List<UserInfo> users) { try { log.info("开始批量插入用户信息,数量: {}", users.size()); int batchSize = 1000; for (int i = 0; i < users.size(); i += batchSize) { int endIndex = Math.min(i + batchSize, users.size()); List<UserInfo> batch = users.subList(i, endIndex); CompletableFuture.runAsync(() -> { try { userInfoMapper.batchInsert(batch); log.info("批量插入用户信息完成,批次: {}-{}", i, endIndex); } catch (Exception e) { log.error("批量插入用户信息失败,批次: {}-{}", i, endIndex, e); } }, executor); } } catch (Exception e) { log.error("批量插入用户信息失败", e); throw new RuntimeException("批量插入用户信息失败", e); } }
@Transactional public void batchInsertOrders(List<UserOrder> orders) { try { log.info("开始批量插入订单信息,数量: {}", orders.size()); int batchSize = 1000; for (int i = 0; i < orders.size(); i += batchSize) { int endIndex = Math.min(i + batchSize, orders.size()); List<UserOrder> batch = orders.subList(i, endIndex); CompletableFuture.runAsync(() -> { try { userOrderMapper.batchInsert(batch); log.info("批量插入订单信息完成,批次: {}-{}", i, endIndex); } catch (Exception e) { log.error("批量插入订单信息失败,批次: {}-{}", i, endIndex, e); } }, executor); } } catch (Exception e) { log.error("批量插入订单信息失败", e); throw new RuntimeException("批量插入订单信息失败", e); } }
public UserInfo getUserById(Long userId) { try { log.debug("查询用户信息: userId={}", userId); QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("user_id", userId); return userInfoMapper.selectOne(queryWrapper); } catch (Exception e) { log.error("查询用户信息失败: userId={}", userId, e); throw new RuntimeException("查询用户信息失败", e); } }
public List<UserOrder> getOrdersByUserId(Long userId) { try { log.debug("查询用户订单列表: userId={}", userId); QueryWrapper<UserOrder> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("user_id", userId); queryWrapper.orderByDesc("create_time"); return userOrderMapper.selectList(queryWrapper); } catch (Exception e) { log.error("查询用户订单列表失败: userId={}", userId, e); throw new RuntimeException("查询用户订单列表失败", e); } }
@Transactional public boolean updateUserStatus(Long userId, String status) { try { log.info("更新用户状态: userId={}, status={}", userId, status); QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("user_id", userId); UserInfo userInfo = new UserInfo(); userInfo.setStatus(status); userInfo.setUpdateTime(LocalDateTime.now()); int result = userInfoMapper.update(userInfo, queryWrapper); log.info("更新用户状态完成: userId={}, result={}", userId, result); return result > 0; } catch (Exception e) { log.error("更新用户状态失败: userId={}", userId, e); throw new RuntimeException("更新用户状态失败", e); } }
@Transactional public boolean updateOrderStatus(Long orderId, String status) { try { log.info("更新订单状态: orderId={}, status={}", orderId, status); QueryWrapper<UserOrder> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("id", orderId); UserOrder order = new UserOrder(); order.setStatus(status); order.setUpdateTime(LocalDateTime.now()); if ("PAID".equals(status)) { order.setPayTime(LocalDateTime.now()); } int result = userOrderMapper.update(order, queryWrapper); log.info("更新订单状态完成: orderId={}, result={}", orderId, result); return result > 0; } catch (Exception e) { log.error("更新订单状态失败: orderId={}", orderId, e); throw new RuntimeException("更新订单状态失败", e); } }
public String getShardInfo(Long userId) { try { int dbIndex = (int) (userId % 4); int tableIndex = (int) (userId % 16); return String.format("数据库: ds%d, 表: user_info_%d", dbIndex, tableIndex); } catch (Exception e) { log.error("获取分片信息失败: userId={}", userId, e); return "未知分片"; } } }
|