在dao中定义的方法
[/size][size=xx-small]
[/size][size=medium]
[align=center][/align]
/**
* 分页
* @param page
* @param rowsPerPage
* @return
*/
public List findPlantByPage(int page, int rowsPerPage);
public int getPlanTotalPage(int rowsPerPage);
public int getPlanNum();
public int getPlanTotalPage1(int rowsPerPage);
dao实现类中方法执行
将 UsersInfo 改成自己的实体类名字
/**
* 分页方法
*/
/**
* 查找并返回所有计划
*/
public List findPlantByPage(int page, int rowsPerPage) {
Session session =this.getSession();
Query query = session.createQuery("from UsersInfo order by id asc");
query.setMaxResults(rowsPerPage); // 每页最多显示几条
query.setFirstResult((page - 1) * rowsPerPage); // 每页从第几条记录开始
List list = query.list();
session.close();
return list;
}
/**
* 共多少页计划数据
*/
public int getPlanTotalPage(int rowsPerPage) {
int rows = 0;
String hql = "select count(*) from UsersInfo";
Session session = this.getSession();
Query query = session.createQuery(hql);
rows = ((Integer) query.iterate().next()).intValue();
session.close();
if (rows % rowsPerPage == 0) {
return rows / rowsPerPage;
} else {
return rows / rowsPerPage + 1;
}
}
public int getPlanNum() {
String hql = "select count(*) from UsersInfo ";
int rows = 0;
Session session =this.getSession();
Query query = session.createQuery(hql);
rows = ((Integer) query.iterate().next()).intValue();
session.close();
return rows;
}
/**
* 条件查询后返回的计划总页数
*/
public int getPlanTotalPage1(int rowsPerPage) {
int rows = 0;
Session session =this.getSession();
String hql = "select count(*) from UsersInfo";
Query query = session.createQuery(hql);
rows = ((Integer) query.iterate().next()).intValue();
session.close();
if (rows % rowsPerPage == 0) {
return rows / rowsPerPage;
} else {
return rows / rowsPerPage + 1;
}
}
同样在biz中定义相同的接口 在biz实现类中调用dao(同普通业务一样调用既可以)
action中定义分页用的变量要有get和set方法
/**
* 分页
*/
private int rowsPerPage = 10;// 每页显示几条
private int page = 1; // 默认当前页
private int totalPage;// 总共多少页
private int planNum;// 总过多少条
action中分页用的查询方法(同样调用自己的biz调用定义的那几个分页方法)
public String findUser() throws Exception {
List list=usersInfoBiz.findPlantByPage(page, rowsPerPage);
totalPage =usersInfoBiz.getPlanTotalPage(rowsPerPage);
System.out.println("totalpage"+totalPage);
planNum =usersInfoBiz.getPlanNum();
System.out.println("planNum"+planNum);
session.put("item", list);
session.put("totalPage", totalPage);
session.put("planNum", planNum);
session.put("page", page);
return SUCCESS;
}
页面 固定代码(只改变自己的跳转用的action方法)
<tr>
<td>当前第<struts:iterator id="page" value="#session.page"><struts:property value="#page"/></struts:iterator> 页</td><td>共<struts:iterator id="planNum" value="#session.planNum"><struts:property value="#planNum"/></struts:iterator>条记录</td><td>共分<struts:iterator id="totalPage" value="#session.totalPage"><struts:property value="#totalPage"/></struts:iterator>页</td><td></td>
<td><a href="findUser.action?page=1">首 页</a></td>
<td>
<struts:if test="#page<=1">
上一页
</struts:if>
<struts:else>
<struts:iterator id="page1" value="#session.page">
<a href="findUser.action?page=<struts:property value='#page1-1'/>">上一页</a>
</struts:iterator>
</struts:else>
</td>
<td><struts:if test="#page>=#totalPage">
下一页
</struts:if>
<struts:else>
<struts:iterator id="page2" value="#session.page">
<a href="findUser.action?page=<struts:property value='#page2+1'/>">下一页</a>
</struts:iterator>
</struts:else>
</td>
<td>
<struts:iterator id="totalPage1" value="#session.totalPage">
<a href="findUser.action?page=<struts:property value='#totalPage'/>">尾页</a>
</struts:iterator>
</td>
</tr>
注:我是用的struts2 写的ssh框架
/**
* 分页
* @param page
* @param rowsPerPage
* @return
*/
public List findPlantByPage(int page, int rowsPerPage);
public int getPlanTotalPage(int rowsPerPage);
public int getPlanNum();
public int getPlanTotalPage1(int rowsPerPage);
dao实现类中方法执行
将 UsersInfo 改成自己的实体类名字
/**
* 分页方法
*/
/**
* 查找并返回所有计划
*/
public List findPlantByPage(int page, int rowsPerPage) {
Session session =this.getSession();
Query query = session.createQuery("from UsersInfo order by id asc");
query.setMaxResults(rowsPerPage); // 每页最多显示几条
query.setFirstResult((page - 1) * rowsPerPage); // 每页从第几条记录开始
List list = query.list();
session.close();
return list;
}
/**
* 共多少页计划数据
*/
public int getPlanTotalPage(int rowsPerPage) {
int rows = 0;
String hql = "select count(*) from UsersInfo";
Session session = this.getSession();
Query query = session.createQuery(hql);
rows = ((Integer) query.iterate().next()).intValue();
session.close();
if (rows % rowsPerPage == 0) {
return rows / rowsPerPage;
} else {
return rows / rowsPerPage + 1;
}
}
public int getPlanNum() {
String hql = "select count(*) from UsersInfo ";
int rows = 0;
Session session =this.getSession();
Query query = session.createQuery(hql);
rows = ((Integer) query.iterate().next()).intValue();
session.close();
return rows;
}
/**
* 条件查询后返回的计划总页数
*/
public int getPlanTotalPage1(int rowsPerPage) {
int rows = 0;
Session session =this.getSession();
String hql = "select count(*) from UsersInfo";
Query query = session.createQuery(hql);
rows = ((Integer) query.iterate().next()).intValue();
session.close();
if (rows % rowsPerPage == 0) {
return rows / rowsPerPage;
} else {
return rows / rowsPerPage + 1;
}
}
同样在biz中定义相同的接口 在biz实现类中调用dao(同普通业务一样调用既可以)
action中定义分页用的变量要有get和set方法
/**
* 分页
*/
private int rowsPerPage = 10;// 每页显示几条
private int page = 1; // 默认当前页
private int totalPage;// 总共多少页
private int planNum;// 总过多少条
action中分页用的查询方法(同样调用自己的biz调用定义的那几个分页方法)
public String findUser() throws Exception {
List list=usersInfoBiz.findPlantByPage(page, rowsPerPage);
totalPage =usersInfoBiz.getPlanTotalPage(rowsPerPage);
System.out.println("totalpage"+totalPage);
planNum =usersInfoBiz.getPlanNum();
System.out.println("planNum"+planNum);
session.put("item", list);
session.put("totalPage", totalPage);
session.put("planNum", planNum);
session.put("page", page);
return SUCCESS;
}
页面 固定代码(只改变自己的跳转用的action方法)
<tr>
<td>当前第<struts:iterator id="page" value="#session.page"><struts:property value="#page"/></struts:iterator> 页</td><td>共<struts:iterator id="planNum" value="#session.planNum"><struts:property value="#planNum"/></struts:iterator>条记录</td><td>共分<struts:iterator id="totalPage" value="#session.totalPage"><struts:property value="#totalPage"/></struts:iterator>页</td><td></td>
<td><a href="findUser.action?page=1">首 页</a></td>
<td>
<struts:if test="#page<=1">
上一页
</struts:if>
<struts:else>
<struts:iterator id="page1" value="#session.page">
<a href="findUser.action?page=<struts:property value='#page1-1'/>">上一页</a>
</struts:iterator>
</struts:else>
</td>
<td><struts:if test="#page>=#totalPage">
下一页
</struts:if>
<struts:else>
<struts:iterator id="page2" value="#session.page">
<a href="findUser.action?page=<struts:property value='#page2+1'/>">下一页</a>
</struts:iterator>
</struts:else>
</td>
<td>
<struts:iterator id="totalPage1" value="#session.totalPage">
<a href="findUser.action?page=<struts:property value='#totalPage'/>">尾页</a>
</struts:iterator>
</td>
</tr>
注:我是用的struts2 写的ssh框架