注册功能正式开始
在business-reg新建RegController
package ml.yompc.myshop.plus.business.controller;
import ml.yompc.myshop.plus.commons.dto.ResponseResult;
import ml.yompc.myshop.plus.provider.api.UmsAdminService;
import ml.yompc.myshop.plus.provider.domain.UmsAdmin;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.CrossOrigin;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* 用户注册。
* @email yom535@outlook.com
* @author: 有民(yom535)
* @date: 2019/9/21
* @time: 16:05
*/
@CrossOrigin(origins = "*",maxAge = 3600)
@RestController
@RequestMapping(value = "reg")
public class RegController {
@Reference(version = "1.0.0")
private UmsAdminService umsAdminService;
/**
*
* @param umsAdmin {@link UmsAdmin}
* @return {@link ResponseResult}
*/
@PostMapping(value = "")
public ResponseResult<UmsAdmin> reg(@RequestBody UmsAdmin umsAdmin){
String message = validateReg(umsAdmin);
//通过验证
if (message==null)
{
int result = umsAdminService.inster(umsAdmin);
UmsAdmin admin = umsAdminService.get(umsAdmin.getUsername());
//注册成功
if (result>0)
{
return new ResponseResult<UmsAdmin>(HttpStatus.OK.value(),"用户注册成功",admin);
}
}
return new ResponseResult<UmsAdmin>(HttpStatus.NOT_ACCEPTABLE.value(),message!=null?message:"用户注册失败");
}
/**
* 注册信息验证
* @param umsAdmin {@link UmsAdmin}
* @return
*/
private String validateReg(UmsAdmin umsAdmin){
UmsAdmin admin = umsAdminService.get(umsAdmin.getUsername());
if (admin != null) {
return "用户名已存在,请重新输入";
}
return null;
}
}
在跟目录新建工具模块commons并加入跟pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ml.yompc.myshop.plus</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>commons</artifactId>
<packaging>pom</packaging>
<url>http://www.yompc.ml</url>
<inceptionYear>2018-Now</inceptionYear>
<licenses>
<license>
<name>Apache 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<developers>
<developer>
<id>yompc</id>
<name>FengJia Liu</name>
<email>yom535@outlook.com</email>
</developer>
</developers>
<modules>
<module>commons-dto</module>
</modules>
</project>
新建模块:通用的数据返回对象commons-dto
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ml.yompc.myshop.plus</groupId>
<artifactId>commons</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>commons-dto</artifactId>
<packaging>jar</packaging>
<url>http://www.yompc.ml</url>
<inceptionYear>2018-Now</inceptionYear>
<licenses>
<license>
<name>Apache 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<developers>
<developer>
<id>yompc</id>
<name>FengJia Liu</name>
<email>yom535@outlook.com</email>
</developer>
</developers>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
通用数据传输对象ResponseResult
package ml.yompc.myshop.plus.commons.dto;
import java.io.Serializable;
import lombok.Data;
/**
* @email yom535@outlook.com
* @author: 有民(yom535)
* @date: 2019/8/25
* @time: 22:59
*/
@Data
public class ResponseResult<T> implements Serializable {
private static final long serialVersionUID = 3468352004150968551L;
/**
* 状态码
*/
private Integer code;
/**
* 消息
*/
private String message;
/**
* 返回对象
*/
private T data;
public ResponseResult() {
super();
}
public ResponseResult(Integer code) {
super();
this.code = code;
}
public ResponseResult(Integer code, String message) {
super();
this.code = code;
this.message = message;
}
public ResponseResult(Integer code, Throwable throwable) {
super();
this.code = code;
this.message = throwable.getMessage();
}
public ResponseResult(Integer code, T data) {
super();
this.code = code;
this.data = data;
}
public ResponseResult(Integer code, String message, T data) {
super();
this.code = code;
this.message = message;
this.data = data;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((data == null) ? 0 : data.hashCode());
result = prime * result + ((message == null) ? 0 : message.hashCode());
result = prime * result + ((code == null) ? 0 : code.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
ResponseResult<?> other = (ResponseResult<?>) obj;
if (data == null) {
if (other.data != null) {
return false;
}
} else if (!data.equals(other.data)) {
return false;
}
if (message == null) {
if (other.message != null) {
return false;
}
} else if (!message.equals(other.message)) {
return false;
}
if (code == null) {
if (other.code != null) {
return false;
}
} else if (!code.equals(other.code)) {
return false;
}
return true;
}
/**
* 状态码
*/
public class CodeStatus {
/**
* 请求成功
*/
public static final int OK = 20000;
/**
* 请求失败
*/
public static final int FAIL = 20002;
/**
* 非法请求
*/
public static final int ILLEGAL_REQUEST = 50000;
/**
* 非法令牌
*/
public static final int ILLEGAL_TOKEN = 50008;
/**
* 其他客户登录
*/
public static final int OTHER_CLIENTS_LOGGED_IN = 50012;
/**
* 令牌已过期
*/
public static final int TOKEN_EXPIRED = 50014;
}
}
接口
package ml.yompc.myshop.plus.provider.api;
import ml.yompc.myshop.plus.provider.domain.UmsAdmin;
/**
* @email yom535@outlook.com
* @author: 有民(yom535)
* @date: 2019/9/20
* @time: 22:46
*/
public interface UmsAdminService {
/**
* 新增用户
* @param umsAdmin
* @return 大于0则新增成功
*/
int inster(UmsAdmin umsAdmin);
/**
* 获取用户
* @param username 用户名
* @return {@link UmsAdmin}
*/
UmsAdmin get(String username);
/**
* 获取用户
* @param umsAdmin {@link UmsAdmin}
* @return {@link UmsAdmin}
*/
UmsAdmin get(UmsAdmin umsAdmin);
}
实现接口
package ml.yompc.myshop.plus.provider.service;
import ml.yompc.myshop.plus.provider.api.UmsAdminService;
import ml.yompc.myshop.plus.provider.domain.UmsAdmin;
import ml.yompc.myshop.plus.provider.mapper.UmsAdminMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.time.LocalDateTime;
/**
* @email yom535@outlook.com
* @author: 有民(yom535)
* @date: 2019/9/20
* @time: 22:55
*/
@Service(version = "1.0.0")
public class UmsAdminServiceImpl implements UmsAdminService {
@Resource
private UmsAdminMapper umsAdminMapper;
@Resource
private BCryptPasswordEncoder passwordEncoder;
@Override
public int inster(UmsAdmin umsAdmin) {
//初始化用户对象
initUmsAdmin(umsAdmin);
return umsAdminMapper.insert(umsAdmin);
}
/**
* 根据条件查询
* @param username 用户名
* @return
*/
@Override
public UmsAdmin get(String username) {
Example example=new Example(UmsAdmin.class);
example.createCriteria().andEqualTo("username",username);
return umsAdminMapper.selectOneByExample(example);
}
/**
* 根据对象查询
* @param umsAdmin {@link UmsAdmin}
* @return
*/
@Override
public UmsAdmin get(UmsAdmin umsAdmin) {
return umsAdminMapper.selectOne(umsAdmin);
}
/**
* 初始化UmsAdmin
*/
private void initUmsAdmin(UmsAdmin umsAdmin){
//创建初始时间
umsAdmin.setCreateTime(LocalDateTime.now());
umsAdmin.setLoginTime(LocalDateTime.now());
//初始化状态
if (umsAdmin.getStatus() == null) {
umsAdmin.setStatus(0);
}
//密码加密
umsAdmin.setPassword(passwordEncoder.encode(umsAdmin.getPassword()));
}
}
使用Postman测试
