当前位置: 首页 > wzjs >正文

做外链选择那些网站wordpress anspress

做外链选择那些网站,wordpress anspress,网站开发播放大视频卡顿,随身办app下载关键词:Spring Boot、Thymeleaf、RESTful API、前后端整合、用户管理 ✅ 功能概述 本文将为你提供一个完整的 Spring Boot Thymeleaf RESTful API 的前后端整合项目,实现以下功能: 模块功能用户管理查看用户列表、新增用户、删除用户后端…

关键词:Spring Boot、Thymeleaf、RESTful API、前后端整合、用户管理


✅ 功能概述

本文将为你提供一个完整的 Spring Boot + Thymeleaf + RESTful API 的前后端整合项目,实现以下功能:

模块功能
用户管理查看用户列表、新增用户、删除用户
后端接口提供 JSON 格式的 RESTful API
前端页面使用 Thymeleaf 渲染 HTML 页面
数据持久化使用 Spring Data JPA + H2 内存数据库

📦 一、创建 Spring Boot 项目

1. 使用idea创建项目

选择如下配置:

  • Project: Maven
  • Language: Java
  • Spring Boot Version: 3.x(如 3.3)
  • Dependencies:
    • Spring Web
    • Spring Data JPA
    • Thymeleaf
    • H2 Database (用于测试)

下载并解压后导入 IDE(如 IntelliJ IDEA 或 Eclipse)。


📁 二、项目结构概览

springboot-thymeleaf-restful/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/
│   │   │       └── example/
│   │   │           └── demo/
│   │   │               ├── DemoApplication.java
│   │   │               ├── controller/
│   │   │               │   └── UserController.java
│   │   │               ├── model/
│   │   │               │   └── User.java
│   │   │               ├── repository/
│   │   │               │   └── UserRepository.java
│   │   │               └── service/
│   │   │                   └── UserService.java
│   │   └── resources/
│   │       ├── templates/
│   │       │   └── users.html
│   │       └── application.properties
└── pom.xml

🧱 三、添加依赖(pom.xml)

<dependencies><!-- Spring 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><!-- Thymeleaf --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!-- H2 Database --><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope></dependency><!-- 测试依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
</dependencies>

🗃️ 四、数据模型与数据库操作

1. 实体类 User.java

package com.example.demo.model;import jakarta.persistence.*;@Entity
public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String name;private String email;public User() {}public User(String name, String email) {this.name = name;this.email = email;}// Getter & Setter
}

2. Repository 接口 UserRepository.java

package com.example.demo.repository;import com.example.demo.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}

3. 服务层 UserService.java

package com.example.demo.service;import com.example.demo.model.User;
import com.example.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserService {@Autowiredprivate UserRepository userRepository;public List<User> getAllUsers() {return userRepository.findAll();}public User saveUser(User user) {return userRepository.save(user);}public void deleteUserById(Long id) {userRepository.deleteById(id);}
}

🎛️ 五、控制器 UserController.java

package com.example.demo.controller;import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;import java.util.List;@Controller
@RequestMapping("/users")
public class UserController {@Autowiredprivate UserService userService;// 展示用户页面@GetMapping("")public String showUsers(Model model) {List<User> users = userService.getAllUsers();model.addAttribute("users", users);return "users";}// 新增用户@PostMapping("/add")public String addUser(@ModelAttribute User user) {userService.saveUser(user);return "redirect:/users";}// 删除用户@GetMapping("/{id}/delete")public String deleteUser(@PathVariable Long id) {userService.deleteUserById(id);return "redirect:/users";}// 返回 JSON 格式的所有用户(供外部调用)@GetMapping(path = "/api", produces = "application/json")@ResponseBodypublic List<User> getUsersJson() {return userService.getAllUsers();}
}

🖼️ 六、前端页面 templates/users.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>用户管理</title><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-4"><h2>用户管理</h2><!-- 表单提交 --><form th:action="@{/users/add}" method="post"><div class="mb-3"><label for="name" class="form-label">姓名</label><input type="text" name="name" id="name" class="form-control" required /></div><div class="mb-3"><label for="email" class="form-label">邮箱</label><input type="email" name="email" id="email" class="form-control" required /></div><button type="submit" class="btn btn-primary">添加用户</button></form><hr /><!-- 用户列表 --><table class="table table-bordered mt-4"><thead><tr><th>ID</th><th>姓名</th><th>邮箱</th><th>操作</th></tr></thead><tbody><tr th:each="user : ${users}"><td th:text="${user.id}"></td><td th:text="${user.name}"></td><td th:text="${user.email}"></td><td><a th:href="@{/{id}/delete(id=${user.id})}" class="btn btn-danger btn-sm">删除</a></td></tr></tbody></table>
</div>
</body>
</html>

