技术栈:spring boot + spring data jpa + thymeleaf + spring mvc + mysql
新建项目
可以使用官网的starter或者IDEA新建项目
Flie->New Project->Spring Initializr.
(出现过连接超时问题,等会重试,设置代理方法解决?)
spring连接失败处理方法.png
新建项目New Project 名字任意,直接下一步,引入相关依赖见下图:
depend.png
项目结构,新建项目之后新建如下所示的目录结构和java、html文件
项目结构.png
pom.xml中的依赖
首先在pom.xml中引入如下依赖(之前在新建项目的时候引入过相关依赖可以忽略):
<?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.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>login_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>login_demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</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-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
在这里进行相关配置,端口号,数据库等配置
server.port=8080
spring.application.admin.enabled=false
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
#服务器端
#数据库基本配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/user?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#用来在控制台输出JPA自动生成的sql语句。
spring.jpa.show-sql=true
spring.jpa.database=mysql
spring.jpa.hibernate.ddl-auto=update
LoginController.java
登录功能的控制类
package com.example.login_demo.controller;
import com.example.login_demo.entity.User;
import com.example.login_demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
/**
* @auther Devin
* 2020/6/9 13:35
* @description 登录
*/
@RestController
@RequestMapping("/")
public class LoginController {
@Autowired
UserRepository userRepository;
@GetMapping("/login")
public ModelAndView login(ModelAndView modelAndView) {
modelAndView.setViewName("login");
return modelAndView;
}
@PostMapping("/toLogin")
public ModelAndView login(HttpServletRequest request, ModelAndView modelAndView) {
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
User user = userRepository.findUserByUsernameAndAndPwd(username, pwd);
if (username != "" && pwd != "") {
if (user!= null) {
modelAndView.setViewName("hello");
return modelAndView;
} else {
modelAndView.setViewName("login");
modelAndView.addObject("msg","账号密码错误");
return modelAndView;
}
} else {
modelAndView.setViewName("error");
return modelAndView;
}
}
}
User.java
用户实体类
package com.example.login_demo.entity;
import javax.persistence.*;
/**
* @auther Devin
* 2020/6/9 13:29
* @description 用户实体类
*/
@Entity
@Table(name="tb_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "username")
private String username;
@Column(name = "pwd")
private String pwd;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
UserRepository.java
package com.example.login_demo.repository;
import com.example.login_demo.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
/**
* @auther Devin
* 2020/6/9 13:48
* @description
*/
@Repository
public interface UserRepository extends JpaRepository<User,Integer> {
User findUserByUsernameAndAndPwd(String username ,String pwd);
}
login.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>登录</title>
</head>
<body>
<form th:action="@{/toLogin}" method="post">
<p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg) }"></p>
<div>
<input id="username" name="username" type="text" placeholder="用户名" />
</div>
<br />
<div>
<input id="pwd" name="pwd" type="password" placeholder="密码" />
</div>
<br />
<button type="submit" >登 录</button>
</form>
</body>
</html>
error.html 和 hello.html
两个文件的<title>和<h1>标签的内容不同
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录成功</title>
</head>
<body>
<h1>登录成功</h1>
</body>
</html>
完成!A