运用springboot和thyme leaf实现部门表和员工表的增删改查,并写出数据库
时间: 2023-06-16 22:04:55 浏览: 179
首先,我们需要创建一个数据库,包含两个表:部门表(department)和员工表(employee)。
建表语句如下:
```sql
CREATE DATABASE IF NOT EXISTS `company`;
USE `company`;
CREATE TABLE IF NOT EXISTS `department` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE IF NOT EXISTS `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`gender` char(1) NOT NULL,
`birthday` date NOT NULL,
`department_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `department_id` (`department_id`),
CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`department_id`) REFERENCES `department` (`id`)
);
```
然后,我们可以使用Spring Boot和Thymeleaf来实现部门表和员工表的增删改查操作。
在Spring Boot中,我们可以使用JPA来操作数据库,同时使用Thymeleaf来实现页面的渲染。
首先,我们需要在pom.xml文件中添加以下依赖:
```xml
<dependencies>
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- MySQL JDBC Driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
```
然后,我们需要在application.properties文件中配置数据库连接信息:
```
spring.datasource.url=jdbc:mysql://localhost:3306/company
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.thymeleaf.cache=false
```
接下来,我们需要创建实体类Department和Employee,对应数据库中的department和employee表:
```java
@Entity
@Table(name = "department")
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
// getter and setter
}
@Entity
@Table(name = "employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "gender")
private String gender;
@Column(name = "birthday")
private Date birthday;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "department_id")
private Department department;
// getter and setter
}
```
然后,我们需要创建DepartmentRepository和EmployeeRepository接口,继承JpaRepository接口,用于操作数据库:
```java
public interface DepartmentRepository extends JpaRepository<Department, Long> {
}
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}
```
接下来,我们需要创建DepartmentController和EmployeeController类,用于处理请求和响应:
```java
@Controller
@RequestMapping("/department")
public class DepartmentController {
@Autowired
private DepartmentRepository departmentRepository;
@GetMapping("/list")
public String list(Model model) {
List<Department> departments = departmentRepository.findAll();
model.addAttribute("departments", departments);
return "department/list";
}
@GetMapping("/add")
public String add(Model model) {
Department department = new Department();
model.addAttribute("department", department);
return "department/form";
}
@PostMapping("/save")
public String save(@ModelAttribute("department") Department department) {
departmentRepository.save(department);
return "redirect:/department/list";
}
@GetMapping("/edit/{id}")
public String edit(@PathVariable("id") Long id, Model model) {
Department department = departmentRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Invalid department Id:" + id));
model.addAttribute("department", department);
return "department/form";
}
@GetMapping("/delete/{id}")
public String delete(@PathVariable("id") Long id) {
Department department = departmentRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Invalid department Id:" + id));
departmentRepository.delete(department);
return "redirect:/department/list";
}
}
@Controller
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;
@Autowired
private DepartmentRepository departmentRepository;
@GetMapping("/list")
public String list(Model model) {
List<Employee> employees = employeeRepository.findAll();
model.addAttribute("employees", employees);
return "employee/list";
}
@GetMapping("/add")
public String add(Model model) {
Employee employee = new Employee();
List<Department> departments = departmentRepository.findAll();
model.addAttribute("employee", employee);
model.addAttribute("departments", departments);
return "employee/form";
}
@PostMapping("/save")
public String save(@ModelAttribute("employee") Employee employee) {
employeeRepository.save(employee);
return "redirect:/employee/list";
}
@GetMapping("/edit/{id}")
public String edit(@PathVariable("id") Long id, Model model) {
Employee employee = employeeRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Invalid employee Id:" + id));
List<Department> departments = departmentRepository.findAll();
model.addAttribute("employee", employee);
model.addAttribute("departments", departments);
return "employee/form";
}
@GetMapping("/delete/{id}")
public String delete(@PathVariable("id") Long id) {
Employee employee = employeeRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Invalid employee Id:" + id));
employeeRepository.delete(employee);
return "redirect:/employee/list";
}
}
```
最后,我们需要创建部门表和员工表的页面,使用Thymeleaf进行渲染:
```html
<!-- department/list.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Department List</title>
</head>
<body>
<h1>Department List</h1>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Action</th>
</tr>
<tr th:each="department : ${departments}">
<td th:text="${department.id}"></td>
<td th:text="${department.name}"></td>
<td>
<a th:href="@{/department/edit/{id}(id=${department.id})}">Edit</a>
<a th:href="@{/department/delete/{id}(id=${department.id})}">Delete</a>
</td>
</tr>
</table>
<a th:href="@{/department/add}">Add Department</a>
</body>
</html>
<!-- department/form.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Add Department</title>
</head>
<body>
<h1>Add Department</h1>
<form action="/department/save" method="post">
<input type="hidden" th:field="*{id}">
<label for="name">Name:</label>
<input type="text" id="name" th:field="*{name}">
<input type="submit" value="Save">
</form>
<a th:href="@{/department/list}">Back to List</a>
</body>
</html>
<!-- employee/list.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Employee List</title>
</head>
<body>
<h1>Employee List</h1>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Gender</th>
<th>Birthday</th>
<th>Department</th>
<th>Action</th>
</tr>
<tr th:each="employee : ${employees}">
<td th:text="${employee.id}"></td>
<td th:text="${employee.name}"></td>
<td th:text="${employee.gender}"></td>
<td th:text="${#dates.format(employee.birthday, 'yyyy-MM-dd')}"></td>
<td th:text="${employee.department.name}"></td>
<td>
<a th:href="@{/employee/edit/{id}(id=${employee.id})}">Edit</a>
<a th:href="@{/employee/delete/{id}(id=${employee.id})}">Delete</a>
</td>
</tr>
</table>
<a th:href="@{/employee/add}">Add Employee</a>
</body>
</html>
<!-- employee/form.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Add Employee</title>
</head>
<body>
<h1>Add Employee</h1>
<form action="/employee/save" method="post">
<input type="hidden" th:field="*{id}">
<label for="name">Name:</label>
<input type="text" id="name" th:field="*{name}">
<br>
<label for="gender">Gender:</label>
<input type="text" id="gender" th:field="*{gender}">
<br>
<label for="birthday">Birthday:</label>
<input type="date" id="birthday" th:field="*{birthday}">
<br>
<label for="department">Department:</label>
<select id="department" th:field="*{department}">
<option th:each="department : ${departments}" th:value="${department}" th:text="${department.name}" th:selected="${employee.department == department}"></option>
</select>
<br>
<input type="submit" value="Save">
</form>
<a th:href="@{/employee/list}">Back to List</a>
</body>
</html>
```
至此,我们就完成了使用Spring Boot和Thymeleaf实现部门表和员工表的增删改查操作。
阅读全文