SpringData JPA分页查询

SpringBoot集成Jpa对数据进行排序、分页、条件查询和过滤 NotIn:等价于 SQL 中的 "not in",findByNameNotIn(Collection userList) ,方法的参数可以是 Collection 类型,也可以是数组或者不定长参数;In:等价于 SQL 中的 "in",findByIdIn(Collection userIdList) ,方法的参数可以是 Collection 类型,也可以是数组或者不定长参数;也可以使用findAllBy。JPA为我们提供了分页的方法,我们可以查看接口集成的JpaRepository接口的关系图。 阅读详情

首先我们需要知道SpringData JPA 的几个接口





其实看名字就大概懂了,也可以很方便的使用

首先我们的持久化层继承JpaRepository,相当于继承了增删改查的持久化层以及分页查询的持久化层

所以如果我们要使用分页查询 ,我们只需要直接调用


由一开始的图也可以看到Pageable的其中一个实现,直接新建一个对象传给你的持久层即可

下面是一个小Demo的例子

package Dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import Entity.Student;

//不需要使用事物注解,因为Spring已经默认帮我们开启 当然我们可以利用注解进行额外的设置
//对数据进行除了查询之外我们都需要加入事物注解不然报错 这是我个人测试
public interface StudentDao extends JpaRepository,CommonDao{
	
	@Query("from Student s where s.sid = ?1")
	Student findById(String id);
//	两种写法一样
//	@Query("select * from Student s where s.sid = :id")
//	Student findById(@Param("id")String id);
	
	@Transactional
	@Query("delete from Student s where s.sid = ?1")
	@Modifying//更新或者删除需要额外加这个注解
	void deleteById(String id);
	
	//父类方法直接调用 findAll(Pageable pageable)
}


//Service 层
public Page findAll(Pageable pageable) {
		// TODO Auto-generated method stub
		return studentDao.findAll(pageable);
	}

//Controller
@RequestMapping("/findAll")
	public Page findAll(@RequestParam("f")int pageNum,@RequestParam("m")int size){
		return studentService.findAll(new PageRequest(pageNum, size));
	}
下面是返回结果:

{"content":[{"sid":"1","sname":"sam","sage":1},{"sid":"2","sname":"2","sage":2},{"sid":"3","sname":"3","sage":3}],"totalPages":1,"totalElements":3,"last":true,"number":0,"size":20,"numberOfElements":3,"sort":null,"first":true}

Springboot 快速学会使用 JPA 分页查询 如果还不知道springboot怎么使用JPA的,先参考这篇 Springboot快速整合JPA实现增删查改:https://blog.youkuaiyun.com/qq_35387940/article/details/102541311 接下来回到正题 示例场景,将下面表数据分页查询出来: 简单模拟, mapper: @Query(value="select * from u... 阅读详情

相关推荐

Spring Data JPA 分页接口的使用方法——PageHelper分页查询方法

2017年,全国IT企业纷纷转向云计算、容器技术,并掀起了一股“容器潮流”。容器化、微服务架构以及分布式系统架构模式越来越流行,对于大型复杂系统而言,数据库分页查询在性能上就成为一个重要的优化点。基于此,当下互联网中最流行的开源框架之一, MyBatis 和 SpringData JPA 等都提供了分页查询功能。PageHelper 是 MyBatis 中的一个分页插件,作用是在不用改变 SQL 查询的前提下,实现自定义物理分页,非常方便开发者使用。

AI天才研究院 3265

JPA三种分页查询

接触Jpa已经有一段时间,从陌生到运用自如。这篇文章便是讲解自己在实际工作中所运用到的三种分页条件查询方法,希望对你有用。

小小小啊伟的博客 2万+

ComfyUI 的 LoRA 训练:秋叶 LoRA-scripts 参数设置与测试方法详解

秋叶 LoRA-scripts 的参数设置需结合理论(如模型原理、优化器机制)和硬件条件(显存、算力),新手可先按推荐值起步,再逐步调整。测试时,loss 值帮你判断训练是否正常,XYZ 图表法帮你找到最佳使用参数。掌握这些知识,你就能避开 LoRA 训练的常见坑,从 “盲目调参” 变为 “精准训练”。

2301_78150483的博客 2402

SpringDataJPA(二)分页查询与动态条件

详细介绍 Spring Data JPA 的分页查询、动态条件、批处理等高级进阶使用

这里是阿安的博客 3663

EEG/ERP新手必看:5本神经科学入门书籍推荐(附高清PDF下载)

本文为EEG/ERP研究新手推荐5本神经科学入门书籍,涵盖认知神经科学、数据分析、ERP技术等核心内容。特别推荐《认知神经科学—关于心智的生物学》和《事件相关电位基础》,这些书籍不仅提供理论框架,还包含实用案例和MATLAB代码,帮助研究者快速掌握脑电研究技术。附高清PDF下载,适合实验室小组学习。

