Mysql在分頁過程中有新的資料插入造成重複資料

2021-09-25 11:30:33 字數 4170 閱讀 8351

在使用mysql實現分頁時,前端一般傳遞分頁引數給後端,後端在把分頁列表資料給前端進行展示。這思想沒問題。都是這個套路,根據不同的問題,編寫不同的**。

傳統分頁就是在資料基本不會變化時,就是不會有新資料插入進來,前端一般 是傳遞 頁碼,每一頁的數量,**如下

@data

public class pageentity implements serializable

public responsedata list(pageentity pageentity)

int shopid = 1;

pageutils pageutils = this.productservice.wxlistproductpage(shopid, pageentity);

return responsedata.success(pageutils);

}

@override

public pageutils wxlistproductpage(int shopid, pageentity pageentity)

//上面是需要對資料進行篩選

//下面是分頁引數的計算

//mysql的limit的第乙個值

int start = (pageentity.getpage() - 1) * pageentity.getlimit();

//mysql的limit的第二個值

int num = pageentity.getlimit();

return new pageutils(maplist, count, pageentity.getlimit(), pageentity.getpage());

}

select

a.id as id,

c.title as categoryname,

a.product_name as productname,

a.num as num,

a.price as price,

a.product_desc as productdesc,

a.create_time as createtime,

a.update_time as updatetime,

a.enable_point as enablepoint,

a.point as point,

b.file_name as filename

from product a

left join sys_file_info b on a.img = b.file_id

left join product_category c on a.product_category_id = c.id

where

a.shop_id = #

and a.product_state = 'enable'

and product_name like concat('%',#,'%')

order by

a.create_time desc

limit #, #

上面就是傳統分頁的實現過程,我這是真實專案裡面的一部分**,所以有很多對與本次演示不相關的**。

而實際情況是後端資料庫裡面會對我要查詢的這張表會經常有新資料產生,而在產生新資料後新的資料會影響到我們分頁的查詢結果,在查詢結果中會出現重複資料,例如下圖

第一頁的資料

第二頁的資料

上述的內容第一頁的最後一條出現在了第二頁的第一條,就是因為資料庫新插入了一條資料,而這條資料影響了我們後台分頁的請求引數的計算。

因此,為了不影響使用者體驗,減少bug,提高產品質量,我們需要修改**,然新產生的資料不會對本階段的分頁資料產生影響,需要獲取新產生的資料需要重新整理從第一頁開始取值就可以。每當我開始請求第二頁時就不要出現重複資料了。

動態分頁的實現過程如下

/**

* 在分頁時過濾新插入的資料

* @param headerid 第一次請求下列表中最大的id

* @param nextid 每一次請求下列表中最小的id

* @param limit 每一頁需要多少條

* @param page 頁碼,後端無意義

* @return

*/public responsedata listbyid(integer headerid, integer nextid, integer limit, integer page)

if (page == null)

int shopid = 1;

pageutils pageutils = this.productservice.wxlistproductpagebyid(shopid, headerid, nextid, limit, page);

return responsedata.success(pageutils);

}

@override

public pageutils wxlistproductpagebyid(integer shopid, integer headerid, integer nextid,integer limit,integer page)

return new pageutils(maplist, count, limit, page);

}

select

a.id as id,

c.title as categoryname,

a.product_name as productname,

a.num as num,

a.price as price,

a.product_desc as productdesc,

a.create_time as createtime,

a.update_time as updatetime,

a.enable_point as enablepoint,

a.point as point,

b.file_name as filename

from

product a

left join sys_file_info b on a.img = b.file_id

left join product_category c on a.product_category_id = c.id

where

a.shop_id = #

and a.product_state = 'enable'

and a.id < #

order by a.id desc

limit #, #

我這裡使用的是自增長id,因此我通過id可以實現,也可以設計字段插入時間,通過插入時間排序後,比較時間段來篩選資料也可以。

最後,貼一下pageutils類的**

/**

* 分頁工具類

* */

public class pageutils implements serializable

/*** 分頁

*/public pageutils(ipage> page)

public int gettotalcount()

public void settotalcount(int totalcount)

public int getpagesize()

public void setpagesize(int pagesize)

public int gettotalpage()

public void settotalpage(int totalpage)

public int getcurrpage()

public void setcurrpage(int currpage)

public list> getlist()

public void setlist(list> list)

}

在儲存過程中分頁

用儲存過程實現的分頁程式 鄒建 2003.09 引用請保留此資訊 呼叫示例 exec p show 地區資料 exec p show 地區資料 5,3,地區編號,地區名稱,助記碼 地區編號 if exists select from dbo.sysobjects where id object id...

在儲存過程中排序分頁

在專案中,會遇到查詢出的結果排序分頁的問題,可以有多種解決方法,乙個是利用開發工具提供的方法,例如.net visual studio中的gridview,就提供了排序,分頁功能。但如果情況複雜,我們可以把排序分頁放到儲存過程來完成。以乙個簡單例子來說明問題 table reportfile fie...

SQL server中學習過程中有用的小知識點

1 在sqlserver 中查詢某個字段存在於那個表中的語句是 use testgo select a.name as tablename from sysobjects a inner join syscolumns b on a.id b.id where b.name 列名 例句 use di...