当前位置:网站首页>Summary of common paging methods
Summary of common paging methods
2022-06-11 22:02:00 【Roman Sultan Mohammed】
Record some paging methods in the work
1. paging
Memory paging is the paging of data during program operation , Generally, it is first found out from the database and then paged .
The disadvantage is that the memory consumption is large , The advantage is that the method is simple
Here are some of my favorite Java Stream operation paging and C# Of LINQ Pagination
Java Stream operation paging
public class Test1 {
public static List<Student> InitData(){
List list = new ArrayList<Student>();
list.add(new Student(1, " Huang Jianxiong ", " Nancheng "));
list.add(new Student(2, "KKK", "qw"));
list.add(new Student(3, " Lichengqi ", " jiangsu "));
list.add(new Student(1, "QQQ", " guangdong "));
return list;
}
public static void main(String[] args) {
List<Student> list = InitData();
int pageIndex = 2,pageSize = 3;
List<Student> pageOne = list.stream().skip(--pageIndex*pageSize).limit(pageSize).collect(Collectors.toList());
pageOne.forEach(item->{
System.out.println(item);});
}
public static class Student{
private int Id;
private String Name;
private String Address;
@Override
public String toString() {
return "Student [Id=" + Id + ", Name=" + Name + ", Address=" + Address + "]";
}
public Student(int id, String name, String address) {
super();
Id = id;
Name = name;
Address = address;
}
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
}
}
In short, it can be summarized as
List newlist = list.stream().skip(--pageIndex*pageSize).limit(pageSize).collect(Collectors.toList());
there pageIndex Is the page number ,pageSize Is the number of entries on a page , Of course you must provide the total number of records Count, How many pages are there .
LINQ It's easier
public static void main(string[] args){
List<SimpleModel> list = new List<SimpleModel>()
{
new SimpleModel(){
id = 1,name = "ss1",address = "q1"},
new SimpleModel(){
id = 2,name = "ss2",address = "q2"},
new SimpleModel(){
id = 3,name = "ss3",address = "q3"},
new SimpleModel(){
id = 4,name = "ss4",address = "q4"},
new SimpleModel(){
id = 5,name = "ss5",address = "q5"}
};
int PageIndex = 2, PageSize = 2;
List<SimpleModel> afterPage = list.Skip(--PageIndex*PageSize).Take(PageSize).ToList();
afterPage.ForEach(item => {
Console.WriteLine(item); });
}
internal class SimpleModel
{
public int id {
get; set; }
public string name {
get; set; }
public string address {
get; set; }
public override string ToString()
{
return "[id:"+this.id+" name: "+this.name+" address: "+this.address+"]";
}
}
2.Sql Pagination
Sql Pagination is in sql Statement , Only a few records can be found in the database at a time
advantage : Memory overhead is small , shortcoming : Different database paging sql Each are not identical
Oracle:
oracle You can use your own rownum
select * from (
select t.*,rownum rn from XXX t where 1=1 and rownum <= pageIndex*pageSize
) where rn >= (pageIndex-1)*pageSzie + 1
Of course , You can also use the windowing function to complete , The effect is the same
MySql:
Through the limit Keywords can be easily paged
Select * from XXX [Where xxx] [order by xxx] limit (pageIndex-1)*pageSize,pageSize
SqlServer:
Generally speaking, use top sentence , It can be
select top pageSize *
from R_ASSET_DETAIL_T
where id not in
(
--40 That's how it's calculated :10*(5-1)
select top (pageIndex-1)*pageSize id from R_ASSET_DETAIL_T order by ID
)
order by id
There is, of course, another way ,OFFSET and FETCH, But this approach requires a higher version of SqlServer
https://docs.microsoft.com/zh-cn/sql/t-sql/queries/select-order-by-clause-transact-sql?view=sql-server-ver16
边栏推荐
- Take off efficiently! Can it be developed like this?
- R language book learning 03 "in simple terms R language data analysis" - Chapter 7 linear regression model
- Relatively perfect singleton mode
- 类和对象(1)
- zypper命令使用示例
- In the future, cloud expansion technology is expected to be selected as a specialized, special and new enterprise in Shanghai
- Top - K problem
- Non recursive writing of quick sort
- R语言相关文章、文献整理合集(持续更新)
- Matlab: 文件夹锁定问题的解决
猜你喜欢

科普 | NFT的类型有哪些(上)

189. 轮转数组

5.学城项目 支付宝支付

LaTex实战笔记 3-宏包与控制命令
![[v2.1] automatic update system based on motion step API (repair bug, increase completion display, support disconnection reconnection and data compensation)](/img/73/2ec957d58616d692e571a70826787f.jpg)
[v2.1] automatic update system based on motion step API (repair bug, increase completion display, support disconnection reconnection and data compensation)

206. reverse linked list

【LeetCode】11. Container with the most water

6.项目上线

Daily question - Roman numeral to integer

Learning bit segment (1)
随机推荐
Nmap进行主机探测出现网段IP全部存活情况分析
类和对象(4)
Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience
How to view the installation date of the win system
[niuke.com] DP30 [template] 01 Backpack
Uncover the secret of the popular app. Why is it so black
超標量處理器設計 姚永斌 第2章 Cache --2.4 小節摘錄
实现栈和队列
相对完善的单例模式
大学三年应该这样过
EndnoteX9简介及基本教程使用说明
学习位段(1)
RPA super automation | nongnongji and cloud expansion accelerate financial intelligent operation
Go IO module
Superscalar processor design yaoyongbin Chapter 2 cache -- Excerpt from subsection 2.4
MySQL事务简介
Maze problem in C language
Go OS module
inner join执行计划变了
Parker plunger pump pv180r1k1t1nmmc