为了节省后端开发时间,自己写了一个后端模板,开箱即用,包括登录注册两个功能
这个是启动类
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)https://github.com/SimplesonSong/Login-Register大家可以直接去GitHub上下载整个项目源码,如果这篇文章对你有帮助的话,麻烦大家点个赞和star