运用springboot和thyme leaf实现部门表和员工表的增删改查,并写出数据库

时间: 2023-06-16 20:04:55 浏览: 187
首先,我们需要创建一个数据库,包含两个表:部门表(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实现部门表和员工表的增删改查操作。
阅读全文

相关推荐

最新推荐

recommend-type

Thymeleaf显示base64字符串为图片.docx

在网页开发中,有时我们需要将Base64编码的字符串直接作为图片显示出来,这在Thymeleaf框架中可以通过特定的方式实现。Base64是一种数据编码方式,它将二进制数据转化为可打印的ASCII字符,常用于在HTTP协议中传输...
recommend-type

JSF如何监测和处理session超时

在这个阶段,如果用户请求的视图已经过期,JSF会抛出`ViewExpiredException`异常。这个异常表明由于会话超时,应用程序无法找到用户的原始视图状态。 处理`ViewExpiredException`的一种方法是创建一个自定义的`...
recommend-type

国外经典LOGO设计欣赏

本文将深入探讨并分析20个国外经典Logo设计,展示它们如何通过创新和巧妙的构思,实现品牌识别性和传播力。 1. Castle Print的Logo巧妙地运用了减色模型,直观展现其印刷业务,同时城堡形象强化了品牌名称。这种...
recommend-type

自动删除hal库spendsv、svc以及systick中断

自动删除hal库spendsv、svc以及systick中断
recommend-type

流量主小程序 多功能工具箱小程序源码-操作简单实用.zip

这是一款多功能工具箱小程序! 目前由N款小功能组合成 比如: 图片拼接 九宫格切图 透明昵称 文字表情 等等上面说的只是一部分
recommend-type

世界地图Shapefile文件解析与测试指南

标题中提到的“世界地图的shapefile文件”,涉及到两个关键概念:世界地图和shapefile文件格式。首先我们来解释这两个概念。 世界地图是一个地理信息系统(GIS)中常见的数据类型,通常包含了世界上所有或大部分国家、地区、自然地理要素的图形表达。世界地图可以以多种格式存在,比如栅格数据格式(如JPEG、PNG图片)和矢量数据格式(如shapefile、GeoJSON、KML等)。 shapefile文件是一种流行的矢量数据格式,由ESRI(美国环境系统研究所)开发。它主要用于地理信息系统(GIS)软件,用于存储地理空间数据及其属性信息。shapefile文件实际上是一个由多个文件组成的文件集,这些文件包括.shp、.shx、.dbf等文件扩展名,分别存储了图形数据、索引、属性数据等。这种格式广泛应用于地图制作、数据管理、空间分析以及地理研究。 描述提到,这个shapefile文件适合应用于解析shapefile程序的测试。这意味着该文件可以被用于测试或学习如何在程序中解析shapefile格式的数据。对于GIS开发人员或学习者来说,能够处理和解析shapefile文件是一项基本而重要的技能。它需要对文件格式有深入了解,以及如何在各种编程语言中读取和写入这些文件。 标签“世界地图 shapefile”为这个文件提供了两个关键词。世界地图指明了这个shapefile文件内容的地理范围,而shapefile指明了文件的数据格式。标签的作用通常是用于搜索引擎优化,帮助人们快速找到相关的内容或文件。 在压缩包子文件的文件名称列表中,我们看到“wold map”这个名称。这应该是“world map”的误拼。这提醒我们在处理文件时,确保文件名称的准确性和规范性,以避免造成混淆或搜索不便。 综合以上信息,知识点的详细介绍如下: 1. 世界地图的概念:世界地图是地理信息系统中一个用于表现全球或大范围区域地理信息的图形表现形式。它可以显示国界、城市、地形、水体等要素,并且可以包含多种比例尺。 2. shapefile文件格式:shapefile是一种矢量数据格式,非常适合用于存储和传输地理空间数据。它包含了多个相关联的文件,以.shp、.shx、.dbf等文件扩展名存储不同的数据内容。每种文件类型都扮演着关键角色: - .shp文件:存储图形数据,如点、线、多边形等地理要素的几何形状。 - .shx文件:存储图形数据的索引,便于程序快速定位数据。 - .dbf文件:存储属性数据,即与地理要素相关联的非图形数据,例如国名、人口等信息。 3. shapefile文件的应用:shapefile文件在GIS应用中非常普遍,可以用于地图制作、数据编辑、空间分析、地理数据的共享和交流等。由于其广泛的兼容性,shapefile格式被许多GIS软件所支持。 4. shapefile文件的处理:GIS开发人员通常需要在应用程序中处理shapefile数据。这包括读取shapefile数据、解析其内容,并将其用于地图渲染、空间查询、数据分析等。处理shapefile文件时,需要考虑文件格式的结构和编码方式,正确解析.shp、.shx和.dbf文件。 5. shapefile文件的测试:shapefile文件在开发GIS相关程序时,常被用作测试材料。开发者可以使用已知的shapefile文件,来验证程序对地理空间数据的解析和处理是否准确无误。测试过程可能包括读取测试、写入测试、空间分析测试等。 6. 文件命名的准确性:文件名称应该准确无误,以避免在文件存储、传输或检索过程中出现混淆。对于地理数据文件来说,正确的命名还对确保数据的准确性和可检索性至关重要。 以上知识点涵盖了世界地图shapefile文件的基础概念、技术细节、应用方式及处理和测试等重要方面,为理解和应用shapefile文件提供了全面的指导。
recommend-type

Python环境监控高可用构建:可靠性增强的策略

# 1. Python环境监控高可用构建概述 在构建Python环境监控系统时,确保系统的高可用性是至关重要的。监控系统不仅要在系统正常运行时提供实时的性能指标,而且在出现故障或性能瓶颈时,能够迅速响应并采取措施,避免业务中断。高可用监控系统的设计需要综合考虑监控范围、系统架构、工具选型等多个方面,以达到对资源消耗最小化、数据准确性和响应速度最优化的目
recommend-type

需要在matlab当中批量导入表格数据的指令

### 如何在 MATLAB 中批量导入表格数据 为了高效地处理多个表格文件,在 MATLAB 中可以利用脚本自动化这一过程。通过编写循环结构读取指定目录下的所有目标文件并将其内容存储在一个统一的数据结构中,能够显著提升效率。 对于 Excel 文件而言,`readtable` 函数支持直接从 .xls 或者 .xlsx 文件创建 table 类型变量[^2]。当面对大量相似格式的 Excel 表格时,可以通过遍历文件夹内的每一个文件来完成批量化操作: ```matlab % 定义要扫描的工作路径以及输出保存位置 inputPath = 'C:\path\to\your\excelFil
recommend-type

Sqlcipher 3.4.0版本发布,优化SQLite兼容性

从给定的文件信息中,我们可以提取到以下知识点: 【标题】: "sqlcipher-3.4.0" 知识点: 1. SQLCipher是一个开源的数据库加密扩展,它为SQLite数据库增加了透明的256位AES加密功能,使用SQLCipher加密的数据库可以在不需要改变原有SQL语句和应用程序逻辑的前提下,为存储在磁盘上的数据提供加密保护。 2. SQLCipher版本3.4.0表示这是一个特定的版本号。软件版本号通常由主版本号、次版本号和修订号组成,可能还包括额外的前缀或后缀来标识特定版本的状态(如alpha、beta或RC - Release Candidate)。在这个案例中,3.4.0仅仅是一个版本号,没有额外的信息标识版本状态。 3. 版本号通常随着软件的更新迭代而递增,不同的版本之间可能包含新的特性、改进、修复或性能提升,也可能是对已知漏洞的修复。了解具体的版本号有助于用户获取相应版本的特定功能或修复。 【描述】: "sqlcipher.h是sqlite3.h的修正,避免与系统预安装sqlite冲突" 知识点: 1. sqlcipher.h是SQLCipher项目中定义特定加密功能和配置的头文件。它基于SQLite的头文件sqlite3.h进行了定制,以便在SQLCipher中提供数据库加密功能。 2. 通过“修正”原生SQLite的头文件,SQLCipher允许用户在相同的编程环境或系统中同时使用SQLite和SQLCipher,而不会引起冲突。这是因为两者共享大量的代码基础,但SQLCipher扩展了SQLite的功能,加入了加密支持。 3. 系统预安装的SQLite可能与需要特定SQLCipher加密功能的应用程序存在库文件或API接口上的冲突。通过使用修正后的sqlcipher.h文件,开发者可以在不改动现有SQLite数据库架构的基础上,将应用程序升级或迁移到使用SQLCipher。 4. 在使用SQLCipher时,开发者需要明确区分它们的头文件和库文件,避免链接到错误的库版本,这可能会导致运行时错误或安全问题。 【标签】: "sqlcipher" 知识点: 1. 标签“sqlcipher”直接指明了这个文件与SQLCipher项目有关,说明了文件内容属于SQLCipher的范畴。 2. 一个标签可以用于过滤、分类或搜索相关的文件、代码库或资源。在这个上下文中,标签可能用于帮助快速定位或检索与SQLCipher相关的文件或库。 【压缩包子文件的文件名称列表】: sqlcipher-3.4.0 知识点: 1. 由于给出的文件名称列表只有一个条目 "sqlcipher-3.4.0",它很可能指的是压缩包文件名。这表明用户可能下载了一个压缩文件,解压后的内容应该与SQLCipher 3.4.0版本相关。 2. 压缩文件通常用于减少文件大小或方便文件传输,尤其是在网络带宽有限或需要打包多个文件时。SQLCipher的压缩包可能包含头文件、库文件、示例代码、文档、构建脚本等。 3. 当用户需要安装或更新SQLCipher到特定版本时,他们通常会下载对应的压缩包文件,并解压到指定目录,然后根据提供的安装指南或文档进行编译和安装。 4. 文件名中的版本号有助于确认下载的SQLCipher版本,确保下载的压缩包包含了期望的特性和功能。 通过上述详细解析,我们可以了解到关于SQLCipher项目版本3.4.0的相关知识,以及如何处理和使用与之相关的文件。
recommend-type

Python环境监控性能监控与调优:专家级技巧全集

# 1. Python环境性能监控概述 在当今这个数据驱动的时代,随着应用程序变得越来越复杂和高性能化,对系统性能的监控和优化变得至关重要。Python作为一种广泛应用的编程语言,其环境性能监控不仅能够帮助我们了解程序运行状态,还能及时发现潜在的性能瓶颈,预防系统故障。本章将概述Python环境性能监控的重要性,提供一个整体框架,以及为后续章节中深入探讨各个监控技术打