SpringBoot -登录注册
在前面的学习中我们学会简单创建一个springboot项目,那么现在就开始学习如何做注册登录了
工具springboot +maven +hibernate+mysql
1、项目结构
html 分为主页面index、注册register、登录login 和 登录成功success页面。
2、pom文件
4.0.0
com.online.shixun
HelloSpringboot
0.0.1-SNAPSHOT
jar
HelloSpringboot
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-devtools
true
true
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
org.springframework.boot
spring-boot-maven-plugin
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
public User findByUsernameAndPassword(String username, String password);
public List 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 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
hello登录成功!