SpringBoot -登录注册
在前面的学习中我们学会简单创建一个springboot项目,那么现在就开始学习如何做注册登录了
工具springboot +maven +hibernate+mysql
参考了https://blog.csdn.net/coder_py/article/details/78125589
1、项目结构
- html 分为主页面index、注册register、登录login 和 登录成功success页面。
2、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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.online.shixun</groupId>
<artifactId>HelloSpringboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>HelloSpringboot</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 数据库包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、编写实体User类
package com.online.shixun.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity(name = "t_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
4、DAO层
package com.online.shixun.dao;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.online.shixun.entity.User;
@Repository
public interface UserDao extends JpaRepository<User, Long> {
public User findByUsernameAndPassword(String username, String password);
public List<User> findByUsername(String username);
}
5、Service层
package com.online.shixun.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.online.shixun.dao.UserDao;
import com.online.shixun.entity.User;
@Service
public class UserService {
@Autowired
UserDao userDao;
public User FindNameAndPsw(String username, String password) {
return userDao.findByUsernameAndPassword(username, password);
}
public void save(User user1) {
userDao.save(user1);
}
public List<User> findByName(String username) {
return userDao.findByUsername(username);
}
}
6、Controller类
package com.online.shixun.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.online.shixun.entity.User;
import com.online.shixun.service.UserService;
@Controller
@RequestMapping("/front/*")
public class UserController {
@Autowired
UserService userService;
@RequestMapping("/")
public String index() {
return "index";
}
/**
* 去注册页面
*
* @return
*/
@RequestMapping("/register")
public String register() {
return "register";
}
/**
* 执行注册 成功后登录页面 否则调回注册页面
*
* @param request
* @param user
* @return
*/
@RequestMapping("/doregister")
public String register(HttpServletRequest request, User user) {
String username = request.getParameter("username");
String password = request.getParameter("password");
String password2 = request.getParameter("password2");
if (password.equals(password2)) {
if (registerUser(username) == true) {
User user1 = new User();
user1.setUsername(username);
user1.setPassword(password);
userService.save(user1);
return "login";
} else {
return "register";
}
} else {
return "register";
}
}
public Boolean registerUser(String username) {
Boolean a = true;
if (userService.findByName(username).isEmpty()) {
return a;
} else {
return false;
}
}
/**
* 去登录页面
*
* @return
*/
@RequestMapping("/login")
public String login() {
return "login";
}
/**
* 执行登录
*
* @param request
* @return
*/
@RequestMapping("/dologin")
public String login(HttpServletRequest request) {
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = userService.FindNameAndPsw(username, password);
String str = "";
if (user != null) {
str = "success";
} else {
str = "login";
}
return str;
}
}
7、application.properties配置文件
server.port=8084
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.jpa.database = MYSQL
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.servlet.content-type=text/html
8、静态页面
index.html
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>hello</title>
</head>
<body>
<div class="main_info">
<a href="/front/login">点击这里去登录</a><a href="/front/register">点击这里去注册</a>
</div>
</body>
</html>
login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form action="/front/dologin" accept-charset="utf-8" method="post">
<div>
<label>用户名</label><input type="text" class="text" id="username"
placeholder="用户名" name="username" required tabindex="1">
</div>
<div>
<label>密 码</label><input type="password"
class="text" id="password" placeholder="密码" required name="password"
tabindex="2"><br>
</div>
<br>
<div>
<span>没有账号?去<a href="/front/register">注册</a></span>
<button type="reset" value="重置" id="reset">重置</button>
<button type="submit" value="登录" id="login">登录</button>
</div>
</form>
</body>
</html>
register.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form accept-charset="utf-8" action="/front/doregister" method="post">
<div>
<label>用户名称</label><input type="text" id="username" placeholder=" 用户名"
name="username" required><br> <br>
</div>
<div>
<label>登录密码</label><input type="password" id="password"
placeholder=" 密码" required name="password"><br> <br>
</div>
<div>
<label>确认密码</label><input type="password" id="password2"
placeholder=" 确认密码" required name="password2"><br>
<br>
</div>
<div>
<span>已有账号去</span><a href="/front/login">登录</a> <input type="submit"
value="同意协议并注册" />
</div>
</form>
</body>
</html>
success.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>success</title>
</head>
<body>
<p>登录成功!</p>
</body>
</html>