博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用SpringCloud Alibaba搭建属于自己的微服务(二十六)~业务开发~用户注册
阅读量:4204 次
发布时间:2019-05-26

本文共 5041 字,大约阅读时间需要 16 分钟。

一.创建用户表,写好domain、mapper和mapper.xml

1.用户表.

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;

2.数据表映射实体domain.

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;}

3.Mapper.

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 {
}

4.Mapper.xml.

二.业务代码,server-user服务中编写.

1.控制层

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(); }}

2.业务层

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); }}

3.持久层

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(),

三.测试.

1.启动gateway网关和server-user服务.

2.swagger测试注册接口.

在这里插入图片描述在这里插入图片描述

3.查看数据表中是否插入成功.

在这里插入图片描述

您的点赞、收藏、转发和关注是我持续创作的动力!

源码地址:

转载地址:http://mktli.baihongyu.com/

你可能感兴趣的文章