之前我们实现了java如何链接数据库,接下来就开始尝试进行业务逻辑接口开发,主要进行登录和注册接口的开发
一、改造实体类user/UserInfo
注册接口和登录接口需要用到的属性就是用户名 username、密码 password、手机号 phone。在原有逻辑上加入phone电话属性字段
package com.example.study.user;
public class UserInfo {
public static String username;
public static String password;
public static String phone;
public UserInfo(String username, String password, String phone) {
UserInfo.username = username;
UserInfo.password = password;
UserInfo.phone = phone;
}
public static void setName(String username){
UserInfo.username = username;
}
public static void setPassword(String password){
UserInfo.password = password;
}
public static void setPhone(String phone){
UserInfo.phone = phone;
}
public String getPassword(){
return password;
}
public String getUsername(){
return username;
}
public String getPhone(){
return phone;
}
}
二、改造实体类user/Response
新增接口状态字段success,这样集成了接口的基本三字段code/msg/success,用来返回接口调用信息描述。
package com.example.study.user;
public class Response {
private static String msg;
private static int code;
private static Boolean success;
public Response(Boolean success,String msg, int code) {
Response.msg = msg;
Response.code = code;
Response.success = success;
}
public static void setMsg(String msg) {
Response.msg = msg;
}
public static void setCode(int code) {
Response.code = code;
}
public static void setSuccess(Boolean success) {
Response.success = success;
}
public int getCode() {return code;}
public String getMsg() {return msg;}
public Boolean getSuccess() {return success;}
}
三、改造服务类 service /UserApi
package com.example.study.service;
import com.example.study.user.UserInfo;
import java.util.List;
public interface UserApi {
int addUser(String username,String password,String phone);//注册时,添加用户
List<UserInfo> queryByUsername(String username);//登陆时,验证用户
}
四、改造服务类 service /impl /UserServiceImpl
package com.example.study.service.impl;
import com.example.study.user.UserInfo;
import com.example.study.mapper.UserMapper;
import com.example.study.service.UserApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserApi {
@Autowired
UserMapper userMapper;
public int addUser(String username,String password,String phone){
UserInfo user = new UserInfo(username,password,phone);
return userMapper.addUser(user);
}
public List <UserInfo> queryByUsername(String username){
return userMapper.queryByUsername(username);
}
}
五、改造map类mapper/UserMapper
package com.example.study.mapper;
import com.example.study.user.UserInfo;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserMapper {
int addUser(UserInfo user);
List<UserInfo> queryByUsername(String username);
}
六、改造sql逻辑resources/mapper/UserMapper.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.example.study.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.example.study.user.UserInfo">
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="phone" jdbcType="VARCHAR" property="phone" />
</resultMap>
<insert id="addUser" parameterType="com.example.study.user.UserInfo">
INSERT INTO `user` VALUES(#{username},#{password},#{phone})
</insert>
<select id="queryByUsername" resultType="com.example.study.user.UserInfo">
SELECT * FROM `user` WHERE username = #{username}
</select >
</mapper>
七、改造控制类UserController调用
package com.example.study.controller;
import com.example.study.user.Response;
import com.example.study.user.UserInfo;
import com.example.study.service.UserApi;
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 org.springframework.web.servlet.config.annotation.EnableWebMvc;
import java.util.List;
import java.util.Map;
@RestController
@EnableWebMvc
public class UserController {
@Autowired
public UserApi service;
@RequestMapping(value = "/register",method = RequestMethod.POST)
public Response register(@RequestBody Map<String, String> person){
System.out.println(person);
String username = person.get("username");
String password = person.get("password");
String phone = person.get("phone");
//1.判断用户名、密码、手机号是否为空
if(username != null && password != null && phone != null){
List<UserInfo> users = service.queryByUsername(username);
//2.判断是否有重复用户名
if(users!=null && !users.isEmpty()){
return new Response(true,"注册失败,用户名重复,请更换",400);
}else {
int count = service.addUser(username,password,phone);
if(count>0){
//3.没有重复用户名,注册成功
return new Response(true,"注册成功",200);
}else {
return new Response(true,"注册失败",400);
}
}
}else{
return new Response(true,"注册失败,请检查用户名、密码、手机号是否为空",400);
}
}
@RequestMapping(value = "login",method = RequestMethod.POST)
public Response login(@RequestBody Map<String, String> person){
String username = person.get("username");
String password =person.get("password");
//1. 判断用户名、密码是否为空
if(username != null && password != null ){
List<UserInfo> users = service.queryByUsername(username);
//2. 判断用户名是否存在
if(users!=null && !users.isEmpty()){
UserInfo user = users.getFirst();
//3. 判断密码是否正确
if(password.equals(user.getPassword())){
//4. 密码正确,登陆成功
return new Response(true,"登陆成功",200);
}else {
return new Response(false,"登陆失败,密码错误",400);
}
}else {
return new Response(true,"登陆失败,用户名不存在",400);
}
}else {
return new Response(true,"登陆失败,请检查用户名、密码是否为空",400);
}
}
}
八、运行项目并测试接口
1、注册接口请求成功