springboot+layui实现网页上增删改查

时间: 2023-12-24 22:06:19 浏览: 37
Spring Boot是一个非常流行的Java框架,可以轻松地创建Web应用程序。Layui是一个流行的前端UI框架,可以帮助我们快速地创建漂亮的Web页面。在这里,我将向你展示如何使用Spring Boot和Layui来实现基本的增删改查功能。 首先,我们需要创建一个Spring Boot项目。你可以使用Spring Initializr来创建一个新项目,也可以使用Eclipse或IntelliJ IDEA等IDE来创建一个新项目。 接下来,我们需要添加一些依赖项。在这个例子中,我们将使用Spring Data JPA来访问数据库。在pom.xml文件中添加以下依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> ``` 我们还需要添加Spring Boot和Layui的依赖项。在pom.xml文件中添加以下依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>layui</artifactId> <version>2.5.4</version> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.1.0</version> </dependency> ``` 接下来,我们需要配置数据库连接。在application.properties文件中添加以下代码: ``` spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.h2.console.enabled=true ``` 在这里,我们使用H2内存数据库来简化示例。在实际应用程序中,你可能需要使用其他数据库,例如MySQL或PostgreSQL。 现在,我们将创建一个实体类来表示我们的数据表。在这个例子中,我们将创建一个简单的数据表,其中包含id,name和age字段。创建一个名为Person的类,并在其中添加以下代码: ```java @Entity @Table(name = "person") public class Person { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; private int age; // getters and setters } ``` 接下来,我们将创建一个Spring Data JPA存储库来访问数据库。创建一个名为PersonRepository的接口,并在其中添加以下代码: ```java @Repository public interface PersonRepository extends JpaRepository<Person, Long> { } ``` 现在我们已经设置好了数据访问层,我们将创建一个控制器来处理HTTP请求。创建一个名为PersonController的类,并在其中添加以下代码: ```java @Controller public class PersonController { @Autowired private PersonRepository personRepository; @GetMapping("/") public String index(Model model) { List<Person> persons = personRepository.findAll(); model.addAttribute("persons", persons); return "index"; } @GetMapping("/add") public String addForm(Model model) { return "add"; } @PostMapping("/add") public String addSubmit(@ModelAttribute Person person) { personRepository.save(person); return "redirect:/"; } @GetMapping("/edit/{id}") public String editForm(@PathVariable Long id, Model model) { Person person = personRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid person id:" + id)); model.addAttribute("person", person); return "edit"; } @PostMapping("/edit/{id}") public String editSubmit(@PathVariable Long id, @ModelAttribute Person person, BindingResult bindingResult) { if (bindingResult.hasErrors()) { person.setId(id); return "edit"; } personRepository.save(person); return "redirect:/"; } @GetMapping("/delete/{id}") public String delete(@PathVariable Long id) { personRepository.deleteById(id); return "redirect:/"; } } ``` 在这里,我们定义了五个方法来处理HTTP请求: - `index()` - 处理根路径请求,列出所有人员记录。 - `addForm()` - 处理添加人员记录请求,返回一个包含表单的页面。 - `addSubmit()` - 处理添加人员记录请求,将表单提交的数据保存到数据库中。 - `editForm()` - 处理编辑人员记录请求,返回一个包含表单的页面。 - `editSubmit()` - 处理编辑人员记录请求,将表单提交的数据更新到数据库中。 - `delete()` - 处理删除人员记录请求,从数据库中删除指定的记录。 现在我们需要创建HTML模板来呈现数据。创建一个名为index.html的文件,并在其中添加以下代码: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Person List</title> <link rel="stylesheet" type="text/css" href="/webjars/layui/2.5.4/css/layui.css"> <script type="text/javascript" src="/webjars/jquery/3.1.0/jquery.min.js"></script> <script type="text/javascript" src="/webjars/layui/2.5.4/layui.js"></script> </head> <body> <div class="layui-container"> <div class="layui-row"> <div class="layui-col-md12"> <h2>Person List</h2> </div> </div> <div class="layui-row"> <div class="layui-col-md12"> <table class="layui-table"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Age</th> <th>Actions</th> </tr> </thead> <tbody> <tr th:each="person : ${persons}"> <td th:text="${person.id}"></td> <td th:text="${person.name}"></td> <td th:text="${person.age}"></td> <td> <button class="layui-btn layui-btn-xs" onclick="location.href='/edit/'+${person.id}">Edit</button> <button class="layui-btn layui-btn-danger layui-btn-xs" onclick="location.href='/delete/'+${person.id}">Delete</button> </td> </tr> </tbody> </table> </div> </div> <div class="layui-row"> <div class="layui-col-md12"> <button class="layui-btn" onclick="location.href='/add'">Add Person</button> </div> </div> </div> </body> </html> ``` 这个模板使用Thymeleaf作为模板引擎,并使用Layui来创建漂亮的表格和按钮。它列出了所有人员记录,并提供了添加,编辑和删除记录的链接。 接下来,我们需要创建一个名为add.html的文件,其中包含添加人员记录的表单。添加以下代码: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Add Person</title> <link rel="stylesheet" type="text/css" href="/webjars/layui/2.5.4/css/layui.css"> <script type="text/javascript" src="/webjars/jquery/3.1.0/jquery.min.js"></script> <script type="text/javascript" src="/webjars/layui/2.5.4/layui.js"></script> </head> <body> <div class="layui-container"> <div class="layui-row"> <div class="layui-col-md12"> <h2>Add Person</h2> </div> </div> <div class="layui-row"> <div class="layui-col-md12"> <form class="layui-form" method="post" action="/add"> <div class="layui-form-item"> <label class="layui-form-label">Name:</label> <div class="layui-input-block"> <input type="text" name="name" required lay-verify="required" placeholder="Enter name" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">Age:</label> <div class="layui-input-block"> <input type="text" name="age" required lay-verify="required" placeholder="Enter age" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <div class="layui-input-block"> <button class="layui-btn" lay-submit lay-filter="formDemo">Submit</button> <button type="reset" class="layui-btn layui-btn-primary">Reset</button> </div> </div> </form> </div> </div> </div> </body> </html> ``` 这个模板使用Layui创建一个包含name和age字段的表单。当用户提交表单时,它将数据发送到服务器上的/add路由。 接下来,我们需要创建一个名为edit.html的文件,其中包含编辑人员记录的表单。添加以下代码: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Edit Person</title> <link rel="stylesheet" type="text/css" href="/webjars/layui/2.5.4/css/layui.css"> <script type="text/javascript" src="/webjars/jquery/3.1.0/jquery.min.js"></script> <script type="text/javascript" src="/webjars/layui/2.5.4/layui.js"></script> </head> <body> <div class="layui-container"> <div class="layui-row"> <div class="layui-col-md12"> <h2>Edit Person</h2> </div> </div> <div class="layui-row"> <div class="layui-col-md12"> <form class="layui-form" method="post" th:action="@{'/edit/'+${person.id}}" th:object="${person}"> <div class="layui-form-item"> <label class="layui-form-label">Name:</label> <div class="layui-input-block"> <input type="text" name="name" required lay-verify="required" th:value="${person.name}" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">Age:</label> <div class="layui-input-block"> <input type="text" name="age" required lay-verify="required" th:value="${person.age}" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <div class="layui-input-block"> <button class="layui-btn" lay-submit lay-filter="formDemo">Submit</button> <button type="reset" class="layui-btn layui-btn-primary">Reset</button> </div> </div> </form> </div> </div> </div> </body> </html> ``` 这个模板使用Layui创建一个包含name和age字段的表单,并将表单数据预填充为当前记录的值。当用户提交表单时,它将数据发送到服务器上的/edit/{id}路由。 最后,我们需要为我们的应用程序创建一个入口点。创建一个名为Application的类,并在其中添加以下代码: ```java @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 现在我们已经设置好了我们的应用程序,运行它并访问http://localhost:8080/,你应该能够看到一个包含所有人员记录的表格,以及添加,编辑和删除记录的链接。 这就是使用Spring Boot和Layui实现网页上增删改查的基本步骤。当然,你可以扩展它以满足你的需求。

