SpringBoot集成JPA实现分页和CRUD

SpringBoot集成JPA实现分页和CRUD

文章目录

  • SpringBoot集成JPA实现分页和CRUD
    • pom.xml
    • application.properties
    • addCategory.jsp
    • editCategory.jsp
    • hello.jsp
    • listCategory.jsp
    • Category
    • CategoryDAO
    • CategoryService
    • CategoryServiceImpl
    • Page4Navigator
    • RedisConfig
    • CategoryController
    • HelloController

在这里插入图片描述
在这里插入图片描述
懒得敲代码,直接拷贝: SpringBoot集成JPA实现分页和CRUD

pom.xml

<?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.3.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.moon</groupId><artifactId>springboot</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot</name><description>springboot</description><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><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-test</artifactId><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency><!--要使用jsp添加这个--><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><scope>provided</scope></dependency><!--数据库--><!-- mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.30</version></dependency><!-- jpa--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

application.properties

填自己的数据库和密码

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update###########################redis#########################
#Redis数据库索引(默认为0)
spring.redis.database=0
#Redis服务器地址
spring.redis.host=127.0.0.1
#Redis服务器连接端口
spring.redis.port=6379
#Redis服务器连接密码(默认为空)
spring.redis.password=
#连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=10
#连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
#连接池中的最大空闲连接
spring.redis.pool.max-idle=8
#连接池中的最小空闲连接
spring.redis.pool.min-idle=0
#连接超时时间(毫秒)
spring.redis.timeout=0spring.jpa.show-sql=true

addCategory.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<div style="margin: 0px auto; width: 500px"><form action="updateCategory" method="post">name:<input name="name" value="${category.name}"><br><button type="submit">提交</button></form>
</div>

editCategory.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8" isELIgnored="false"%><div style="margin: 0px auto; width: 500px"><form action="updateCategory" method="post">name:<input name="name" value="${c.name}"><br><input name="id" type="hidden" value="${c.id}"><button type="submit">提交</button></form>
</div>

hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8" isELIgnored="false"%><div style="margin: 0px auto; width: 500px"><form action="updateCategory" method="post">name:<input name="name" value="${c.name}"><br><input name="id" type="hidden" value="${c.id}"><button type="submit">提交</button></form>
</div>

listCategory.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div align="center"></div>
<div style="width: 500px;margin: 20px auto;text-align: center"><table align="center" border="1" cellspacing="0"><caption>员工<a href="addCategory">添加</a></caption><thead><tr><th>id</th><th>name</th><th>编辑</th><th>删除</th></tr></thead><tbody><c:forEach items="${page.content}" var="c" varStatus="st"><tr><td>${c.id}</td><td>${c.name}</td><td><a href="editCategory?id=${c.id}">编辑</a></td><td><a href="deleteCategory?id=${c.id}">删除</a></td></tr></c:forEach></tbody></table><div><a href="?start=0">【首页】</a><a href="?start=${page.number-1}">【上一页】</a><a href="?start=${page.number+1}">【下一页】</a><a href="?start=${page.totalPages-1}">【末页】</a></div><div></div>
</div>

Category

package com.moon.springboot.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import javax.persistence.*;/*** @Author moon* @Date 2023/9/26 21:05* @Description*/
@Entity
@Table(name = "category_")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Category {@Id//表明自增长方式@GeneratedValue(strategy = GenerationType.IDENTITY)@Column(name = "id")private Integer id;@Column(name = "name")private String name;
}

CategoryDAO

package com.moon.springboot.dao;import com.moon.springboot.pojo.Category;
import org.springframework.data.jpa.repository.JpaRepository;/*** @Author moon* @Date 2023/9/26 21:18* @Description* 泛型<Category, Integer> 表示这个是针对Category类的DAO,Integer表示主键是Integer类型。* JpaRepository 这个父接口,就提供了CRUD, 分页等等一系列的查询了*/
public interface CategoryDAO extends JpaRepository<Category, Integer> {
}

CategoryService

package com.moon.springboot.service;import com.moon.springboot.pojo.Category;
import com.moon.springboot.util.Page4Navigator;
import org.springframework.data.domain.Pageable;public interface CategoryService {public Page4Navigator<Category> list(Pageable pageable);public void save(Category category);public void delete(int id);public Category get(int id);
}

CategoryServiceImpl

