以下是需要注意的几点。
注册接口和登录接口需要用到的属性就是用户名username、密码password、手机号phone。
登录接口的逻辑处理
根据service服务类创建对应的mapper类。新建mapper文件包,在此文件包下新建UserMapper类,内容如下:
package com.dingding.mapper;
import com.dingding.entity.User;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* Created by xpwu on 2019/6/27.
*/
@Repository
public interface UserMapper{
int addUser(User user);
List queryByUsername(String username);
}
在此文件包下面新建User类,来用抽象用户信息,内容如下:
package com.dingding.entity;
/**
* Created by xpwu on 2019/6/18.
*/
public class User {
public String username;
public int password;
public String phone;
public User(){}
public User(String username, int password, String phone) {
this.username = username;
this.password = password;
this.phone = phone;
}
public String getName(){
return username;
}
public void setName(String username){
this.username = username;
}
public int getPassword(){
return password;
}
public void setPassword(int password){
this.password = password;
}
public String getPhone(){
return phone;
}
public void setPhone(String phone){
this.phone= phone;
}
}
判断用户名、密码、手机号是否为空,如果为空,注册失败;如果都不为空,根据用户名查询是否有重复用户名,如果有,注册失败;如果没有重复用户名,注册成功。
判断用户名、密码是否为空;如果都不为空,根据用户名查询数据库对应的密码;如果密码有误,登陆失败;如果密码正确,登陆成功。
package com.dingding.controller;
import com.dingding.entity.Response;
import com.dingding.entity.User;
import com.dingding.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
/**
* Created by xpwu on 2019/6/18.
*/
@RestController
public class UserController {
@Autowired
private UserService service;
@RequestMapping(value = '/register',method = RequestMethod.POST)
public Response register(@RequestBody Map person){
String username = person.get('username');
int password = Integer.parseInt(person.get('password'));
String phone = person.get('phone');
//1.判断用户名、密码、手机号是否为空
if(username != null && password != 0 && phone != null){
List users = service.queryByUsername(username);
//2.判断是否有重复用户名
if(users!=null && users.size()>0){
return new Response(true,'注册失败,用户名重复,请更换',-1);
}else {
int count = service.addUser(username,password,phone);
if(count>0){
//3.没有重复用户名,注册成功
return new Response(true,'注册成功',1);
}else {
return new Response(true,'注册失败',-1);
}
}
}else{
return new Response(true,'注册失败,请检查用户名、密码、手机号是否为空',-1);
}
}
@RequestMapping(value = '/login',method = RequestMethod.POST)
public Response login(@RequestBody Map person){
String username = person.get('username');
int password =Integer.parseInt(person.get('password'));
//1. 判断用户名、密码是否为空
if(username != null && password != 0 ){
List users = service.queryByUsername(username);
//2. 判断用户名是否存在
if(users!=null && users.size()>0){
User user = users.get(0);
//3. 判断密码是否正确
if(password == user.getPassword()){
//4. 密码正确,登陆成功
return new Response(true,'登陆成功',1);
}else {
return new Response(false,'登陆失败,密码错误',-1);
}
}else {
return new Response(true,'登陆失败,用户名不存在',-1);
}
}else {
return new Response(true,'登陆失败,请检查用户名、密码是否为空',-1);
}
}
}
新建controller文件包,在此文件包下新建UserController类,在此类中调用调用相关服务并处理逻辑,内容如下:
使用postman验证接口。
在此文件包下面新建Response类,用来返回接口调用信息描述。内容如下:
package com.dingding.entity;
/**
* Created by xpwu on 2019/6/28.
*/
public class Response {
String msg;
int code;
Boolean isSuc = true;
public Response(){}
public Response(Boolean isSuc,String msg, int code) {
this.msg = msg;
this.code = code;
this.isSuc = isSuc;
}
public Boolean getIsSuc() {
return isSuc;
}
public void setIsSuc(Boolean isSuc) {
this.isSuc = isSuc;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}
测试接口是否成功
创建mapper类
根据业务场景,需要提供一个注册服务与登录服务。分别为增加用户信息与根据用户名查询用户信息。
创建service服务类
在resources文件夹下,新建mapper文件夹,在此文件夹下新建相关的xml文件,内容如下。
INSERT INTO `user` VALUES(#{username},#{password},#{phone})
新建entity文件包
创建sql语句
注册接口的逻辑处理
调用服务,处理业务逻辑
查看数据库是否添加成功。
新建service文件包,在此文件包下新建UserService类编写接口,内容如下:
package com.dingding.service;
import com.dingding.entity.User;
import java.util.List;
/**
* Created by xpwu on 2019/6/18.
*/
public interface UserService{
int addUser(String username,int password,String phone);//用于注册时,添加用户
List queryByUsername(String username);//用于登陆时,验证用户
}
创建entity实体类
所以entity下面有两个实体类,User类和Response类。
验证注册接口选择请求方式为POST,在地址栏中输入http://localhost:8080/register在Body中添加相关参数。执行Send,选择Params,选择Body,得到如下结果,验证成功。验证登录接口将地址改为登录接口地址,输入用户名和密码;选择send,查看结果。
在service文件包下面新建impl文件包,在impl文件包下新建UserServiceImpl类来实现UserService接口。
package com.dingding.service.impl;
import com.dingding.entity.User;
import com.dingding.mapper.UserMapper;
import com.dingding.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by xpwu on 2019/6/28.
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
public int addUser(String username,int password,String phone){
User user = new User(username,password,phone);
int count = userMapper.addUser(user);
return count;
}
public List queryByUsername(String username){
List userList = userMapper.queryByUsername(username);
return userList;
}
}
文章为作者独立观点,不代表股票交易接口观点