当前位置:网站首页>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
边栏推荐
- 2022-02-28(2)
- All inherited features
- 206.反转链表
- Go OS module
- How to use the transaction code sat to find the name of the background storage database table corresponding to a sapgui screen field
- 超標量處理器設計 姚永斌 第2章 Cache --2.4 小節摘錄
- RPA+低代码为何是加速财务数字化转型之利器?
- 华为设备配置H-VPN
- [v2.1] automatic update system based on motion step API (repair bug, increase completion display, support disconnection reconnection and data compensation)
- Cdr2022 serial number coreldraw2022 green key
猜你喜欢
随机推荐
C language to achieve eight sorts (2)
Internet of things development practice 18 scenario linkage: how does an intelligent light perceive light? (I) (learning notes)
The upcoming launch of the industry's first retail digital innovation white paper unlocks the secret of full link digital success
类和对象(4)
C语言实现迷宫问题
ESP32C3 Arduino库使用方法
R language book learning 03 "in simple terms R language data analysis" - Chapter 8 logistic regression model Chapter 9 clustering model
快速排序的三种方法
自定义实现offsetof
科普 | NFT的类型有哪些(上)
即将首发 | 业界首个零售数字化创新白皮书,解锁全链路数字化致胜秘籍
Optimization of quick sort
C语言实现八种排序(1)
Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience
Static PVC with CEPH CSI
Popular science | what are the types of NFT (Part 1)
Superscalar processor design yaoyongbin Chapter 2 cache -- Excerpt from subsection 2.4
Implementation stack and queue
为什么需要微服务
Builder pattern








