一.建立数据库:
1.建立数据库:DataLab
2.在数据库中建表:leaveRequest
代码如下:
CREATE DATABASE dataLab;
CREATE TABLE leaveRequest(
id INT,
NAME VARCHAR(30),
telephone VARCHAR(30),
groupId VARCHAR(30),
leaveType VARCHAR(30),
startYear INT,
startMonth INT,
startDay INT,
startHour INT,
startMinute INT,
endYear INT,
endMonth INT,
endDay INT,
endHour INT,
endMinute INT,
leaveReason VARCHAR(30));
其中包含信息有:
二.springboot项目:
1.新建一个springboot项目:datalab
2.导入依赖: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.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xin</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>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-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml </groupId>
<artifactId>nekohtml </artifactId>
<version>1.9.22 </version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.新建banner.txt文件
为了迷信恳求佛祖保佑永无bug:
在resource目录下新建banner.txt文件:
// _ooOoo_ //
// o8888888o //
// 88" . "88 //
// (| ^_^ |) //
// O\ = /O //
// ____/`---'\____ //
// .' \\| |// `. //
// / \\||| : |||// \ //
// / _||||| -:- |||||- \ //
// | | \\\ - /// | | //
// | \_| ''\---/'' | | //
// \ .-\__ `-` ___/-. / //
// ___`. .' /--.--\ `. . ___ //
// ."" '< `.___\_<|>_/___.' >'"". //
// | | : `- \`.;`\ _ /`;.`/ - ` : | | //
// \ \ `-. \_ __\ /__ _/ .-` / / //
// ========`-.____`-.___\_____/___.-`____.-'======== //
// `=---=' //
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
// 佛祖保佑 永不宕机 永无BUG //
4.配置application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/dataLab?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=hyx824364
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false
spring.mvc.static-path-pattern=/**
spring.resources.static-locations=classpath:/templates/
5.建立目录结构:
6.新建Leave类:
package com.data.datalab.pojo;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Data
@Entity
@Table(name = "leaverequest")
public class Leave {
@Id
private Integer Id;
private String name;
private String telephone;
private String groupId;
private String leaveType;
private int startYear;
private int startMonth;
private int startDay;
private int startHour;
private int startMinute;
private int endYear;
private int endMonth;
private int endDay;
private int endHour;
private int endMinute;
private String leaveReason;
public Integer getId() {
return Id;
}
public void setId(Integer id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
public String getLeaveType() {
return leaveType;
}
public void setLeaveType(String leaveType) {
this.leaveType = leaveType;
}
public int getStartYear() {
return startYear;
}
public void setStartYear(int startYear) {
this.startYear = startYear;
}
public int getStartMonth() {
return startMonth;
}
public void setStartMonth(int startMonth) {
this.startMonth = startMonth;
}
public int getStartDay() {
return startDay;
}
public void setStartDay(int startDay) {
this.startDay = startDay;
}
public int getStartHour() {
return startHour;
}
public void setStartHour(int startHour) {
this.startHour = startHour;
}
public int getStartMinute() {
return startMinute;
}
public void setStartMinute(int startMinute) {
this.startMinute = startMinute;
}
public int getEndYear() {
return endYear;
}
public void setEndYear(int endYear) {
this.endYear = endYear;
}
public int getEndMonth() {
return endMonth;
}
public void setEndMonth(int endMonth) {
this.endMonth = endMonth;
}
public int getEndDay() {
return endDay;
}
public void setEndDay(int endDay) {
this.endDay = endDay;
}
public int getEndHour() {
return endHour;
}
public void setEndHour(int endHour) {
this.endHour = endHour;
}
public int getEndMinute() {
return endMinute;
}
public void setEndMinute(int endMinute) {
this.endMinute = endMinute;
}
public String getLeaveReason() {
return leaveReason;
}
public void setLeaveReason(String leaveReason) {
this.leaveReason = leaveReason;
}
}
@Data可以为类提供读写功能,从而不用写get、set方法
@Entity对实体注释。任何Hibernate映射对象都要有这个注释
@Table 声明此对象映射到数据库的数据表,通过它可以为实体指定表(talbe),目录(Catalog)和schema的名字。该注释不是必须的,如果没有则系统使用默认值(实体的短类名)。
@Id 声明此属性为主键。该属性值可以通过应该自身创建,但是Hibernate推荐通过Hibernate生成
7.LeaveController:
目前只写了增加和显示所有
package com.data.datalab.controller;
import com.data.datalab.pojo.Leave;
import com.data.datalab.service.LeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/leave")
public class LeaveController {
@Autowired
private LeaveService leaveService;
@GetMapping("/findAll")
public ModelAndView findAll(Model model){
List<Leave> findAll = leaveService.findAll();
model.addAttribute("findAll",findAll);
return new ModelAndView("findAll","find",model);
}
@PostMapping("/add") //post请求
public Leave save(Leave leave) {
return leaveService.save(leave);
}
}
8.LeaveDao:
package com.data.datalab.dao;
import com.data.datalab.pojo.Leave;
import org.springframework.data.jpa.repository.JpaRepository;
public interface LeaveDao extends JpaRepository<Leave,Integer> {
}
9.LeaveSurvice:
package com.data.datalab.service;
import com.data.datalab.pojo.Leave;
import java.util.List;
public interface LeaveService {
List<Leave> findAll();
Leave save(Leave leave);
}
10.IleaveSurvice
package com.data.datalab.service.Impl;
import com.data.datalab.dao.LeaveDao;
import com.data.datalab.pojo.Leave;
import com.data.datalab.service.LeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.List;
@Service
public class ILeaveService implements LeaveService {
@Autowired
private LeaveDao leaveDao;
@Override
public List<Leave> findAll() {
return leaveDao.findAll();
}
@Override
public Leave save(Leave leave) {
return leaveDao.save(leave);
}
}
10.简单页面:
写一个简单页面看看功能是否能使用:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table class="table table-striped">
<tr><th>序号</th>
<th>姓名</th>
<th>电话</th>
<th>组别</th>
<th>请假类型</th>
<th>开始年份</th>
<th>开始月份</th>
<th>开始日</th>
<th>开始时</th>
<th>开始分</th>
<th>结束年份</th>
<th>结束月份</th>
<th>结束日</th>
<th>结束时</th>
<th>结束分</th>
<th>请假原因</th>
</tr>
<tr th:each="f1:${findAll}">
<td th:text="${f1.id}"></td>
<td th:text="${f1.name}"></td>
<td th:text="${f1.groupId}"></td>
<td th:text="${f1.telephone}"></td>
<td th:text="${f1.telephone}"></td>
<td th:text="${f1.startYear}"></td>
<td th:text="${f1.startMonth}"></td>
<td th:text="${f1.startDay}"></td>
<td th:text="${f1.startHour}"></td>
<td th:text="${f1.startMinute}"></td>
<td th:text="${f1.endYear}"></td>
<td th:text="${f1.endMonth}"></td>
<td th:text="${f1.endDay}"></td>
<td th:text="${f1.endHour}"></td>
<td th:text="${f1.endMinute}"></td>
<td th:text="${f1.leaveReason}"></td>
</tr>
</table>
</body>
</html>