web项目阅读文章的显示上一页下一页
笔者在写文章的上一篇下一篇时很苦恼,真不知道怎么写才能最省事一下便是我写的认为最省事的方式
在Controller层获取到上一篇下一篇的文章id,title等等
List<Article> list = articleBiz.getListsxyt(id);
if(list!=null){
if(list.size()==2){
for (Article a2 : list) {
int id2 = a2.getId();
if(id2 <id){
xyt =a2;
}
if(id2>id){
syt = a2;
}
}
}else if(list.size()==1){
for (Article article2 : list) {
if(article2.getId()<id){
xyt = article2;
}else{
syt = article2;
}
}
}
}
if(syt!=null){
String sytHtml="<span><font color='red'>上一条</font><a href='"+request.getRequestURI()+"?id="+syt.getId()+"'>"+syt.getTitle()+"</a></span>";
model.addAttribute("sytHtml",sytHtml);
}else{
String sytHtml="<span><font color='red'>上一条</font><a href='javascript:void(0);'>无</a></span>";
model.addAttribute("sytHtml",sytHtml);
}
if(xyt!=null){
String xytHtml="<span><font color='red'>下一条</font><a href='"+request.getRequestURI()+"?id="+xyt.getId()+"'>"+xyt.getTitle()+"</a></span>";
model.addAttribute("xytHtml",xytHtml);
}else{
String xytHtml="<span><font color='red'>下一条</font><a href='javascript:void(0);'>无</a></span>";
model.addAttribute("xytHtml",xytHtml);
}
sql语句为:select * from zx_articles
where id = (select id from zx_articles where id < 11368 and menu_id=(select menu_id from zx_articles where id=11368 and deleted_at is null) order by id desc limit 1)
or id=(select id from zx_articles where id > 11368 and menu_id=(select menu_id from zx_articles where id=11368 and deleted_at is null) order by id asc limit 1)

浙公网安备 33010602011771号