当前位置:网站首页>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

原网站

版权声明
本文为[Roman Sultan Mohammed]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112142541144.html