相关推荐

最新推荐

recommend-type

智慧酒店项目智能化系统汇报方案qy.pptx

智慧酒店项目智能化系统汇报方案qy.pptx
recommend-type

基于C语言编写的高并发Epoll服务器.zip

基于C语言编写的高并发Epoll服务器.zip
recommend-type

liba2ps1-4.14-bp156.5.5.ppc64le.rpm

liba2ps1-4.14-bp156.5.5.ppc64le
recommend-type

基于matlab实现囚徒困境中的博弈策略的模拟:尝试了采用几种策略进行博弈使最终双赢的概率变大.rar

基于matlab实现囚徒困境中的博弈策略的模拟:尝试了采用几种策略进行博弈使最终双赢的概率变大.rar
recommend-type

RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz

REALTEK 8188FTV 8188eus 8188etv linux驱动程序稳定版本, 支持AP,STA 以及AP+STA 共存模式。 稳定支持linux4.0以上内核。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

Redis验证与连接:快速连接Redis服务器指南

![Redis验证与连接:快速连接Redis服务器指南](https://img-blog.csdnimg.cn/20200905155530592.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzNTg5NTEw,size_16,color_FFFFFF,t_70) # 1. Redis验证与连接概述 Redis是一个开源的、内存中的数据结构存储系统,它使用键值对来存储数据。为了确保数据的安全和完整性,Redis提供了多
recommend-type

gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker app:app 报错 ModuleNotFoundError: No module named 'geventwebsocket' ]

这个报错是因为在你的环境中没有安装 `geventwebsocket` 模块,可以使用下面的命令来安装: ``` pip install gevent-websocket ``` 安装完成后再次运行 `gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker app:app` 就不会出现这个报错了。
recommend-type

c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf

校园超市商品信息管理系统课程设计旨在帮助学生深入理解程序设计的基础知识,同时锻炼他们的实际操作能力。通过设计和实现一个校园超市商品信息管理系统,学生掌握了如何利用计算机科学与技术知识解决实际问题的能力。在课程设计过程中,学生需要对超市商品和销售员的关系进行有效管理,使系统功能更全面、实用,从而提高用户体验和便利性。 学生在课程设计过程中展现了积极的学习态度和纪律,没有缺勤情况,演示过程流畅且作品具有很强的使用价值。设计报告完整详细,展现了对问题的深入思考和解决能力。在答辩环节中,学生能够自信地回答问题,展示出扎实的专业知识和逻辑思维能力。教师对学生的表现予以肯定,认为学生在课程设计中表现出色,值得称赞。 整个课程设计过程包括平时成绩、报告成绩和演示与答辩成绩三个部分,其中平时表现占比20%,报告成绩占比40%,演示与答辩成绩占比40%。通过这三个部分的综合评定,最终为学生总成绩提供参考。总评分以百分制计算,全面评估学生在课程设计中的各项表现,最终为学生提供综合评价和反馈意见。 通过校园超市商品信息管理系统课程设计,学生不仅提升了对程序设计基础知识的理解与应用能力,同时也增强了团队协作和沟通能力。这一过程旨在培养学生综合运用技术解决问题的能力,为其未来的专业发展打下坚实基础。学生在进行校园超市商品信息管理系统课程设计过程中,不仅获得了理论知识的提升,同时也锻炼了实践能力和创新思维,为其未来的职业发展奠定了坚实基础。 校园超市商品信息管理系统课程设计的目的在于促进学生对程序设计基础知识的深入理解与掌握,同时培养学生解决实际问题的能力。通过对系统功能和用户需求的全面考量,学生设计了一个实用、高效的校园超市商品信息管理系统,为用户提供了更便捷、更高效的管理和使用体验。 综上所述,校园超市商品信息管理系统课程设计是一项旨在提升学生综合能力和实践技能的重要教学活动。通过此次设计,学生不仅深化了对程序设计基础知识的理解,还培养了解决实际问题的能力和团队合作精神。这一过程将为学生未来的专业发展提供坚实基础,使其在实际工作中能够胜任更多挑战。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依