本文共 5041 字,大约阅读时间需要 16 分钟。
CREATE TABLE `user_info` ( `user_id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键', `username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '用户名', `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '密码', `update_time` datetime(0) NULL DEFAULT NULL COMMENT '数据最后修改时间', `create_time` datetime(0) NULL DEFAULT NULL COMMENT '数据的创建时间', PRIMARY KEY (`user_id`) USING BTREE) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Dynamic;
package com.ccm.server.user.dao.mysql.domain;import lombok.Data;import java.util.Date;/** * @Description user_info表实体类映射 * @Author zhouzhiwu * @CreateTime 2020/08/05 15:19 */@Datapublic class UserInfo { private Long userId; private String username; private String password; private Date updateTime; private Date createTime;}
package com.ccm.server.user.dao.mysql.mapper;import com.ccm.server.user.dao.mysql.domain.UserInfo;import org.apache.ibatis.annotations.Param;/** * @Description user_info表mapper * @Author ccm * @CreateTime 2020/08/05 15:20 */public interface UserInfoMapper { }
package com.ccm.server.user.controller;import com.ccm.common.exception.result.ResultSet;import com.ccm.server.user.controller.req.UserRegisterReq;import com.ccm.server.user.service.UserService;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.validation.Valid;@Api(tags = "用户控制层")@RestController@RequestMapping(value = "user")public class UserController { @Autowired private UserService userService; @ApiOperation(value = "注册") @PostMapping(value = "register") public ResultSet register(@Valid @RequestBody UserRegisterReq userRegisterReq) { userService.register(userRegisterReq.getUsername(),userRegisterReq.getPassword()); return ResultSet.success(); }}
package com.ccm.server.user.service;/** * @Description 用户业务层 * @Author ccm * @CreateTime 2020/8/5 15:07 */public interface UserService { /** * @Description 注册 * @Author ccm * @CreateTime 2020/8/5 15:16 * @Params [username, password] * @Return java.lang.Integer */ Integer register(String username,String password);}
package com.ccm.server.user.service.impl;import com.ccm.common.exception.CustomerException;import com.ccm.server.user.dao.mysql.domain.UserInfo;import com.ccm.server.user.dao.mysql.mapper.UserInfoMapper;import com.ccm.server.user.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;/** * @Description 用户业务层实现 * @Author ccm * @CreateTime 2020/08/05 15:17 */@Servicepublic class UserServiceImpl implements UserService { @Autowired private UserInfoMapper userInfoMapper; @Override @Transactional public Integer register(String username, String password) { //判断用户名是否被占用 UserInfo userInfo = userInfoMapper.selectByUsernameForUpdate(username); //加入写锁,避免高并发情况下出现用户名重复的情况 if(userInfo != null) { throw new CustomerException("该用户名已经被占用"); } //插入数据 userInfo = new UserInfo(); userInfo.setUsername(username); userInfo.setPassword(password); return userInfoMapper.insert(userInfo); }}
package com.ccm.server.user.dao.mysql.mapper;import com.ccm.server.user.dao.mysql.domain.UserInfo;import org.apache.ibatis.annotations.Param;/** * @Description user_info表mapper * @Author ccm * @CreateTime 2020/08/05 15:20 */public interface UserInfoMapper { int insert(UserInfo userInfo); UserInfo selectByUsernameForUpdate(@Param("username") String username);}
insert into user_info user_id, username, `password`, update_time, create_time,#{userId,jdbcType=BIGINT}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, now(), now(),
您的点赞、收藏、转发和关注是我持续创作的动力!
源码地址:
转载地址:http://mktli.baihongyu.com/