SpringBoot学习之实现登录注册功能

1、系统目录结构

2、创建数据库

数据库名称:student,数据表:user,创建命令如下:

//创建数据库
create database student;

//选择创建好的数据库,建立user表
use student;

create table user(
    id int primary key auto_increment,
    username varchar(32) unique not null,
    password varchar(32) not null
);


//想创建好的数据库中添加数据
insert into user values(1,"xioming","000");

//查看自己添加的数据
select * from user;

 3、创建前端页面

3.1 登录页面:login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>login</title>
</head>
<body>
    <form method="post" action="/user/login">
        用户名:<input name="username" type="text"/>
        密码:<input name="password" type="password"/>
        <input type="submit" value="登录验证">
        <input type="button" onclick="javascript:window.location.href='regist.html'" value="去注册页面">
    </form>
</body>
</html>

3.2 注册页面:regist.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>regist</title>
</head>
<body>
    <form method="post" action="/user/regist">
        用户名:<input name="username" type="text"/>
        密码:<input name="password" type="password"/>
        <input type="submit" value="注册校验">
        <input type="button" onclick="javascript:window.location.href='login.html'" value="返回注册页面">

    </form>
</body>
</html>

 4、Java代码

4.1 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 https://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.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>studentsys</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>studentsys</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.73</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

4.2 数据配置信息:application.properties

#配置数据库连接信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root

#数据库连接池

#mapper映射
mybatis.type-aliases-package=com.example.studentsys.entiy
mybatis.mapper-locations=classpath:mapper/*.xml

 4.3 实体类:User.java

package com.example.studentsys.entiy;

public class User {
    private Integer id;
    private String username;
    private String password;

    public void setId(Integer id){
        this.id = id;
    }
    public Integer getId(){
        return id;
    }
    public void setUsername(String username){
        this.username = username;
    }
    public String getUsername(){
        return username;
    }
    public void setPassword(String password){
        this.password = password;
    }
    public String getPassword(){
        return password;
    }
}

4.4  mapper接口:UserMapper.java

package com.example.studentsys.mapper;

import com.example.studentsys.entiy.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

//@Repository将类标识为bean
@Repository
@Mapper
public interface UserMapper {
    List<User> findAll();
    User findByName(String name);
    String findPswByName(String UserName);
    void save(User user);
}

4.5  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">
<!--xmlns="http://mybatis.org/schema/mybatis-mapper"-->
<mapper namespace="com.example.studentsys.mapper.UserMapper">
    <select id="findAll" resultType="User">
        select * from User
    </select>
    <select id="findByName" resultType="User">
        select * from User where username=#{username}
    </select>
    <select id="findPswByName" resultType="String">
        select password from user where username = #{username}
    </select>
    <insert id="save">
        insert into user(username,password) value (#{username},#{password})
    </insert>
</mapper>

4.6 service类:UserService.java

package com.example.studentsys.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.studentsys.entiy.User;
import com.example.studentsys.mapper.UserMapper;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class UserService {

    //该注解可以对类成员变量、方法以及构造函数进行标注,完成自动装配工作
    @Autowired
    UserMapper userMapper;

    public String login(User user){
        //登录逻辑函数
        try{
            User userExistN = userMapper.findByName(user.getUsername());
            if(userExistN!=null){
                String userExistP = userMapper.findPswByName(user.getUsername());
                if(userExistP.equals(user.getPassword())){
                    return user.getUsername()+"登录成功,欢迎您!";
                }else {
                    return "登录失败,密码错误!";
                }
            }else {
                return "登录失败,用户不存在!";
            }
        }catch (Exception e){
            e.printStackTrace();
            return e.getMessage();
        }
    }

    public String regist(User user){
        //注册逻辑函数
        try {
            User userExist = userMapper.findByName(user.getUsername());
            if (user.getUsername().equals("")){
                return "用户名不能为空";
            }else if (user.getPassword().equals("")){
                return "密码不能为空";
            }else if (userExist!=null){
                return "账户已经存在";
            }else{
                userMapper.save(user);
                return "注册成功";
            }
        }catch (Exception e){
            e.printStackTrace();
            return e.getMessage();
        }

    }

    public List<User> findAll(){
        List<User> list = userMapper.findAll();
        return list;
    }

}

4.7  控制类:UserController.java

package com.example.studentsys.controller;

import com.example.studentsys.entiy.User;
import com.example.studentsys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
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;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("/login")
    public String login(User user){
        return userService.login(user);
    }

    @PostMapping("/regist")
    public String regist(User user){
        return userService.regist(user);
    }

    /**
     * 解决查询数据库中文出现乱码问题
     * @return
     */
    @RequestMapping(value = "/alluser", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public List<User> findAll(){
        return userService.findAll();
    }
}

5、运行项目

http://localhost:8080/user/login.html 进入到登录页面

 http://localhost:8080/user/regist.html 进入到注册页面

 http://localhost:8080/user/alluser 查看当前所有用户信息

6、总结

MySQL常见管理命令

@RestController注解

业务逻辑:

Mapper负责连接数据并进行相关的CRUD操作

Service借助Mapper来完成登录注册的业务逻辑

Controller接待浏览器访问,借助Service提供登录注册服务

猜你喜欢

转载自blog.csdn.net/li_w_ch/article/details/108937751