Springboot+MySQL登录注册后端逻辑模板

本文介绍了使用SpringBoot快速开发一个包含登录和注册功能的后端应用,展示了实体类、Mapper接口、Service以及前端表单交互的代码示例。
摘要由CSDN通过智能技术生成

为了节省后端开发时间,自己写了一个后端模板,开箱即用,包括登录注册两个功能

这个是启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class LoginRegisterApplication {

    public static void main(String[] args) {
        SpringApplication.run(LoginRegisterApplication.class, args);
    }

}

这个是控制类

import com.example.login_register.entity.User;
import com.example.login_register.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;


@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/")
    public String loginPage() {
        return "Login&Register";
    }

    @PostMapping("login")
    public String login(User user, HttpSession session, Model model) {
        String form_password = user.getPassword();
        if (userService.userNameIsExist(user.getUsername()) != null) {
            String password = userService.findUserPasswordByUserName(user.getUsername());
            if (password.equals(form_password)) {
                return "redirect:/index.html";
            }
        } else {
            model.addAttribute("msg", "账号或者密码有误");
            return "Login&Register";
        }
        return "Login&Register";
    }

    @PostMapping(value = "register")
    public String addUser(User user) {
        System.out.println(user.getUsername() + user.getPassword());
        userService.addUser(user.getUsername(), user.getPassword());
        return "redirect:/index.html";
    }

    @GetMapping("index.html")
    public String successPage(User user,HttpSession session, Model model) {
        return "index";
    }

这个是实体类

import lombok.Data;

@Data
public class User {
    private int id;
    private String username;
    private String password;

}

这个是Mapper类

import com.example.login_register.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserMapper {
    // 登录方法,根据用户名查询密码
    @Select("select password from login_register.user where username = #{username}")
    String findUserPasswordByUserName(String username);

    @Select("select * from login_register.user where username = #{username}")
    String userNameIsExist(String username);

    @Insert("insert into login_register.user(username, password) values (#{username}, #{password})")
    void adduser(String username, String password);

    @Select("select * from login_register.user")
    List<User> getAll();

}

这个是Service类

import com.example.login_register.entity.User;
import com.example.login_register.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;

    public String findUserPasswordByUserName(String username) {
        return userMapper.findUserPasswordByUserName(username);
    }

    public String userNameIsExist(String username) {
        return userMapper.userNameIsExist(username);
    }

    public void addUser(String username, String password) {
        userMapper.adduser(username, password);
    }

    public List<User> getAll() {
        List<User> users = userMapper.getAll();
        return users;
    }
}

此外,还有前端静态网页如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>
<div>
    <div>登 录 & 注 册</div>
    <form th:action="@{/login}" method="post">
        <div>
            <input type="text" placeholder="请输入用户名" name="username">
        </div>
        <div>
            <input type="password" placeholder="请输入密码" name="password">
        </div>
        <div class="btn">
            <input type="submit" value="登录" height="50px" style="background-color:#61dafb;color: aliceblue;display: block;"/>
            <span th:text="${msg}"></span>
        </div>
    </form>

    <form th:action="@{/register}" method="post">
        <div>
            <input type="text" placeholder="请输入用户名" name="username">
        </div>
        <div>
            <input type="password" placeholder="请输入密码" name="password">
        </div>
        <input type="submit" value="注册">
    </form>
</div>
</body>
</html>

登录成功后的主页

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>测试</title>
</head>
<body>
<h1>欢迎来到首页</h1>

</body>

<script>

</script>
</html>

另外,如果大家感觉复制过于麻烦,我将此项目上传到了GitHub,这里是GitHub的链接:

SimplesonSong/Login-Register (github.com)icon-default.png?t=N7T8https://github.com/SimplesonSong/Login-Register大家可以直接去GitHub上下载整个项目源码,如果这篇文章对你有帮助的话,麻烦大家点个赞和star

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值