weixin_29210727的博客 446

哈工大2024春大数据挖掘期末考试回忆版试题

(3)现有一个用户数量远多于物品(还是物品远多于用户,忘了是哪个了)的情景,应该使用基于user-user的推荐系统还是item-itmen的推荐系统,说明原因。(1)L是n*n拉普拉斯矩阵,x是一个n*1列向量,证明对任意的x,(x)^TLx>=0(6分)(3)证明:p到o的K-可达距离,至少是o的K-distance(2分)(2)(1)的结果说明竞争率的上界是多少?(2)考Gain的意义,如何利用Gain选择划分点(2分)(2)说明如何利用划分后的数据进行验证(2分)(2)k可达距离是对称的吗?

ymd2002的博客 1891

SpringData JPA @Query分页查询 Cannot use native queries with dynamic sorting and/or pagination in method

异常信息:Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'newsDao': Invocation of init method failed; nested exception is org.springframework.data.jpa.repo

IT打工匠 4574

MIC卡的OFED驱动安装

 IB网络和OFED驱动安装 IB InfiniBand是一个统一的互联结构,可以处理存储I/O、网络I/O和进程间通信(IPC)。它可以将磁盘阵列、SANs、LANs、服务器和集群服务器进行互联,也可以连接外部网络(比如WAN、VPN、互联网)。设计InfiniBand的目的主要是用于企业数据中心,大型的或小型的。目标主要是实现高的可靠性、可用性、可扩展性和高的性能。Inf

beglorious的专栏 5106

SpringData JPA条件查询、排序、分页查询

Spring Data JPA是spring提供的一款对于数据访问层(Dao层)的框架,使用Spring Data JPA,只需要按照框架的规范提供dao接口,不需要实现类就可以完成数据库的增删改查、分页查询等方法的定义,极大的简化了我们的开发过程。

Veggie的博客 1万+

手把手教你学Simulink--进阶电机控制场景实例:基于Simulink的直流电机双闭环调速系统抗负载扰动仿真

摘要:本文基于Simulink平台构建直流电机双闭环调速系统,重点研究其在工业传送带场景下的抗负载扰动性能。系统采用转速外环(稳速)和电流内环(限流)的嵌套结构,通过工程法整定PI参数,实现了转速跌落<10%、恢复时间<0.5s的优异抗扰性能。仿真结果表明,相比单闭环系统,双闭环控制使转速跌落率降低47%,恢复时间缩短75%。文中详细介绍了数学模型建立、Simulink模块实现及参数优化方法,并提供了完整代码和模型文件,为电机控制系统设计提供实用参考。

MHD0815的博客 179

基于 Langchain 开发一个完整的支持多轮数据分析(直连 mysql 数据,多表综合分析 SQL 取数,数据分析,报表生成)ChatBI 系统(前后端完整实现源代码)

ChatBI系统开发摘要 本文介绍了基于Langchain的多轮数据分析ChatBI系统开发方案。该系统支持MySQL数据库连接,提供多表分析、SQL查询、数据分析和报表生成功能。系统采用前后端分离架构:前端使用React+Ant Design+ECharts实现交互界面;后端基于FastAPI搭建服务,集成Langchain处理自然语言到SQL的转换;核心技术栈还包括SQLAlchemy、Pandas和Matplotlib。 系统核心模块包括: 数据库连接层:使用SQLAlchemy管理MySQL连接 L

AI天才研究院 816

springdata jpa Example、Pageable分页查询使用

springdata jpa Example、Pageable分页查询使用 ExampleMatcher实例查询三要素 实体对象:在ORM框架中与Table对应的域对象,一个对象代表数据库表中的一条记录,如上例中User对象,对应user表。在构建查询条件时,一个实体对象代表的是查询条件中的“数值”部分。如:要查询姓“X”的客户,实体对象只需要存储条件值“X”。 匹配器:Examp...

你当像鸟飞往你的山~~ 5319

Python 爬虫实战:亚马逊 + eBay 商品信息跨平台抓取(价格 / 销量 / 评价对比)

亚马逊和 eBay 是全球知名的电商平台,拥有海量的商品信息。对于消费者来说,比较不同平台上的商品价格、销量和用户评价可以帮助他们找到性价比最高的商品。对于商家而言,了解竞争对手的商品信息有助于优化自身的产品策略和定价策略。然而,手动在两个平台上逐一比较商品信息是非常耗时且低效的。因此,开发一个能够自动抓取并对比这两个平台商品信息的爬虫工具具有重要的实际意义。

