一.创建用户表,写好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
*/
@Data
public 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.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.ccm.server.user.dao.mysql.mapper.UserInfoMapper" >
</mapper>
二.业务代码,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
*/
@Service
public 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);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.ccm.server.user.dao.mysql.mapper.UserInfoMapper" >
<insert id="insert" useGeneratedKeys="true" keyColumn="user_id"
keyProperty="userId" parameterType="com.ccm.server.user.dao.mysql.domain.UserInfo" >
insert into user_info
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="userId != null and userId != ''" >
user_id,
</if>
<if test="username != null and username != ''" >
username,
</if>
<if test="password != null and password != ''" >
`password`,
</if>
update_time,
create_time,
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="userId != null and userId != ''" >
#{userId,jdbcType=BIGINT},
</if>
<if test="username != null and username != ''" >
#{username,jdbcType=VARCHAR},
</if>
<if test="password != null and password != ''" >
#{password,jdbcType=VARCHAR},
</if>
now(),
now(),
</trim>
</insert>
<select id="selectByUsernameForUpdate" resultType="com.ccm.server.user.dao.mysql.domain.UserInfo">
select * from user_info where username = #{username} for update
</select>
</mapper>
三.测试.
1.启动gateway网关和server-user服务.
2.swagger测试注册接口.
3.查看数据表中是否插入成功.
您的点赞、收藏、转发和关注是我持续创作的动力!
源码地址:https://gitee.com/chouchimoo/ccm-mall.git(本章节代码分支:zj-26)