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;
}
}
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);
}
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";
}
}
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
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
index.html文件
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
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"/>
<br/>
<input type="submit" value="Login" >
<input type="reset" value="reset">
form>
div>
body>
html>
root.html文件
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1>welcome to you,rooth1>
<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文件我就写了些问候语句,就不放上来了
通过编写这个简单的demo,我学习到了
看到很多人说,这里备注一下,login界面是index.html
后期我会将代码整理打包发到github上,如果有什么问题欢迎大家留言讨论
github : https://github.com/Slwhy/springboot