⚙️ 七、配置文件 application.properties

# H2 数据库配置
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# 开启 H2 控制台
spring.h2.console.enabled=true# Thymeleaf 缓存关闭(开发阶段)
spring.thymeleaf.cache=false

▶️ 八、运行与测试

1. 启动项目

运行 DemoApplication.java 中的 main() 方法启动 Spring Boot 应用。

2. 访问页面

打开浏览器访问:

http://localhost:8080/users

你会看到用户管理页面,可以:

  • 添加用户(提交表单)
  • 查看用户列表
  • 删除用户

3. 调用 RESTful API

访问 JSON 格式的用户列表:

GET http://localhost:8080/users/api

返回结果类似:

[{"id":1,"name":"张三","email":"zhangsan@example.com"}
]

✅ 总结

技术点内容
Spring Boot 整合 Thymeleaf快速搭建前后端一体化应用
RESTful API 设计提供 JSON 接口供其他系统调用
MVC 架构实践控制器、模型、视图三层清晰分工
内存数据库 H2快速测试无需安装数据库
前后端共存同时支持 HTML 页面和 JSON 接口

文章转载自:

http://8lPXwJmc.tkfnp.cn
http://56WjetWQ.tkfnp.cn
http://6lAtrFYt.tkfnp.cn
http://cQYrafdP.tkfnp.cn
http://CaieOMcz.tkfnp.cn
http://4z8j3sbb.tkfnp.cn
http://1DHKOsGQ.tkfnp.cn
http://UZuIMD0U.tkfnp.cn
http://1aJ1Fe0L.tkfnp.cn
http://abXL8nRx.tkfnp.cn
http://UV5LSc60.tkfnp.cn
http://SsDQWDzO.tkfnp.cn
http://Kp9MsoV8.tkfnp.cn
http://A0ynjRsR.tkfnp.cn
http://sRTmhjln.tkfnp.cn
http://08Tb4Ni2.tkfnp.cn
http://8wr7d2Ia.tkfnp.cn
http://sqG0UF4f.tkfnp.cn
http://430psB18.tkfnp.cn
http://2iS5SUh2.tkfnp.cn
http://JTkmBKYq.tkfnp.cn
http://Ek6VRFok.tkfnp.cn
http://WijndtDr.tkfnp.cn
http://ni9nWKWE.tkfnp.cn
http://XfFUl5MU.tkfnp.cn
http://8H2OzGIL.tkfnp.cn
http://tHxyApEs.tkfnp.cn
http://s9FP9oLQ.tkfnp.cn
http://4451pcQS.tkfnp.cn
http://Zxvc3FRF.tkfnp.cn
http://www.dtcms.com/wzjs/603175.html

相关文章:

  • 长春网站开发培训wordpress调整语言
  • 建瓯建设局网站百度软件下载安装
  • 站长工具5g东胜网站建设
  • 网站开发项目的简介做推广网站的文章术语
  • 网站制作 苏州做网站成功案例
  • 做网站如何备案python软件
  • 免费创建论坛网站天元建设集团有限公司嘉和新城
  • 广州北京网站建设公司wordpress 文章 路径
  • 网站建设中倒计时模板下载wordpress 5.2.2安装要求
  • 商城网站建设需求宁波网站建设就业方向
  • 房产设计公司网站网站制作常见问题
  • tp框架做响应式网站锡林郭勒盟建设工程造价管理网站
  • cydia软件源网站开发网站建设需要学代码吗
  • 工信部网站备案查不到dw网页代码
  • 以企业介绍为主做外贸网站好吗东莞房价会涨吗
  • 网站页面的组成个人淘宝客网站如何备案
  • 上海cms网站建设网站制作报价ihanshi
  • 网站优化 北京抖音代运营话术模板
  • 廊坊网站建设为什么做网站要有自己的服务器
  • 深圳网站建设方维少儿图书销售网站开发背景
  • 静海县建设局网站网站建设培训四川
  • php网站后台忘记密码wordpress仿百度首页
  • 电子商务网站建设的风险分析做别墅花园绿化的网站
  • 企业建站的费用小程序的定义
  • 做响应网站的素材网站有哪些公司简介宣传
  • 博罗县建设局网站婚纱摄影网站模版整站源码
  • 河南网站制作公司百度seo排名
  • 台州网站建设公司.昆山网页设计公司书生商友
  • 资阳建网站网站演示网站代码
  • 网站设计风格确认书响应式网站制设计