首先我们需要一个工具类Page类来表达:
import java.util.List;
public class Page
{
private int everyPage = 10; //每页显示的条数
private int totalCount; //总共需要显示的条数
private int totalPage; //总共需要显示的页数
private int beginPage = 1; //开始的页数
private boolean hasPrePage; //是否有上一页
private boolean hasNextPage; //是否有下一页
private List list; //用来存储的数据结构(统一为一个list)
/**
* 初始化:当获得了总共需要显示的条数后即可执行这个方法
*/
public void init(){
int totalPage = totalCount % everyPage == 0 ? totalCount / everyPage : totalCount / everyPage + 1;
this.setTotalPage(totalPage);
this.setHasNextPage(beginPage >= totalCount);
this.setHasPrePage(beginPage <= 1);
}
public int getEveryPage()
{
return this.everyPage;
}
public void setEveryPage(int everyPage)
{
this.everyPage = everyPage;
}
public int getTotalCount()
{
return this.totalCount;
}
public void setTotalCount(int totalCount)
{
this.totalCount = totalCount;
}
public int getTotalPage()
{
return this.totalPage;
}
public void setTotalPage(int totalPage)
{
this.totalPage = totalPage;
}
public int getBeginPage()
{
return this.beginPage;
}
public void setBeginPage(int beginPage)
{
this.beginPage = beginPage;
}
public boolean isHasPrePage()
{
return this.hasPrePage;
}
public void setHasPrePage(boolean hasPrePage)
{
this.hasPrePage = hasPrePage;
}
public boolean isHasNextPage()
{
return this.hasNextPage;
}
public void setHasNextPage(boolean hasNextPage)
{
this.hasNextPage = hasNextPage;
}
public List getList()
{
return this.list;
}
public void setList(List list)
{
this.list = list;
}
}
假设你有这么一个类Microblog,其结构如下:
public class Microblog {
private Integer id;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column microblog.userId
* 这条微博现在属于谁
* @mbggenerated
*/
private Integer userid;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column microblog.content
* 这条微博的内容
* @mbggenerated
*/
private String content;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column microblog.time
* 这条微博现在发布的时间
* @mbggenerated
*/
private Date time;
/**
* 这条微博是否是转载的,是转载的为true,否则为false
*/
private Boolean isForward;
private Integer sourceuserid;
/**
* 这条博客的原创博客的id(原创的话即为0)
*/
private Integer sourceMicroblogId;
/**
* 你转发的那条微博(不一定是原创的那一条,原创的话即为0)
*/
private Integer forwardMicroblogId;
/*接下来就是相应的get和set方法,博主在此省略*/
}
接下来看看底层如何获取这些数据,(假设你此时需要取得一条微博的所有转发微博)xml文件对应的mybatis代码为:
<select id="selectPageByForwardMicroblogId" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from microblog
where forwardMicroblogId = #{0,jdbcType=INTEGER} order by time desc limit #{1,jdbcType=INTEGER},#{2,jdbcType=INTEGER}
</select>
其对应接口中的方法声明为:(接口为MicroblogMapper)
/**
* 查看当前页面的转发评论
* @param forwardMicroblogId
* @param beginPage 从第几个开始寻找几个,这个是从0开始的(并不是第几页)
* @param everyPage 一页多少条记录
* @return
*/
List<Microblog> selectPageByForwardMicroblogId(Integer forwardMicroblogId, int beginPage, int everyPage);
Service层的写法为:
List<Microblog> microblogs = this.microblogMapper.selectPageByForwardMicroblogId(
microblog.getForwardMicroblogId(), (page.getBeginPage() - 1)*page.getEveryPage(), page.getEveryPage());
int totalCount = this.microblogMapper.countByForwardMicroblogId(microblog.getForwardMicroblogId());
page.setList(microblogs);
page.setTotalCount(totalCount);
page.init();
Controller层的写法:
@RequestMapping("user/microblog_showForwardMore")
public String showForwardMore(Microblog microblog, Page page, ModelMap modelMap){
page = this.microblogService.getTenByForward(microblog,page);
Microblog mic = this.microblogService.findOne(microblog.getForwardMicroblogId());
modelMap.addAttribute("microblog", mic);
modelMap.addAttribute("Page", page);
modelMap.addAttribute("partSelect",1);
return "/user/showMore";
}
接下来,你再将page传给页面,定义为“Page”(博主并没有转化为JSON数据,因为暂时还没有接触,但其实变成JSON更好,因为这样在使用前台的框架时可以更加方便并且可以更加通用,假如你的前台并不是jsp),其使用大致为
<c:forEach var="m" items="${Page.list}">
<div style="border: 1px solid black">
<a href="user_scan.action?id=${m.sourceUser.id}">@${m.sourceUser.name}</a>:${m.forwardRemark }
<c:forEach items="${m.forwardRemarks}" var="r">
<a href="user_scan.action?id=${r.userId}">@${r.userName }</a>${r.remark }
</c:forEach>
time:${m.getNTime()}<br /> <a href="#"
οnclick="prom(${m.sourceMicroblogId==0?m.id:m.sourceMicroblogId }, ${m.id})">
(转发${m.getForwards() }) </a> <br />
</div>
</c:forEach>
取数据是比较简单的,因为可以利用各种前台标签(SSM貌似支持最基础的C标签,如果大家知道可以用其他标签的话,请告诉博主,博主将感激不尽),主要说一下首页、上一页、下一页的显示:
<a
href="microblog_showForwardMore.action?forwardMicroblogId=${Microblog.forwardMicroblogId }&sourceMicroblogId=${Microblog.sourceMicroblogId}&beginPage=1"><input
type="button" class="className" value=" 首 页 " /> </a>
<c:choose>
<c:when test="${Page.beginPage == 1}">
<input type="button" class="className" style="width: 55px;"
value="上一页" />
</c:when>
<c:otherwise>
<a
href="microblog_showForwardMore.action?forwardMicroblogId=${Microblog.forwardMicroblogId }&sourceMicroblogId=${Microblog.sourceMicroblogId}&beginPage=${Page.beginPage-1}"><input
type="button" class="className" style="width: 55px;" value="上一页" /></a>
</c:otherwise>
</c:choose>
共
${Page.totalCount }条记录,
每页
${Page.getEveryPage() }条
${Page.beginPage }/${Page.totalPage}
<c:choose>
<c:when test="${Page.beginPage >= Page.totalPage}">
<input type="button" class="className" style="width: 55px;"
value="下一页" />
</c:when>
<c:otherwise>
<a
href="microblog_showForwardMore.action?forwardMicroblogId=${Microblog.forwardMicroblogId }&sourceMicroblogId=${Microblog.sourceMicroblogId}&beginPage=${Page.beginPage+1}"><input
type="button" class="className" style="width: 55px;" value="下一页" /></a>
</c:otherwise>
</c:choose>
<a
href="microblog_showForwardMore.action?forwardMicroblogId=${Microblog.forwardMicroblogId }&sourceMicroblogId=${Microblog.sourceMicroblogId}&beginPage=${Page.totalPage } "> <input
type="button" class="className" value="尾 页" /></a>
点击首页这些按钮时,改变的只有一个beginPage属性而已,请谨记。
总结:博主最开始的分页是在利用struts2写的,当时看了一个晚上,结果写的时候还是磕磕绊绊,但是当你真正实现了一个之后,再看别人写的就不再那么困难了,所以“千里之行始于足下”,希望大家能早日完成自己的功能!