实战 Java 第3章:登录、注册接口
1、添加数据库依赖,参照第二章
2、定义接口返回格式,entity/Response
Java Code复制内容到剪贴板
- package com.yoyo.tool.entity;
- public class Response {
- String message;
- int code;
- Boolean isSuccess = true;
- public Response(){}
- public Response(int code,String msg, Boolean isSuccess) {
- this.code = code;
- this.message = msg;
- this.isSuccess = isSuccess;
- }
- public Boolean getIsSuccess() {
- return isSuccess;
- }
- public void setIsSuccess(Boolean isSuccess){
- this.isSuccess = isSuccess;
- }
- public String getMessage() {
- return message;
- }
- public void setMsg(String msg) {
- this.message = msg;
- }
- public int getCode() {
- return code;
- }
- public void setCode(int code) {
- this.code = code;
- }
- }
3、定义用户表字段属性,entity/User
Java Code复制内容到剪贴板
- package com.yoyo.tool.entity;
- import org.springframework.util.DigestUtils;
- public class User {
- public String username;
- public String password;
- public String email;
- public int status;
- public User(){}
- public User(String username, String password, String email) {
- this.username = username;
- this.password = password;
- this.email = email;
- }
- public String getUserName(String username){
- return username;
- }
- public void setUsername(String username){
- this.username = username;
- }
- /**
- * 对原密码md5转换
- * @param password
- */
- public void setPassword(String password){
- // this.password_hash = DigestUtils.md5DigestAsHex(password.getBytes());
- this.password = password;
- }
- public String getPassword(){
- return password;
- }
- /**
- * 一组获取邮箱,设置邮箱,值为String型
- * @param email
- * @return
- */
- public String getEmail(String email){
- return email;
- }
- public void setEmail(String email){
- this.email = email;
- }
- /**
- * 一组获取状态,设置状态,值为int型
- * @param status
- * @return
- */
- public int getStatus(int status){
- return status;
- }
- public void setStatus(int status){
- this.status = status;
- }
- }
4、Dao层的增、查,resourses/mapper/UserMapper.xml
Java Code复制内容到剪贴板
- <?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.yoyo.tool.mapper.UserMapper">
- <resultMap id="BaseResultMap" type="com.yoyo.tool.entity.User">
- <result column="id" jdbcType="INTEGER" property="id" />
- <result column="username" jdbcType="VARCHAR" property="username" />
- <result column="email" jdbcType="VARCHAR" property="email" />
- </resultMap>
- <insert id="addUser" parameterType="com.yoyo.tool.entity.User">
- INSERT INTO `enewsuser` (`username`, `password`,`email`,`status`) VALUES(#{username},#{password},#{email},#{status})
- </insert>
- <select id="queryByUsername" resultType="com.yoyo.tool.entity.User">
- SELECT * FROM `enewsuser` WHERE username = #{username}
- </select >
- </mapper>
5、根据mapper写好的sql操作,操作数据库
Java Code复制内容到剪贴板
- package com.yoyo.tool.mapper;
- import com.yoyo.tool.entity.User;
- import org.apache.ibatis.annotations.Mapper;
- import java.util.List;
- @Mapper
- public interface UserMapper {
- int addUser(User user);
- List<User> queryByUsername(String username);
- }
6、impl实现具体的业务逻辑,service/impl/UserServiceIMpl
Java Code复制内容到剪贴板
- package com.yoyo.tool.service.impl;
- import com.yoyo.tool.entity.User;
- import com.yoyo.tool.mapper.UserMapper;
- import com.yoyo.tool.service.UserService;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- import java.util.List;
- @Service
- public class UserServiceImpl implements UserService {
- @Resource
- UserMapper userMapper;
- public int addUser(String username,String password,String email){
- User user = new User(username,password,email);
- // user.setUsername(username);
- // user.setEmail(email);
- // user.setPassword(password);
- // user.setStatus(0); // 默认正常
- int id = userMapper.addUser(user);
- return id;
- }
- public List<User> queryByUsername(String username){
- List <User> userList = userMapper.queryByUsername(username);
- return userList;
- }
- }
7、根据impl已经写好的具体业务逻辑,调用
Java Code复制内容到剪贴板
- package com.yoyo.tool.service;
- import com.yoyo.tool.entity.User;
- import java.util.List;
- public interface UserService {
- int addUser(String username,String password,String phone);//用于注册时,添加用户
- List<User> queryByUsername(String username);//用于登陆时,验证用户
- }
8、控制器,调用service
Java Code复制内容到剪贴板
- package com.yoyo.tool.controller;
- import com.yoyo.tool.entity.Response;
- import com.yoyo.tool.entity.User;
- import com.yoyo.tool.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;
- @RestController
- public class UserController {
- @Autowired
- private UserService service;
- @RequestMapping(value = "/register",method = RequestMethod.POST)
- public Response register(@RequestBody Map<String, String> person){
- String username = person.get("username");
- // int password = Integer.parseInt(person.get("password"));// 如果password必须为数字的话
- String password = person.get("password");
- String email = person.get("email");
- //1.判断用户名、密码、手机号是否为空
- // if(username != null && password != 0 && phone != null){ // 如果判断password必须为数字的话
- if(username != null && password != null){
- List<User> users = service.queryByUsername(username);
- // 2.判断是否有重复用户名
- if(users!=null && users.size()>0){
- return new Response(-1,"注册失败,用户名重复,请更换",true);
- }else {
- int count = service.addUser(username,password,email);
- if(count>0){
- // 3.没有重复用户名,注册成功
- return new Response(1,"注册成功",true);
- }else {
- return new Response(-1,"注册失败",true);
- }
- }
- }else{
- return new Response(-1,"注册失败,请检查用户名、密码、手机号是否为空",true);
- }
- }
- @RequestMapping(value = "/login",method = RequestMethod.POST)
- public Response login(@RequestBody Map<String, String> person){
- String username = person.get("username");
- // int password =Integer.parseInt(person.get("password"));// 如果password必须为数字的话
- String password = person.get("password");
- //1. 判断用户名、密码是否为空
- // if(username != null && password != 0 ){ // 如果password必须为数字的话
- if(username != null && password != null ){
- List<User> users = service.queryByUsername(username);
- //2. 判断用户名是否存在
- if(users!=null && users.size()>0){
- User user = users.get(0);
- //3. 判断密码是否正确,string不属于数据基本类型(byte short int long float double boolean char),所以==判断没有用,需要用equals
- if(password.equals(user.getPassword())){
- // System.out.println("可以使用 equals 来比较");
- return new Response(200,"登陆成功",true);
- }else{
- // System.out.println("不可以使用 equals 来比较");
- return new Response(400,"登陆失败,密码错误,传参密码:" + password + ",数据库密码:" + user.getPassword(),false);
- }
- // if(password == user.getPassword()){
- // //4. 密码正确,登陆成功
- // return new Response(200,"登陆成功",true);
- // }else {
- // return new Response(400,"登陆失败,密码错误,传参密码:" + password + ",数据库密码:" + user.getPassword(),false);
- // }
- }else {
- return new Response(400,"登陆失败,用户名不存在",false);
- }
- }else {
- return new Response(400,"登陆失败,请检查用户名、密码是否为空",false);
- }
- }
- }
下一篇 返回列表