From 7dcb9220410d7db82c8b9e9d48fde262039b7f5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E5=B0=8F=E5=A2=A8?= <2291200076@qq.com> Date: Wed, 2 Mar 2022 11:36:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=8B=E6=9C=BA=E5=8F=B7=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E6=B3=A8=E5=86=8C=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Others/sql/flashsale.sql | 11 +++++++---- .../flashsale/service/impl/UserServiceImpl.java | 7 ++++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Others/sql/flashsale.sql b/Others/sql/flashsale.sql index ac3997b..4d43fb3 100644 --- a/Others/sql/flashsale.sql +++ b/Others/sql/flashsale.sql @@ -11,7 +11,7 @@ Target Server Version : 50726 File Encoding : 65001 - Date: 02/03/2022 10:30:12 + Date: 02/03/2022 11:35:11 */ SET NAMES utf8mb4; @@ -29,13 +29,15 @@ CREATE TABLE `user_info` ( `telephone` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', `register_mode` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '通过手机号注册,通过微信注册,通过支付宝注册', `third_party_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', - PRIMARY KEY (`id`) USING BTREE -) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic; + PRIMARY KEY (`id`) USING BTREE, + UNIQUE INDEX `telephone_unique_index`(`telephone`) USING BTREE +) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Records of user_info -- ---------------------------- INSERT INTO `user_info` VALUES (1, 'admin', 2, 18, '110', 'phone', ''); +INSERT INTO `user_info` VALUES (2, 'user-1wk6xq-59489', 1, 49, '18900000001', 'phone', ''); -- ---------------------------- -- Table structure for user_password @@ -48,11 +50,12 @@ CREATE TABLE `user_password` ( PRIMARY KEY (`id`) USING BTREE, INDEX `user_id`(`user_id`) USING BTREE, CONSTRAINT `user_password_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user_info` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT -) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic; +) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Records of user_password -- ---------------------------- INSERT INTO `user_password` VALUES (1, '21232f297a57a5a743894a0e4a801fc3', 1); +INSERT INTO `user_password` VALUES (2, 'bnW2mQTNe40qWH3b5atFWw==', 2); SET FOREIGN_KEY_CHECKS = 1; diff --git a/src/main/java/com/cxyxiaomo/flashsale/service/impl/UserServiceImpl.java b/src/main/java/com/cxyxiaomo/flashsale/service/impl/UserServiceImpl.java index 9029395..a0e2149 100644 --- a/src/main/java/com/cxyxiaomo/flashsale/service/impl/UserServiceImpl.java +++ b/src/main/java/com/cxyxiaomo/flashsale/service/impl/UserServiceImpl.java @@ -11,6 +11,7 @@ import com.cxyxiaomo.flashsale.service.model.UserModel; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DuplicateKeyException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -50,7 +51,11 @@ public class UserServiceImpl implements UserService { // 实现 model -> dataobject 方法 UserDO userDO = convertFormModel(userModel); - userDOMapper.insertSelective(userDO); + try { + userDOMapper.insertSelective(userDO); + } catch (DuplicateKeyException ex) { + throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "手机号已被注册"); + } userModel.setId(userDO.getId());