package com.moon.springboot.service.impl;import com.moon.springboot.dao.CategoryDAO;
import com.moon.springboot.pojo.Category;
import com.moon.springboot.service.CategoryService;
import com.moon.springboot.util.Page4Navigator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;@Service
@CacheConfig(cacheNames="category")
public class CategoryServiceImpl implements CategoryService {@AutowiredCategoryDAO categoryDAO;@Override@Cacheable(key="'category '+#p0.offset + '-' + #p0.pageSize ")public Page4Navigator<Category> list(Pageable pageable) {Page<Category> pageFromJPA=  categoryDAO.findAll(pageable);Page4Navigator<Category> page = new Page4Navigator<>(pageFromJPA,5);return page;}@Override@Cacheable(key="'category '+ #p0")public Category get(int id) {Category c =categoryDAO.getOne(id);return c;}@Override@CacheEvict(allEntries=true)
//  @CachePut(key="'category '+ #p0")public void save(Category category) {// TODO Auto-generated method stubcategoryDAO.save(category);}@Override@CacheEvict(allEntries=true)
//  @CacheEvict(key="'category '+ #p0")public void delete(int id) {// TODO Auto-generated method stubcategoryDAO.deleteById(id);}}

Page4Navigator

在执行查询方法时,可以传入一个PageRequest对象,代表进行分页查询。
PageRequest对象代表了查询的条件和约束,通常包含当前页数,每页几条数据。
也可以在分页查询时指定Direction或Sort。

查询的结果为Page对象,包含当前页所及所有数据相关信息。
Page对象常用方法:
getTotalPages() 总共有多少页
getTotalElements() 总共有多少条数据
getNumber() 获取当前页码
getSize() 每页指定有多少元素
getNumberOfElements() 当前页实际有多少元素
hasContent() 当前页是否有数据
getContent() 获取当前页中所有数据(List)
getSort() 获取分页查询排序规则
isFirst() 当前页是否是第一页
isLast() 当前页是否是最后一页
hasPrevious() 是否有上一页
hasNext() 是否有下一页

package com.moon.springboot.util;import java.util.List;import org.springframework.data.domain.Page;public class Page4Navigator<T> {Page<T> page4jpa;//导航分页的页码数int navigatePages;//要打印的总页数int totalPages;int number;long totalElements;int size;int numberOfElements;List<T> content;boolean isHasContent;boolean first;boolean last;boolean isHasNext;boolean isHasPrevious;int[] navigatepageNums;public Page4Navigator() {//这个空的分页是为了 Redis 从 json格式转换为 Page4Navigator 对象而专门提供的}public Page4Navigator(Page<T> page4jpa,int navigatePages) {this.page4jpa = page4jpa;this.navigatePages = navigatePages;totalPages = page4jpa.getTotalPages();number  = page4jpa.getNumber() ;totalElements = page4jpa.getTotalElements();size = page4jpa.getSize();numberOfElements = page4jpa.getNumberOfElements();content = page4jpa.getContent();isHasContent = page4jpa.hasContent();first = page4jpa.isFirst();last = page4jpa.isLast();isHasNext = page4jpa.hasNext();isHasPrevious  = page4jpa.hasPrevious();calcNavigatepageNums();}private void calcNavigatepageNums() {int navigatepageNums[];int totalPages = getTotalPages();int num = getNumber();//当总页数小于或等于导航页码数时if (totalPages <= navigatePages) {navigatepageNums = new int[totalPages];for (int i = 0; i < totalPages; i++) {navigatepageNums[i] = i + 1;}} else { //当总页数大于导航页码数时navigatepageNums = new int[navigatePages];int startNum = num - navigatePages / 2;int endNum = num + navigatePages / 2;if (startNum < 1) {startNum = 1;//(最前navigatePages页for (int i = 0; i < navigatePages; i++) {navigatepageNums[i] = startNum++;}} else if (endNum > totalPages) {endNum = totalPages;//最后navigatePages页for (int i = navigatePages - 1; i >= 0; i--) {navigatepageNums[i] = endNum--;}} else {//所有中间页for (int i = 0; i < navigatePages; i++) {navigatepageNums[i] = startNum++;}}}this.navigatepageNums = navigatepageNums;}public int getNavigatePages() {return navigatePages;}public void setNavigatePages(int navigatePages) {this.navigatePages = navigatePages;}public int getTotalPages() {return totalPages;}public void setTotalPages(int totalPages) {this.totalPages = totalPages;}public int getNumber() {return number;}public void setNumber(int number) {this.number = number;}public long getTotalElements() {return totalElements;}public void setTotalElements(long totalElements) {this.totalElements = totalElements;}public int getSize() {return size;}public void setSize(int size) {this.size = size;}public int getNumberOfElements() {return numberOfElements;}public void setNumberOfElements(int numberOfElements) {this.numberOfElements = numberOfElements;}public List<T> getContent() {return content;}public void setContent(List<T> content) {this.content = content;}public boolean isHasContent() {return isHasContent;}public void setHasContent(boolean isHasContent) {this.isHasContent = isHasContent;}public boolean isFirst() {return first;}public void setFirst(boolean first) {this.first = first;}public boolean isLast() {return last;}public void setLast(boolean last) {this.last = last;}public boolean isHasNext() {return isHasNext;}public void setHasNext(boolean isHasNext) {this.isHasNext = isHasNext;}public boolean isHasPrevious() {return isHasPrevious;}public void setHasPrevious(boolean isHasPrevious) {this.isHasPrevious = isHasPrevious;}public int[] getNavigatepageNums() {return navigatepageNums;}public void setNavigatepageNums(int[] navigatepageNums) {this.navigatepageNums = navigatepageNums;}}

RedisConfig

package com.moon.springboot.config;import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.*;import java.time.Duration;@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {private RedisSerializer<String> keySerializer() {return new StringRedisSerializer();}private RedisSerializer<Object> valueSerializer() {return new GenericJackson2JsonRedisSerializer();//value值使用json序列化器}@Beanpublic CacheManager cacheManager(RedisConnectionFactory factory) {RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();redisCacheConfiguration = redisCacheConfiguration.entryTtl(Duration.ofMinutes(30L))//设置缓存延时时间为30分钟.disableCachingNullValues()//如果是空值,不缓存.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer()))//设置key值序列化.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer()));//设置value值序列化为jsonreturn RedisCacheManager.builder(RedisCacheWriter.nonLockingRedisCacheWriter(factory)).cacheDefaults(redisCacheConfiguration).build();}
}

CategoryController

package com.moon.springboot.web;import com.moon.springboot.pojo.Category;
import com.moon.springboot.service.CategoryService;
import com.moon.springboot.util.Page4Navigator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;@Controller
public class CategoryController {@AutowiredCategoryService categoryService;@RequestMapping("/listCategory")public String listCategory(Model m,@RequestParam(value = "start", defaultValue = "0") int start,@RequestParam(value = "size", defaultValue = "5") int size) throws Exception {start = start<0?0:start;Sort sort = Sort.by(Sort.Direction.DESC, "id");Pageable pageable = PageRequest.of(start, size, sort);Page4Navigator<Category> page =categoryService.list(pageable);m.addAttribute("page", page);return "listCategory";}@RequestMapping("/addCategory")public String addCategory(Category c) throws Exception {categoryService.save(c);return "redirect:listCategory";}@RequestMapping("/deleteCategory")public String deleteCategory(Category c) throws Exception {categoryService.delete(c.getId());return "redirect:listCategory";}@RequestMapping("/updateCategory")public String updateCategory(Category c) throws Exception {categoryService.save(c);return "redirect:listCategory";}@RequestMapping("/editCategory")public String editCategory(int id,Model m) throws Exception {Category c= categoryService.get(id);m.addAttribute("c", c);return "editCategory";}
}

HelloController

package com.moon.springboot.web;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.text.DateFormat;
import java.util.Date;/*** @Author moon* @Date 2023/9/25 21:17* @Description*/@Controller
public class HelloController {@RequestMapping("/hello")public String hello(Model model) {model.addAttribute("now", DateFormat.getDateTimeInstance().format(new Date()));return "hello";}@RequestMapping("/hello_1")public String hello_1(Model model) {model.addAttribute("now", DateFormat.getDateTimeInstance().format(new Date()));return "hello_1";}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/161184.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

剑指JUC原理-12.手写简易版线程池思路

&#x1f44f;作者简介&#xff1a;大家好&#xff0c;我是爱吃芝士的土豆倪&#xff0c;24届校招生Java选手&#xff0c;很高兴认识大家&#x1f4d5;系列专栏&#xff1a;Spring源码、JUC源码&#x1f525;如果感觉博主的文章还不错的话&#xff0c;请&#x1f44d;三连支持&…

Ubuntu20.04搭建RISC-V和qemu环境

1. 前言 risc-v是一个非常有潜力的指令集框架&#xff0c;最近对其产生了浓厚的兴趣&#xff0c;由于之前对于这方面的知识储备很少&#xff0c;在加上网上的教程都是点到为止&#xff0c;所以安装过程异常曲折。好在最后一步一步积累摸索&#xff0c;终于利用源码安装完成。看…

【ElasticSearch系列-05】SpringBoot整合elasticSearch

ElasticSearch系列整体栏目 内容链接地址【一】ElasticSearch下载和安装https://zhenghuisheng.blog.csdn.net/article/details/129260827【二】ElasticSearch概念和基本操作https://blog.csdn.net/zhenghuishengq/article/details/134121631【三】ElasticSearch的高级查询Quer…

LeetCode热题100——链表

链表 1. 相交链表2. 反转链表3. 回文链表4. 环形链表5. 合并两个有序链表 1. 相交链表 给你两个单链表的头节点 headA 和 headB &#xff0c;请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点&#xff0c;返回 null 。 // 题解&#xff1a;使用A/B循环遍…

csapp bomb lab part4

csapp bomb lab part4 phase 5 是一个循环&#xff0c;不断累加&#xff0c;访问的地址基于0x4024b0, phase 6 之后更新 汇编 地址计算 寄存器 cl 是 x86 汇编语言中的一个 8 位寄存器&#xff0c;它是 ecx 寄存器的低位部分。 具体来说&#xff0c;x86 架构中的寄存器可…

植物花粉深度学习图片数据集大合集

最近收集了一波有关于植物花粉的图片数据集&#xff0c;可以用于相关深度学习模型的搭建&#xff0c;废话不多说&#xff0c;上数据集&#xff01;&#xff01;&#xff01; 1、23种花粉类型805张花粉图像数据集 关于此数据&#xff1a;花粉种类和类型的分类是法医抱粉学、考…

NTFS文件系统解析(三)

1、引言 对于NTFS文件系统而言&#xff0c;无论文件内容本身&#xff0c;抑或真实的文件属性&#xff0c;都被称之为属性。 而正如前文说到的&#xff0c;NTFS预定义了16种属性用于文件系统的管理。 而通常情况下&#xff0c;往往只需要关注其中的某些属性即可。 2、属性头 …

使用 Python 进行自然语言处理第 4 部分:文本表示

一、说明 本文是在 2023 年 3 月为 WomenWhoCode 数据科学跟踪活动发表的系列文章中。早期的文章位于&#xff1a;第 1 部分&#xff08;涵盖 NLP 简介&#xff09;、第 2 部分&#xff08;涵盖 NLTK 和 SpaCy 库&#xff09;、第 2 部分&#xff08;涵盖NLTK和SpaCy库&#xf…

Flask——接口路由技术

接口路由技术 一、Flask 简介1、环境安装&#xff1a;2、一个最小的应用3、两种运行方式 二、定义路由1、普通路由2、动态路由3、限定类型4、地址尾部的“/” 三、请求与响应-请求方法四、请求与响应-处理请求数据1、request的常用属性/方法2、get 请求参数3、json 请求4、表单…

手把手教程 | YOLOv8-seg训练自己的分割数据集

&#x1f680;&#x1f680;&#x1f680;手把手教程&#xff1a;教会你如何使用自己的数据集开展分割任务 &#x1f680;&#x1f680;&#x1f680;YOLOv8-seg创新专栏&#xff1a;http://t.csdnimg.cn/KLSdv 学姐带你学习YOLOv8&#xff0c;从入门到创新&#xff0c;轻轻松…

Bytedance揭秘OpenAI大模型: GPT-3到GPT-4进化路径

文章目录 探秘GPT-3到GPT-4进化之路1、SFT&#xff1a;早期GPT进化的推动者2、RLHF和SFT&#xff1a;编码能力提升的功臣3、代码加入预训练&#xff0c;对推理帮助最大4、“跷跷板”现象 论文地址项目链接Reference GPT-Fathom: Benchmarking Large Language Models to Deciphe…

Angular异步数据流编程

1 目前常见的异步编程的几种方法 首先给出一个异步请求的实例&#xff1a; import {Injectable} from angular/core;Injectable({providedIn: root }) export class RequestServiceService {constructor() {}getData() {setTimeout(() > {let res zhaoshuai-lcreturn res…