spring boot + vue 前后分离实现登录功能(三)

Spring boot 后台

github 地址 SpringBoot-book-vue-demo

使用tk.mytabis 简化mybatis 开发

使用 durid 连接池 连接Mysql

pom.xml

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lick</groupId>
    <artifactId>vue_demo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>vue_demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <mysql.version>8.0.15</mysql.version>

    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- Druid 数据库连接 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>${mysql.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>3.4.4</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

实体类 TbUser.java

package com.lick.vue_demo.domain;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

@Table(name = "tb_user")
public class TbUser {
    /**
     * 用户ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    /**
     * 登录名
     */
    @NotNull(message = "用户名不能为空")
    private String username;

    /**
     * 登录密码
     */
    @NotNull(message = "密码不能为空")
    private String password;

    /**
     * 手机号
     */
    private String phone;

    /**
     * 获取用户ID
     *
     * @return id - 用户ID
     */
    public Integer getId() {
        return id;
    }

    /**
     * 设置用户ID
     *
     * @param id 用户ID
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     * 获取登录名
     *
     * @return username - 登录名
     */
    public String getUsername() {
        return username;
    }

    /**
     * 设置登录名
     *
     * @param username 登录名
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * 获取登录密码
     *
     * @return password - 登录密码
     */
    public String getPassword() {
        return password;
    }

    /**
     * 设置登录密码
     *
     * @param password 登录密码
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * 获取手机号
     *
     * @return phone - 手机号
     */
    public String getPhone() {
        return phone;
    }

    /**
     * 设置手机号
     *
     * @param phone 手机号
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }
}

登录控制层 LoginController.java

package com.lick.vue_demo.controller;

import com.lick.vue_demo.domain.TbUser;
import com.lick.vue_demo.mapper.TbUserMapper;
import com.lick.vue_demo.result.AjaxResult;
import com.lick.vue_demo.result.ResultFactory;
import com.lick.vue_demo.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.validation.Valid;


@RestController
@CrossOrigin
public class LoginController {
    @Autowired
    private TbUserMapper tbUserMapper;

    @RequestMapping(value = "/api/login", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public AjaxResult login(@Valid @RequestBody TbUser tbUser, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String message = String.format("登陆失败,详细信息[%s]。", bindingResult.getFieldError().getDefaultMessage());
            return ResultFactory.buildFailResult(message);
        }
        Example example = new Example(TbUser.class);
        example.createCriteria().andEqualTo("username",tbUser.getUsername());
        TbUser tbUser1 = tbUserMapper.selectOneByExample(example);
        if(StringUtils.isNotNull(tbUser1)) {
            if ( !tbUser.getPassword().equals(tbUser1.getPassword())) {
                String message = String.format("登陆失败,详细信息[密码不正确]。");
                return ResultFactory.buildFailResult(message);
            }else {
                return ResultFactory.buildSuccessResult(tbUser1);
            }
        }else {
            String message = String.format("登陆失败,详细信息[用户名不正确]。");
            return ResultFactory.buildFailResult(message);
        }

    }
}


返回值实体类 Result.java

package com.lick.vue_demo.result;

/**
 * 通用返回值实例
 */
public class AjaxResult {
    /**
     *  响应状态码
     */
    private int code;
    /**
     *  响应状态消息
     */
    private String msg;
    /**
     *  响应结果对象
     */
    private Object object;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }


    public Object getObject() {
        return object;
    }

    public void setObject(Object object) {
        this.object = object;
    }

    AjaxResult (int code,String msg,Object data) {
        this.code = code;
        this.msg = msg;
        this.object = data;
    }
}

代码编号 ResultCode.java

package com.lick.vue_demo.result;

public enum ResultCode {
    /**
     * 成功
     */
    SUCCESS(200),
    /**
     * 失败
     */
    FAIL(400),

    /**
     * 未认证(签名错误)
     */
    UNAUTHORIZED(401),

    /**
     * 接口不存在
     */
    NOT_FOUND(404),

    /**
     * 服务器内部错误
     */
    INTERNAL_SERVER_ERROR(500);

    public int code;

    ResultCode(int code) {
        this.code = code;
    }

}

响应结果生产工厂 ResultFactory.java

package com.lick.vue_demo.result;

/**
 * @description 响应结果生成工厂类
 */
public class ResultFactory {
    public static AjaxResult buildSuccessResult(Object data) {
        return buildResult(ResultCode.SUCCESS, "成功", data);
    }

    public static AjaxResult buildFailResult(String message) {
        return buildResult(ResultCode.FAIL, message, null);
    }

    public static AjaxResult buildResult(ResultCode resultCode, String message, Object data) {
        return buildResult(resultCode.code, message, data);
    }

    public static AjaxResult buildResult(int resultCode, String message, Object data) {
        return new AjaxResult(resultCode, message, data);
    }
}

posted @ 2019-06-05 10:46  lick  阅读(2868)  评论(0编辑  收藏  举报