综合实践:JPA+Thymeleaf 增删改查

在Java Web开发中,使用JPA(Java Persistence API)作为ORM(对象关系映射)框架和Thymeleaf作为模板引擎来实现增删改查(CRUD)操作是一种常见且高效的方式。以下是一个简单的示例,展示如何使用Spring Boot、JPA和Thymeleaf来实现一个基本的CRUD应用。

1. 搭建Spring Boot项目

首先,你需要创建一个Spring Boot项目。可以使用Spring Initializr(https://start.spring.io/)来快速生成项目结构。

  • 选择项目类型:Maven Project 或 Gradle Project
  • 语言:Java
  • Spring Boot版本:选择最新的稳定版本
  • 项目元数据:填写Group和Artifact名称
  • 依赖:Spring Web, Spring Data JPA, Thymeleaf, H2 Database(或你选择的数据库)

2. 配置数据库

src/main/resources/application.propertiesapplication.yml中配置数据库连接。以下是一个使用H2数据库的示例:

java 复制代码
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

3. 创建实体类

假设我们要管理一个Person实体,创建一个Person.java类:

java 复制代码
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. 创建JPA仓库

创建一个PersonRepository.java接口来继承JpaRepository

java 复制代码
import org.springframework.data.jpa.repository.JpaRepository;  
  
public interface PersonRepository extends JpaRepository<Person, Long> {  
}

5. 创建服务层

创建一个PersonService.java类来处理业务逻辑:

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Service;  
  
import java.util.List;  
  
@Service  
public class PersonService {  
  
    @Autowired  
    private PersonRepository personRepository;  
  
    public List<Person> findAll() {  
        return personRepository.findAll();  
    }  
  
    // 添加、更新、删除方法省略  
}

6. 创建控制器

创建一个PersonController.java类来处理HTTP请求:

java 复制代码
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 PersonService personService;  
  
    @GetMapping("/")  
    public String listPersons(Model model) {  
        model.addAttribute("persons", personService.findAll());  
        return "persons";  
    }  
  
    // 添加、更新、删除方法的视图映射省略  
}

7. 创建Thymeleaf模板

src/main/resources/templates目录下创建persons.html模板文件:

java 复制代码
<!DOCTYPE html>  
<html xmlns:th="http://www.thymeleaf.org">  
<head>  
    <title>Persons</title>  
</head>  
<body>  
    <h1>Persons</h1>  
    <table>  
        <thead>  
            <tr>  
                <th>ID</th>  
                <th>Name</th>  
                <th>Age</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>  
            </tr>  
        </tbody>  
    </table>  
    <!-- 添加表单、链接等用于CRUD操作 -->  
</body>  
</html>

8. 运行和测试

运行Spring Boot应用,并访问http://localhost:8080/来查看你的CRUD应用。

以上步骤提供了一个基本的框架,你可以根据具体需求添加表单、链接和逻辑来完善CRUD操作。

相关推荐
健康平安的活着3 分钟前
redis 中IO多路复用与Epoll函数
网络·数据库·redis
江凡心16 分钟前
Qt 每日面试题 -5
服务器·数据库·qt·学习·面试
HumoChen991 小时前
MySQL tinyint(1)类型数据在经过flink cdc同步到doris后只有0/1问题定位与解决
数据库·mysql·flink
阿乾之铭2 小时前
MySQL数据查询(基础)
数据库·sql·mysql
Yan-D2 小时前
【Redis 源码】2项目结构说明
数据库·redis·缓存
DieSnowK2 小时前
[Redis][事务]详细讲解
数据库·redis·缓存·事务·redis事务·新手向·事务操作
开源哥662 小时前
【含文档】基于Springboot+微信小程序 的中心医院用户移动端(含源码+数据库+lw)
数据库·spring boot·微信小程序
rubyw2 小时前
SQL:如果字段需要排除某个值但又有空值时,不能直接用“<>”或not in
服务器·数据库·sql
TuringSnowy2 小时前
SQL_having_pandas_filter
数据库·笔记·sql·mysql·pandas