使用idea的spring框架做一个具有登录功能和增删改查的功能的通讯录源码

时间: 2023-12-10 19:41:30 浏览: 31
很抱歉,我无法为您提供完整的源代码,但我可以给您提供一些指导和建议以帮助您开始。 首先,您需要设置一个Spring Boot项目并添加所需的依赖项。您可能需要以下依赖项: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> ``` 接下来,您需要创建一个简单的联系人类,如下所示: ```java @Data @Entity public class Contact { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @NotBlank private String name; @Email @NotBlank private String email; @NotBlank private String phone; public Contact() {} public Contact(String name, String email, String phone) { this.name = name; this.email = email; this.phone = phone; } } ``` 然后,您需要创建一个联系人存储库接口,如下所示: ```java @Repository public interface ContactRepository extends JpaRepository<Contact, Long> {} ``` 接下来,您需要创建一个联系人服务接口和实现,如下所示: ```java public interface ContactService { List<Contact> getAllContacts(); void saveContact(Contact contact); void deleteContact(Long id); Contact getContactById(Long id); } @Service public class ContactServiceImpl implements ContactService { @Autowired private ContactRepository contactRepository; @Override public List<Contact> getAllContacts() { return contactRepository.findAll(); } @Override public void saveContact(Contact contact) { contactRepository.save(contact); } @Override public void deleteContact(Long id) { contactRepository.deleteById(id); } @Override public Contact getContactById(Long id) { Optional<Contact> optionalContact = contactRepository.findById(id); return optionalContact.orElse(null); } } ``` 然后,您需要创建一个控制器类,如下所示: ```java @Controller public class ContactController { @Autowired private ContactService contactService; @GetMapping("/") public String home(Model model) { List<Contact> contacts = contactService.getAllContacts(); model.addAttribute("contacts", contacts); return "home"; } @GetMapping("/add") public String addContactForm(Model model) { model.addAttribute("contact", new Contact()); return "add_contact"; } @PostMapping("/save") public String saveContact(@ModelAttribute("contact") Contact contact) { contactService.saveContact(contact); return "redirect:/"; } @GetMapping("/edit/{id}") public String editContactForm(@PathVariable Long id, Model model) { Contact contact = contactService.getContactById(id); model.addAttribute("contact", contact); return "edit_contact"; } @PostMapping("/update/{id}") public String updateContact(@PathVariable Long id, @ModelAttribute("contact") Contact contact) { Contact existingContact = contactService.getContactById(id); existingContact.setName(contact.getName()); existingContact.setEmail(contact.getEmail()); existingContact.setPhone(contact.getPhone()); contactService.saveContact(existingContact); return "redirect:/"; } @GetMapping("/delete/{id}") public String deleteContact(@PathVariable Long id) { contactService.deleteContact(id); return "redirect:/"; } } ``` 最后,您需要创建一些前端页面,如下所示: home.html ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Contacts</title> </head> <body> <h1>Contacts</h1> <table> <tr> <th>Name</th> <th>Email</th> <th>Phone</th> <th></th> <th></th> </tr> <tr th:each="contact : ${contacts}"> <td th:text="${contact.name}"></td> <td th:text="${contact.email}"></td> <td th:text="${contact.phone}"></td> <td><a th:href="@{/edit/{id}(id=${contact.id})}">Edit</a></td> <td><a th:href="@{/delete/{id}(id=${contact.id})}" onclick="return confirm('Are you sure?')">Delete</a></td> </tr> </table> <p><a href="/add">Add Contact</a></p> </body> </html> ``` add_contact.html ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Add Contact</title> </head> <body> <h1>Add Contact</h1> <form th:action="@{/save}" th:object="${contact}" method="post"> <label for="name">Name:</label> <input type="text" id="name" name="name" required th:value="${contact.name}"/><br/> <label for="email">Email:</label> <input type="email" id="email" name="email" required th:value="${contact.email}"/><br/> <label for="phone">Phone:</label> <input type="text" id="phone" name="phone" required th:value="${contact.phone}"/><br/> <input type="submit" value="Save"/> </form> </body> </html> ``` edit_contact.html ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Edit Contact</title> </head> <body> <h1>Edit Contact</h1> <form th:action="@{/update/{id}(id=${contact.id})}" th:object="${contact}" method="post"> <label for="name">Name:</label> <input type="text" id="name" name="name" required th:value="${contact.name}"/><br/> <label for="email">Email:</label> <input type="email" id="email" name="email" required th:value="${contact.email}"/><br/> <label for="phone">Phone:</label> <input type="text" id="phone" name="phone" required th:value="${contact.phone}"/><br/> <input type="submit" value="Save"/> </form> </body> </html> ``` 这只是一个简单的示例,但希望它能帮助您开始使用Spring框架创建通讯录应用程序。

相关推荐

最新推荐

recommend-type

使用idea搭建一个spring mvc项目的图文教程

主要介绍了使用idea直接创建一个spring mvc项目的图文教程,本文通过图文并茂的方式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

IDEA 中 30 秒创建一个 Spring Cloud Alibaba 工程

主要介绍了IDEA 中 30 秒生成 Spring Cloud Alibaba 工程,本文通过图文并茂的形式给大家介绍的非常详细对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

IDEA实现 springmvc的简单注册登录功能的示例代码

主要介绍了IDEA实现 springmvc的简单注册登录功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

使用IntelliJ IDEA搭建SSM框架的图文教程

本文通过图文并茂的形式给大家介绍了使用IntelliJ IDEA搭建SSM框架的教程,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

JavaWeb使用Cookie模拟实现自动登录功能(不需用户名和密码)

不需要填写用户名和密码自动登录系统,其实现思路使用cookie模拟浏览器自动登录,对cookie实现自动登录功能感兴趣的朋友一起学习吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

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

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。