马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在Java Web开发中,JPA(Java Persistence API)和Thymeleaf是非常流行的技术组合,分别用于数据持久化和前端模板渲染。下面我将指导你如何使用Spring Boot框架结合JPA和Thymeleaf来实现一个根本的增删改查(CRUD)应用。
1. 搭建项目基础
起首,你需要创建一个Spring Boot项目。你可以使用Spring Initializr(https://start.spring.io/)来快速生成项目骨架。
- 选择项目类型:Maven Project
- 选择Spring Boot版本:选择最新的稳定版本
- 添加依赖:Spring Web, Spring Data JPA, Thymeleaf, H2 Database(作为示例数据库)
2. 设置数据库
在src/main/resources/application.properties中设置数据库毗连和JPA相干属性:
- spring.datasource.url=jdbc:h2:mem:testdb
- spring.datasource.driverClassName=org.h2.Driver
- spring.datasource.username=sa
- spring.datasource.password=password
- spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
- spring.h2.console.enabled=true
- spring.jpa.show-sql=true
- spring.jpa.hibernate.ddl-auto=update
复制代码 3. 创建实体类
以Person为例,创建一个实体类:
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
-
- @Entity
- public class Person {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private Long id;
- private String name;
- private int age;
-
- // 构造方法、getter和setter省略
- }
复制代码 4. 创建Repository
创建一个JPA Repository来访问数据库:
- import org.springframework.data.jpa.repository.JpaRepository;
-
- public interface PersonRepository extends JpaRepository<Person, Long> {
- }
复制代码 5. 创建Controller
创建Controller来处理HTTP哀求:
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.*;
-
- @Controller
- public class PersonController {
-
- @Autowired
- private PersonRepository personRepository;
-
- // 列表页
- @GetMapping("/persons")
- public String listPersons(Model model) {
- model.addAttribute("persons", personRepository.findAll());
- return "persons";
- }
-
- // 新增页面
- @GetMapping("/persons/new")
- public String newPerson(Model model) {
- model.addAttribute("person", new Person());
- return "person_form";
- }
-
- // 保存
- @PostMapping("/persons")
- public String savePerson(@ModelAttribute Person person) {
- personRepository.save(person);
- return "redirect:/persons";
- }
-
- // 编辑页面
- @GetMapping("/persons/{id}/edit")
- public String editPerson(@PathVariable Long id, Model model) {
- Person person = personRepository.findById(id).orElse(null);
- if (person != null) {
- model.addAttribute("person", person);
- return "person_form";
- }
- return "redirect:/persons";
- }
-
- // 删除
- @GetMapping("/persons/{id}/delete")
- public String deletePerson(@PathVariable Long id) {
- personRepository.deleteById(id);
- return "redirect:/persons";
- }
- }
复制代码 6. 创建Thymeleaf模板
- persons.html(列表页)
- person_form.html(新增和编辑页)
在src/main/resources/templates目次下创建这些HTML文件,并使用Thymeleaf语法来渲染数据。
7. 运行和测试
运行Spring Boot应用,并通过欣赏器访问http://localhost:8080/persons来查看列表页面,你可以实验添加、编辑和删除记录来测试功能。
8. 附加提示
- 确保你的HTML表单和Controller中的方法精确匹配(尤其是POST哀求的方法名)。
- 使用Spring Security来掩护你的应用,制止未授权访问。
- 在生产情况中使用更安全的数据库设置,如使用毗连池和设置加密的数据库毗连。
通过以上步骤,你应该能够创建一个根本的Spring Boot应用,该应用使用JPA进行数据持久化,并使用Thymeleaf作为前端模板引擎来展示数据。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |