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

SpringBoot+Jpa+Thymeleaf实现增删改查

SpringBoot+Jpa+Thymeleaf实现增删改查

这篇文章介绍如何使用 Jpa 和 Thymeleaf 做一个增删改查的示例。

1、pom依赖

pom 包里面添加JpaThymeleaf 的相关包引用

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.0.RELEASE</version></parent><groupId>com.example</groupId><artifactId>spring-boot-jpa-thymeleaf-curd</artifactId><version>0.0.1-SNAPSHOT</version><name>spring-boot-jpa-thymeleaf-curd</name><description>spring-boot-jpa-thymeleaf-curd</description><properties><java.version>1.8</java.version></properties><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>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><fork>true</fork></configuration></plugin></plugins></build></project>

2、application.properties中添加配置

spring.datasource.url=jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.jpa.properties.hibernate.hbm2ddl.auto=create
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true
spring.thymeleaf.cache=false

其中propertiesspring.thymeleaf.cache=false是关闭 Thymeleaf 的缓存,不然在开发过程中修改页面不会

立刻生效需要重启,生产可配置为 true。

在项目 resources 目录下会有两个文件夹:static目录用于放置网站的静态内容如 cssjs图片

templates 目录用于放置项目使用的页面模板。

3、启动类

启动类需要添加 Servlet 的支持

package com.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;@SpringBootApplication
public class Application extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(Application.class);}public static void main(String[] args) throws Exception {SpringApplication.run(Application.class, args);}
}

4、数据库层代码

实体类映射数据库表

package com.example.model;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;@Entity
public class User {@Id@GeneratedValueprivate long id;@Column(nullable = false, unique = true)private String userName;@Column(nullable = false)private String password;@Column(nullable = false)private int age;public long getId() {return id;}public User setId(long id) {this.id = id;return this;}public String getUserName() {return userName;}public User setUserName(String userName) {this.userName = userName;return this;}public String getPassword() {return password;}public User setPassword(String password) {this.password = password;return this;}public int getAge() {return age;}public User setAge(int age) {this.age = age;return this;}
}

5、 JpaRepository

package com.example.repository;import com.example.model.User;
import org.springframework.data.jpa.repository.JpaRepository;public interface UserRepository extends JpaRepository<User, Long> {User findById(long id);void deleteById(Long id);
}

继承 JpaRepository 类会自动实现很多内置的方法,包括增删改查,也可以根据方法名来自动生成相关 Sql。

6、业务层处理

Service 调用 Jpa 实现相关的增删改查,实际项目中 Service 层处理具体的业务代码。

package com.example.service;import com.example.model.User;import java.util.List;public interface UserService {public List<User> getUserList();public User findUserById(long id);public void save(User user);public void edit(User user);public void delete(long id);}
package com.example.service.impl;import com.example.model.User;
import com.example.repository.UserRepository;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserRepository userRepository;@Overridepublic List<User> getUserList() {return userRepository.findAll();}@Overridepublic User findUserById(long id) {return userRepository.findById(id);}@Overridepublic void save(User user) {userRepository.save(user);}@Overridepublic void edit(User user) {userRepository.save(user);}@Overridepublic void delete(long id) {userRepository.deleteById(id);}
}

7、控制层

package com.example.web;import com.example.model.User;
import com.example.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;import javax.annotation.Resource;
import java.util.List;@Controller
public class UserController {@ResourceUserService userService;@RequestMapping("/")public String index() {return "redirect:/list";}@RequestMapping("/list")public String list(Model model) {List<User> users = userService.getUserList();model.addAttribute("users", users);return "user/list";}@RequestMapping("/toAdd")public String toAdd() {return "user/userAdd";}@RequestMapping("/add")public String add(User user) {userService.save(user);return "redirect:/list";}@RequestMapping("/toEdit")public String toEdit(Model model, Long id) {User user = userService.findUserById(id);model.addAttribute("user", user);return "user/userEdit";}@RequestMapping("/edit")public String edit(User user) {userService.edit(user);return "redirect:/list";}@RequestMapping("/delete")public String delete(Long id) {userService.delete(id);return "redirect:/list";}
}

Controller 负责接收请求,处理完后将页面内容返回给前端。

  • return "user/userEdit"; 代表会直接去 resources 目录下找相关的文件。

  • return "redirect:/list"; 代表转发到对应的 Controller,这个示例就相当于删除内容之后自动调整到

    list 请求,然后再输出到页面。

8、页面内容

list 列表 list.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"/><title>userList</title><link rel="stylesheet" th:href="@{/css/bootstrap.css}"></link>
</head>
<body class="container">
<br/>
<h1>用户列表</h1>
<br/><br/>
<div class="with:80%"><table class="table table-hover"><thead><tr><th>#</th><th>User Name</th><th>Password</th><th>Age</th><th>Edit</th><th>Delete</th></tr></thead><tbody><tr  th:each="user : ${users}"><th scope="row" th:text="${user.id}">1</th><td th:text="${user.userName}">neo</td><td th:text="${user.password}">Otto</td><td th:text="${user.age}">6</td><td><a th:href="@{/toEdit(id=${user.id})}">edit</a></td><td><a th:href="@{/delete(id=${user.id})}">delete</a></td></tr></tbody></table>
</div>
<div class="form-group"><div class="col-sm-2 control-label"><a href="/toAdd" th:href="@{/toAdd}" class="btn btn-info">add</a></div>
</div></body>
</html>

访问http://localhost:8080/,效果如下图所示:
在这里插入图片描述

<tr th:each="user : ${users}"> 这里会从 Controler 层 model set 的对象去获取相关的内容,th:each

示会循环遍历对象内容。

点击Add按钮会跳转到新增页面。

新增页面 userAdd.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"/><title>user</title><link rel="stylesheet" th:href="@{/css/bootstrap.css}"></link>
</head>
<body class="container">
<br/>
<h1>添加用户</h1>
<br/><br/>
<div class="with:80%"><form class="form-horizontal"   th:action="@{/add}"  method="post"><div class="form-group"><label for="userName" class="col-sm-2 control-label">userName</label><div class="col-sm-10"><input type="text" class="form-control" name="userName"  id="userName" placeholder="userName"/></div></div><div class="form-group"><label for="password" class="col-sm-2 control-label" >Password</label><div class="col-sm-10"><input type="password" class="form-control" name="password" id="password" placeholder="Password"/></div></div><div class="form-group"><label for="age" class="col-sm-2 control-label">age</label><div class="col-sm-10"><input type="text" class="form-control" name="age"  id="age" placeholder="age"/></div></div><div class="form-group"><div class="col-sm-offset-2 col-sm-10"><input type="submit" value="Submit" class="btn btn-info" />&nbsp; &nbsp; &nbsp;<input type="reset" value="Reset" class="btn btn-info" /></div></div></form>
</div>
</body>
</html>

在这里插入图片描述

填写数据并且点击Submit按钮,用户添加成功并且跳转到用户列表页面。

在这里插入图片描述

点击edit按钮跳转到用户修改页面。

修改页面 userEdit.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"/><title>user</title><link rel="stylesheet" th:href="@{/css/bootstrap.css}"></link>
</head>
<body class="container">
<br/>
<h1>修改用户</h1>
<br/><br/>
<div class="with:80%"><form class="form-horizontal"   th:action="@{/edit}" th:object="${user}"  method="post"><input type="hidden" name="id" th:value="*{id}" /><div class="form-group"><label for="userName" class="col-sm-2 control-label">userName</label><div class="col-sm-10"><input type="text" class="form-control" name="userName"  id="userName" th:value="*{userName}" placeholder="userName"/></div></div><div class="form-group"><label for="password" class="col-sm-2 control-label" >Password</label><div class="col-sm-10"><input type="password" class="form-control" name="password" id="password"  th:value="*{password}" placeholder="Password"/></div></div><div class="form-group"><label for="age" class="col-sm-2 control-label">age</label><div class="col-sm-10"><input type="text" class="form-control" name="age"  id="age" th:value="*{age}" placeholder="age"/></div></div><div class="form-group"><div class="col-sm-offset-2 col-sm-10"><input type="submit" value="Submit" class="btn btn-info" />&nbsp; &nbsp; &nbsp;<a href="/toAdd" th:href="@{/list}" class="btn btn-info">Back</a></div></div></form>
</div>
</body>
</html>

在这里插入图片描述

修改用户的年龄为100然后点击Submit按钮提交修改。

然后跳转到用户列表页面发现数据修改了。

在这里插入图片描述

可以点击delete按钮删除用户。

删除之后跳转到用户列表页面。

在这里插入图片描述

这样一个使用 Jpa 和 Thymeleaf 的增删改查示例就完成了。

相关文章:

SpringBoot+Jpa+Thymeleaf实现增删改查

SpringBootJpaThymeleaf实现增删改查 这篇文章介绍如何使用 Jpa 和 Thymeleaf 做一个增删改查的示例。 1、pom依赖 pom 包里面添加Jpa 和 Thymeleaf 的相关包引用 <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.…...

最快的包管理器--pnpm创建vue项目完整步骤

1.用npm全局安装pnpm npm install -g pnpm 2.在要创建vue项目的包下进入cmd&#xff0c;输入&#xff1a; pnpm create vue 3.输入项目名字&#xff0c;选择Router,Pinia,ESLint,Prettier之后点确定 4.cd到创建好的项目 &#xff0c;安装依赖 cd .\刚创建好的项目名称\ p…...

算法通过村第九关-二分(中序遍历)黄金笔记|二叉搜索树

文章目录 前言1. 有序数组转二叉搜索树2. 寻找连个正序数组的中位数总结 前言 提示&#xff1a;有时候&#xff0c;我感觉自己一辈子活在两个闹钟之间&#xff0c;早上的第一次闹钟&#xff0c;以及5分钟之后的第二次闹钟。 --奥利弗萨克斯《意识的河流》 每个专题都有简单题&a…...

Mock.js之Element-ui搭建首页导航与左侧菜单

&#x1f3ac; 艳艳耶✌️&#xff1a;个人主页 &#x1f525; 个人专栏 &#xff1a;《Spring与Mybatis集成整合》《springMvc使用》 ⛺️ 生活的理想&#xff0c;为了不断更新自己 ! 1、Mock.js的使用 1.1.什么是Mock.js Mock.js是一个模拟数据的生成器&#xff0c;用来帮助前…...

robotframework在Jenkins执行踩坑

1. Groovy Template file [robot_results.groovy] was not found in $JENKINS_HOME/email_template 1.需要在managed files 添加robot_results.groovy。这个名字需要和配置在构建项目里default content一致&#xff08;Extended E-mail Notification默认设置里Default Content…...

关于ElementUI之首页导航与左侧菜单实现

目录 一.Mock 1.1.什么是Mock.js 1.2.特点 1.3.安装与配置 1.3.1. 安装mock.js 1.3.2.引入mock.js 1.4.mockjs使用 1.4.1.定义测试数据文件 1.4.2.mock拦截Ajax请求 1.4.3.界面代码优化 二.总线 2.1.是什么 2.2.前期准备 2.3.配置组件与路由关系 2.3.1. 配置组件 …...

基于springboot小区疫情防控系统

博主主页&#xff1a;猫头鹰源码 博主简介&#xff1a;Java领域优质创作者、CSDN博客专家、公司架构师、全网粉丝5万、专注Java技术领域和毕业设计项目实战 主要内容&#xff1a;毕业设计(Javaweb项目|小程序等)、简历模板、学习资料、面试题库、技术咨询 文末联系获取 项目介绍…...

【k8s】YAML语言基础

文章目录 YAML介绍语法支持的数据类型注意事项json与yaml互转 YAML介绍 YAML是一个类似于XML、JSON的标记语言。强调以数据为中心&#xff0c;并不是以标记语言为中心 <heima><age>15</age><address>Beijing</address> </heima>heima:age:…...

AI时代的中国困境: ChatGPT为什么难以复制

如今&#xff0c;几乎所有中国互联网大厂都公布了自己的“类ChatGPT”解决方案&#xff0c;有些还公布了背后的关于AI技术模型的详情。 其中最高调的是百度&#xff0c;其“文心一言”解决方案号称即将接入数十家内容平台和数以百计的媒体、自媒体。腾讯公布的微信 AI 模型“W…...

如何使用Docker安装最新版本的Redis并设置远程访问(含免费可视化工具)

文章目录 安装Docker安装Redisredis.conf文件远程访问Redis免费可视化工具相关链接Docker是一种开源的应用容器引擎,使用Docker可以让我们快速部署应用环境,本文介绍如何使用Docker安装最新版本的Redis。 安装Docker 首先需要安装Docker,具体的安装方法可以参考Docker官方文…...

怒刷LeetCode的第8天(Java版)

目录 第一题 题目来源 题目内容 解决方法 方法一&#xff1a;双指针和排序 ​编辑第二题 题目来源 题目内容 解决方法 方法一&#xff1a;双指针 方法二&#xff1a;递归 方法三&#xff1a;快慢指针 方法四&#xff1a;栈 第三题 题目来源 题目内容 解决方法…...

Vue Hooks 让Vue开发更简单与高效

Vue Hooks 让Vue开发更简单与高效 介绍 Vue Hooks 是一个基于 Vue.js 的插件&#xff0c;它提供了一种新的方式来编写 Vue 组件&#xff0c;使得开发更加简单和高效。它借鉴了 React Hooks 的概念&#xff0c;通过使用 Hooks&#xff0c;我们可以在不编写类组件的情况下&…...

Go编程规范

文章目录 注释转义符定义变量方法一&#xff1a;指定变量类型&#xff0c;声明后若不赋值&#xff0c;使用默认值方法二&#xff1a;根据值自行判定变量类型(类型推导)方法三&#xff1a;省略var, 注意:左侧的变量不应该是已经声明过的&#xff0c;否则会导致编译错误[推荐]全局…...

premiere 新建 视频导入 视频拼接 视频截取 多余视频删除

1 新建项目 文件 -> 新建 -> 项目 2 导入 2.1 方法一 直接从本地 将 文件拖入对应的文件夹 2.2 方法二 鼠标右键在指定素材文件夹, 选择导入 选择对应本地文件夹对应素材 3 预设 -> 粗剪 -> 在指定模块处 创建序列预设 3.1 指定模块处 鼠标右键 -> 新建项目…...

笔记01:第一行Python

NameError 名字不含特殊符号&#xff08;只能是英文、数字、下划线、中文等&#xff09;名字区分大小写名字先定义后使用 SyntaxError 不符合Python语法书写规范除了语法成分中的保留拼写错误输出中文符号if、for、def等语句末尾忘记冒号 IdentationError 缩进错误&#x…...

资产连接支持会话分屏,新增Passkey用户认证方式,支持查看在线用户信息,JumpServer堡垒机v3.7.0发布

2023年9月25日&#xff0c;JumpServer开源堡垒机正式发布v3.7.0版本。在这一版本中&#xff0c;在用户管理层面&#xff0c;为了提高使用JumpServer操作资产的效率&#xff0c;JumpServer支持对会话进行分屏操作&#xff0c;用户可以在一个浏览器页面打开多个会话&#xff0c;方…...

uniapp项目实践总结(二十二)分包优化和游客模式

导语&#xff1a;这篇主要介绍应用分包和游客模式相关的内容。 目录 应用分包游客模式 应用分包 微信对于小程序的打包压缩后的代码体积是有限制的&#xff0c;网页和 APP 也可以适用分包功能&#xff0c;因此需要进行分包添加以及分包优化。 分包添加 在pages.json文件中…...

Unity中UI组件对Shader调色

文章目录 前言一、原理在Shader中直接暴露的Color属性&#xff0c;不会与UI的Image组件中的Color形成属性绑定。因为UI的Image组件中更改的颜色是顶点颜色&#xff0c;如果需要在修改组件中的颜色时&#xff0c;使Shader中的颜色也同时改变。那么就需要在应用程序阶段传入到顶点…...

PhpStorm 2023年下载、安装教程和好用插件,保姆级教程

PhpStorm 2023年下载、安装教程和好用插件&#xff0c;保姆级教程 文章目录 PhpStorm 2023年下载、安装教程和好用插件&#xff0c;保姆级教程前言一、安装PhpStorm二、好用的插件简体中文包Chinese(Simplified)Language Pack 三、卸载插件CTRLN 查找类CTRLSHIFTN 全局搜索文件…...

1960-2017年世界各国总和生育率数据

1960-2017年世界各国总和生育率数据 1、时间&#xff1a;1960-2017年 2、指标&#xff1a;生育率 3、范围&#xff1a;全球各国 4、来源&#xff1a;世界银行 5、指标解释&#xff1a; 总生育率表示假设妇女度过整个生育期并按照当期的年龄别生育率生育孩子所生育的孩子数…...

synchronized 学习

学习源&#xff1a; https://www.bilibili.com/video/BV1aJ411V763?spm_id_from333.788.videopod.episodes&vd_source32e1c41a9370911ab06d12fbc36c4ebc 1.应用场景 不超卖&#xff0c;也要考虑性能问题&#xff08;场景&#xff09; 2.常见面试问题&#xff1a; sync出…...

从WWDC看苹果产品发展的规律

WWDC 是苹果公司一年一度面向全球开发者的盛会&#xff0c;其主题演讲展现了苹果在产品设计、技术路线、用户体验和生态系统构建上的核心理念与演进脉络。我们借助 ChatGPT Deep Research 工具&#xff0c;对过去十年 WWDC 主题演讲内容进行了系统化分析&#xff0c;形成了这份…...

【论文笔记】若干矿井粉尘检测算法概述

总的来说&#xff0c;传统机器学习、传统机器学习与深度学习的结合、LSTM等算法所需要的数据集来源于矿井传感器测量的粉尘浓度&#xff0c;通过建立回归模型来预测未来矿井的粉尘浓度。传统机器学习算法性能易受数据中极端值的影响。YOLO等计算机视觉算法所需要的数据集来源于…...

TRS收益互换:跨境资本流动的金融创新工具与系统化解决方案

一、TRS收益互换的本质与业务逻辑 &#xff08;一&#xff09;概念解析 TRS&#xff08;Total Return Swap&#xff09;收益互换是一种金融衍生工具&#xff0c;指交易双方约定在未来一定期限内&#xff0c;基于特定资产或指数的表现进行现金流交换的协议。其核心特征包括&am…...

Reasoning over Uncertain Text by Generative Large Language Models

https://ojs.aaai.org/index.php/AAAI/article/view/34674/36829https://ojs.aaai.org/index.php/AAAI/article/view/34674/36829 1. 概述 文本中的不确定性在许多语境中传达,从日常对话到特定领域的文档(例如医学文档)(Heritage 2013;Landmark、Gulbrandsen 和 Svenevei…...

Python Ovito统计金刚石结构数量

大家好,我是小马老师。 本文介绍python ovito方法统计金刚石结构的方法。 Ovito Identify diamond structure命令可以识别和统计金刚石结构,但是无法直接输出结构的变化情况。 本文使用python调用ovito包的方法,可以持续统计各步的金刚石结构,具体代码如下: from ovito…...

MinIO Docker 部署:仅开放一个端口

MinIO Docker 部署:仅开放一个端口 在实际的服务器部署中,出于安全和管理的考虑,我们可能只能开放一个端口。MinIO 是一个高性能的对象存储服务,支持 Docker 部署,但默认情况下它需要两个端口:一个是 API 端口(用于存储和访问数据),另一个是控制台端口(用于管理界面…...

Caliper 配置文件解析:fisco-bcos.json

config.yaml 文件 config.yaml 是 Caliper 的主配置文件,通常包含以下内容: test:name: fisco-bcos-test # 测试名称description: Performance test of FISCO-BCOS # 测试描述workers:type: local # 工作进程类型number: 5 # 工作进程数量monitor:type: - docker- pro…...

Unity VR/MR开发-VR开发与传统3D开发的差异

视频讲解链接&#xff1a;【XR马斯维】VR/MR开发与传统3D开发的差异【UnityVR/MR开发教程--入门】_哔哩哔哩_bilibili...

ui框架-文件列表展示

ui框架-文件列表展示 介绍 UI框架的文件列表展示组件&#xff0c;可以展示文件夹&#xff0c;支持列表展示和图标展示模式。组件提供了丰富的功能和可配置选项&#xff0c;适用于文件管理、文件上传等场景。 功能特性 支持列表模式和网格模式的切换展示支持文件和文件夹的层…...