yansideyucsdn的博客 617

Springdata jpa 的分页查询

  之前用springdata jap做分页,花费了很多时间,最后在网上找了一些资料,现在分享出来,希望对大家有一些帮助; 直接上代码: 1、Controller部分省略。。。。 service: Pageable pageable = new PageRequest(studentQuery.getPage() - 1, studentQuery.getRows(), Sort.D...

农民娃儿的博客 509

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

springdata jpa带模糊条件分页查询

Dao层 public interface BookDao extends JpaRepository<Book,Integer>, JpaSpecificationExecutor<Book> { } serviceImpl实现类的代码 @Autowired BookDao bookDao; /** * jap带单个条件的分页查询 * @param queryPageBean * @return */ @O

军大君的博客 823

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

springBoot整合springdata Jpa的基本使用以及带条件的分页查询

一、springBoot整合springData Jpa 在application.properties中声明启用如下: spring.datasource.name=root spring.datasource.password=123456 spring.datasource.url=jdbc:mysql://localhost:3306/mst?useSSL=false spring....

pycharm_u的博客 2720

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

SpringData JPA联表分页查询需要使用countQuery属性

定义一个特殊的count查询,用于分页查询时,查找页面元素的总个数。如果没有配置,将根据方法名派生一个count查询。属性中与计数查询一起提供。此查询在结果查询之后单独执行,其结果用于填充返回的 Page 的 totalCount 属性。countQuery用于分页,如果要求分页结果返回正确的总计数,属性计算出总数的话,会在查询的时候进行报错。进行分页查询的时候,如果不是用。...

叉叉不低头 3320

Spring Data JPA分页查询

使用Spring Data JPA的朋友,在实际工作中经常需要用到分页查询。下面介绍一个简单的分页查询的例子:查询学生信息,每页10行数据,并按成绩排序。先看数据表:   实现:repo需要继承PagingAndSortingRepository,如下: public interface StudentRepo extends CrudRepository&lt;StudentEntity...

liuyunyihao的博客 3407

spring data jpa实现有条件的分页查询功能

spring data jpa实现有条件的分页查询功能 前端部分代码。发送请求: $('#grid').datagrid({ iconCls: 'icon-forward', fit: true, border: false, rownumbers: true, striped: true, pageList: [30, 50, 10

学亮编程手记 1万+

java+jpa+多条件查询_java相关:Spring Data JPA 复杂/多条件组合分页查询

java相关:Spring Data JPA 复杂/多条件组合分页查询发布于 2020-6-25|复制链接本文主要介绍了Spring Data JPA 复杂/多条件组合分页查询的相关资料。具有很好的参考价值。下面跟着小妖一起来看下吧话不多说,请看代码:```javapublic Map getWeeklyBySearch(final Map serArgs,String pageNum, Stri...

weixin_42299140的博客 259

SpringData JPA查询分页demo

SpringData JPA 的 PagingAndSortingRepository接口已经提供了对分页的支持,查询的时候我们只需要传入一个 org.springframework.data.domain.Pageable 接口的实现类,指定PageNumber和pageSize即可 springData包中的 PageRequest类已经实现了Pageable接口,我们可以直接使用下边是部

maguanghui_2012的专栏 777

spring Data Jpa的分页管理

通过Jpa大大简化了我们对数据库的开发工作。但是,之前的例子中我们只提到了最简单的CRUD(增删改查)操作。实际上,Spring Data Jpa对于分页以及排序的查询也有着完美的支持,接下来,我们来学习如何通过Pageable来对数据库进行分页查询。 一 Pageable 是Spring Data库中定义的一个接口,该接口是所有分页相关信息的一个抽象,通过该接口,我们可以得到和分页相关所有信...

weixin_41049260的博客 376

SpringData JPA @Query分页查询Exception问题解决方案

Caused by: org.springframework.data.jpa.repository.query.InvalidJpaQueryMethodException: Cannot use native queries with dynamic sorting and/or pagination in method public abstract org.springframework.

爱编程~爱大牛~ 8725

SpringData JPA实现单表多条件分页查询

public Page findDeviceByParams(int pageNo, int pageSize, String rIndexCode, String name, String parentDevice) { try{ Specification specification = new Specification(...

weixin_38170137的博客 507

spring data jpa实现分页查询功能

spring data jpa实现分页查询功能 HTML代码部分: // 收派标准信息表格 $('#grid').datagrid( { iconCls : 'icon-forward', fit : true, border : false, rownumbers : true, striped : true, pageList:

学亮编程手记 2739
上一篇: spring data jpa 定义全局接口BaseDao
下一篇: spring data jpa使用二级缓存
SnakeMoving
博客等级 码龄11年 65粉丝 97原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值