首页 > 编程知识 正文

mysql如何实现分页,oracle分页查询语句

时间:2023-05-04 08:08:07 阅读:62126 作者:1643

有爱心的JPA

平时写小项目的时候,喜欢引用Spring Data Jpa,其实他写代码还是很快~

在日常开发工作中,分页列表查询基本上随处可见。 让我们看看如何使用jpa进行多条件查询和查询列表分页吗?

关于使用JPA

以下两个步骤简要介绍了jpa的使用。 详细情况请由合作伙伴自己搜索吧~

1、引入依赖

org.springframework.boot

spring-boot-starter-data-jpa

mysql、web、druid ……

2、配置yml

图对直接粘贴代码很有用:

spring:

#数据源

datasource:

URL : JDBC : MySQL ://127.0.0.1:3306/tmax? use unicode=truewqdls=utf-8 use SSL=false

username :根

password:1234

type : com.Alibaba.druid.pool.druid data source

driver class name : com.MySQL.JDBC.driver

jpa:

#在数据库操作时显示sql语句

show-SQL :真

#自动生成表结构

generate-DDL :真

hibernate:

DDL-auto :无

database-platform : org.hibernate.dialect.MySQL 57 dialect

分页查询

我知道jpa基本上不需要写sql,只要继承jpa资料档案库就可以了。 同样,它也提供了分页查询的方法。 “补充:使用分页查询必须同时继承

示例:

pagefindbycondition (searchvosearchvo,Pageablepageable;

通过传递基于pageale协议的对象来获取页面的数据,从源代码的角度来看,Pageable是一个接口,它提供了对页数、每页多少记录、排序信息等一系列方法进行分页的声明部分方法如下。

intgetPageNumber;

intgetPageSize (

intgetOffset (;

SortgetSort (

Pageablenext (;

PageablepreviousOrFirst (

Pageablefirst (;

booleanhasPrevious (;

可以用这些方法构建我们的pageable对象。 请注意,jpa在构建页码初始时从0开始。

不要乱说,看看代码吧:

1. impl

@Override

publicpagefindbycondition (videocategoryvideocategory,SearchVosearchVo,Pageablepageable ) {

returnvideocategorydao.find all (新规范() )。

@Nullable

@Override

publicpredicatetopredicate (根路由、CriteriaQuerycq、CriteriaBuildercb ) {

//添加其他搜索筛选器条件默认情况下也有创建时间筛选器

pathcreatetimefield=root.get (createtime );

pathcategoryidfield=root.get (categoryid );

列表列表=new ArrayList (;

//创建时间

if (str util.is not blank (search VO.get start date () ) str util.is not blank (search VO.get end date ) ) ) }

数据开始=date util.parse (search VO.get start date ();

dateend=date util.parse (search VO.get end date ();

list.add (CB.between (创建字段,开始,日期util.endo fday (结束) )

}

//视频分类

if (str util.is not blank (视频类别. get category () ) ) ) 0

list.add (CB.equal (categoryid field,视频category.get categoryid () );

}

predicate [ ] arr=new predicate [ list.size (];

CQ.where(list.toArray ) (arr );

返回空值;

}

、pageable;

}

2 .控制器

@ request mapping (value='/getbycondition ',method=RequestMethod.GET ) )。

@ API操作(value='获取多条件页') )。

publicResultgetByCondition (

@ modelattributevideocategoryvideocategory,

@ modelattributesearchvosearchvo,

@ModelAttributePageVopageVo

page page=videocategoryservice.findbycondition (视频类别,searchVo,pageutil.initpage ) pageVO );

returnnewResultUtil ().setdata ) ) page;

}

3. PageUtil

publicstaticpageableinitpage (pagevopage ) {

可分页分页=null;

intpage number=page.getpage number (;

intpageSize=page.getPageSize (;

Stringsort=page.getSort (;

Stringorder=page.getOrder (;

if (页面编号1 ) {

页面编号=1;

}

if(pagesize1) {

pageSize=10;

}

if(strutil.isnotblank(sort ) ) )

Sort.Directiond;

if(strutil.isblank(order ) ) }

d=Sort.Direction.DESC;

}else{

d=sort.direction.value of (order.toupper case ) );

}

sorts=newsort(d,sort );

pageable=page request.of (page number-1,pageSize,s );

}else{

pageable=page request.of (page number-1,pageSize );

}

返回页面;

}

转载来源: https://www.cn blogs.com/nice yoo/p/10817290.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。