版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/slwhy/article/details/89439709
任务要求
- 实现一个简易的学生管理系统
- 允许root,老师,学生三种不同用户登录
- root用户可以添加用户
- 对用户登录进行验证
项目实现
- 新建一个项目,具体情况见上一篇博客springboot实战一:helloworld
要注意在新建的时候,加入sql的相关依赖
- 定义User实体类,具体代码如下
package com.springboot.stumanage.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
////@Entity 是一个必选的注解,声明这个类对应了一个数据库表
////@Table(name = "user") 是一个可选的注解。
//// 声明了数据库实体对应的表信息。包括表名称、索引信息等。
//// 这里声明这个实体类对应的表名是 user。如果没有指定,则表名和实体的名称保持一致。
@Entity
//@Table(name = "user")
public class User {
@Id
@GeneratedValue
private Long id;
public Integer getTab() {
return tab;
}
public void setTab(Integer tab) {
this.tab = tab;
}
//标记,0为root用户,1为老师,2为学生
private Integer tab;
@Column(length = 32)
private String username;
@Column(length = 64)
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;
}
}
- 定义 UserRepository 接口,用来实现查询等操作
package com.springboot.stumanage.entity;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Transactional
public interface UserRepository extends CrudRepository<User, Long> {
public List<User> findByusername(String name);
}
- 编写yml配置文件,连接数据库
spring:
datasource:
# driver-class-name: com.mysql.jdbc.Driver
# stu是我为项目新建的数据库
url: jdbc:mysql://localhost:3306/stu?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
username: root
password: root
jpa:
hibernate:
# ddl-auto的属性值
# create 启动时删数据库中的表,然后创建,退出时不删除数据表
# create-drop 启动时删数据库中的表,然后创建,退出时删除数据表 如果表不存在报错
# update 如果启动时表格式不一致则更新表,原有数据保留
# validate 项目启动表结构进行校验 如果不一致则报错
ddl-auto: update
#有个很迷的问题,就是本人之前没有加后面这句话,一直无法生成数据表
use-new-id-generator-mappings: false
show-sql: true
-
运行程序,生成相关数据表,并向user表内插入root用户数据
这里稍微提一下,因为本人安装的是mysql8.x,利用idea一直连接不上,具体情况以及解决方法见,The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or represents more than one time zone -
编写相应的controller
package com.springboot.stumanage.controller;
import com.springboot.stumanage.entity.User;
import com.springboot.stumanage.entity.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
@Controller
public class LoginController {
//加@Autowired属性就可以实现接口自动的实例化了
@Autowired
private UserRepository userRepository;
@RequestMapping(value = {"/index", "/"})
public String index() {
return "index";
}
@RequestMapping(value = "login", method = RequestMethod.POST)
public String signin(User user) {
String username = user.getUsername();
List<User> userList = userRepository.findByusername(username);
if (userList.size() != 0) {
User user1 = userList.get(0);
switch (user1.getTab()) {
case 0:
if (user1.getPassword().equals(user.getPassword())) {
return "root";
} else {
break;
}
case 1:
if (user1.getPassword().equals(user.getPassword())) {
return "teacher";
} else {
break;
}
case 2:
if (user1.getPassword().equals(user.getPassword())) {
return "student";
} else {
break;
}
default:
break;
}
}
return "index";
}
}
package com.springboot.stumanage.controller;
import com.springboot.stumanage.entity.User;
import com.springboot.stumanage.entity.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RootController {
@Autowired
private UserRepository userRepository;
@RequestMapping("/root/addUser")
public String addUser(User user){
userRepository.save(user);
return "success";
}
}
- 在pom文件中添加thymeleaf模板的相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- 修改yml文件指定html文件的路径
spring:
thymeleaf:
prefix: classpath:/templates/
datasource:
# driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/stu?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
username: root
password: root
jpa:
hibernate:
ddl-auto: update
use-new-id-generator-mappings: false
show-sql: true
- 编写相应的html文件
index.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div align="center">
<h1>学生管理系统登录</h1>
<form class="form-signin" action="./login" method="post">
<input type="text" name="username" placeholder="username"/>
<br/>
<!-- <input type="password" name="password",placeholder="password"/>-->
<input type="password" name="password" placeholder="password"/>
<br/>
<input type="submit" value="Login" >
<input type="reset" value="reset">
</form>
</div>
</body>
</html>
root.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>welcome to you,root</h1>
<div class="change">
<form class="add-user" action="./root/addUser" method="post">
<input type="text" name="username" placeholder="username"/>
<input type="password" name="password" placeholder="password"/>
<input type="text" name="tab" placeholder="角色"/>
<input type="submit" value="添加"/>
</form>
</div>
</body>
</html>
因为是简易的一个demo,其他的html文件我就写了些问候语句,就不放上来了
-
运行程序,在浏览器的地址栏内,输入 http://localhost:8080/login 查看效果
你可以利用你之前插入的root的用户密码登录,进入root用户后,可以插入其他user数据 -
完整的目录结构
总结
通过编写这个简单的demo,我学习到了
- yml类型的配置文件的编写
- 在pom文件中导入相关依赖
- 利用jpa根据实体类生成mysql数据表
- 利用jpa对mysql进行查询,插入数据
- 如何响应html文件
- 如何读取表单提交的数据
后期我会将代码整理打包发到github上,如果有什么问题欢迎大家留言讨论