使用Maria DB

时间:2019-03-14 01:13:19

标签: mariadb spring-mybatis

我的网站是由Spring框架创建的。
并使用Mybatis。

在我的网站上有公告栏页面。
这是板桌。

TB_BOARD

seq(pk), user_seq, title, category, content, reg_time

现在我要应用分页。
所以我检查了Maria DB文件。
他们说使用pk而不是offset。
好的,我知道..但是有问题。

如果我的公告板页面上只有上一个和下一个按钮,那就没有问题。
我将这样写我的SQL:

下一个

select * from TB_BOARD where seq < #{last_seq} order by seq desc limit 5

previos

select * from TB_BOARD where seq >= #{last_seq} order by seq desc limit 5

但是如果公告页上有一个编号的分页按钮?
[1] [2] [3] [4] [5]

有人点击了[4]按钮。如何获取[4]按钮的记录?
我认为记录可以更改(更新,删除),seq将不再是连续的。
因此无法预测。

没有“ select @rownum”,有没有办法做到这一点?
谢谢。

1 个答案:

答案 0 :(得分:1)

The 'previos' query needs ASC, not DESC if I understand seq correctly.

If you are going to have [1][2]... with all pages possible, you are asking for trouble. I will assume you list only the next and previous, say, 5 pages. That is, if you are on page 13, the buttons will show 8..18. (Plus, perhaps, [first] and [last].

You must not use a hard-coded "sequence" number. Instead, use a datetime or other thing that has the same effect

As for INSERTs and DELETEs, "next" and "previous" can be made to work "correctly", but jumping to an arbitrary page is ambiguous because of changes that could have happened.

3 pages head of the current one is seq < $this ORDER BY seq DESC LIMIT 15,5 (or something like that.

More on pagination here.