Dao层代码:
1、数据层接口代码
package cn.cdi.util.page; import java.util.List; public interface MemberDao { /** * 分页查询 * @param hql 查询的条件 * @param offset 开始记录 * @param length 一次查询几条记录 * @return */ public List queryForPage(final String hql,final int offset,final int length); /** * 查询所有记录数 * @param hql 查询的条件 * @return 总记录数 */ public int getAllRowCount(String hql); }
2、实现接口的代码
package cn.cdi.util.page; import java.sql.SQLException; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public class MemberDaoImpl extends HibernateDaoSupport implements MemberDao { /** * 分页查询 * @param hql 查询的条件 * @param offset 开始记录 * @param length 一次查询几条记录 * @return */ public List queryForPage(final String hql,final int offset,final int length){ List list = getHibernateTemplate().executeFind(new HibernateCallback(){ public Object doInHibernate(Session session) throws HibernateException,SQLException{ Query query = session.createQuery(hql); query.setFirstResult(offset); query.setMaxResults(length); List list = query.list(); return list; } }); return list; } /** * 查询所有记录数 * @return 总记录数 */ public int getAllRowCount(String hql){ return getHibernateTemplate().find(hql).size(); } }
业务逻辑层代码
1、业务逻辑层接口代码
package cn.cdi.util.page; public interface MemberService { /** * 分页查询 * @param pageSize 每页显示几条 * @param currentPage 当前第几页 * @return 封闭了分页信息(包括记录集list)的Bean */ public PageBean queryForPage(int pageSize,int currentPage,String ModelBean); }
2、业务逻辑层实现接口代码
package cn.cdi.util.page; import java.util.List; public class MemberServiceImpl implements MemberService{ //通过applicationContext.xml配置文件注入MemberDao的值 private MemberDao memberDao; public void setMemberDao(MemberDao memberDao) { this.memberDao = memberDao; } /** * 分页查询 * @param currentPage 当前第几页 * @param pageSize 每页大小 * @return 封闭了分页信息(包括记录集list)的Bean */ public PageBean queryForPage(int pageSize,int page,String hql){ //final String hql = "from "+ModelBean; //查询语句 int allRow = memberDao.getAllRowCount(hql); //总记录数 int totalPage = PageBean.countTotalPage(pageSize, allRow); //总页数 final int offset = PageBean.countOffset(pageSize, page); //当前页开始记录 final int length = pageSize; //每页记录数 final int currentPage = PageBean.countCurrentPage(page); List list = memberDao.queryForPage(hql,offset, length); //"一页"的记录 //把分页信息保存到Bean中 PageBean pageBean = new PageBean(); pageBean.setPageSize(pageSize); pageBean.setCurrentPage(currentPage); pageBean.setAllRow(allRow); pageBean.setTotalPage(totalPage); pageBean.setList(list); pageBean.init(); return pageBean; } }
bean层代码
package cn.cdi.util.page; import java.util.ArrayList; import java.util.List; import cn.cdi.importData.entity.ImportTask; public class PageBean { @SuppressWarnings("rawtypes") private List list = new ArrayList(); //要返回的某一页的记录列表 private int allRow; //总记录数 private int totalPage; //总页数 private int currentPage; //当前页 private int pageSize; //每页记录数 private boolean isFirstPage; //是否为第一页 private boolean isLastPage; //是否为最后一页 private boolean hasPreviousPage; //是否有前一页 private boolean hasNextPage; //是否有下一页 @SuppressWarnings("rawtypes") public List getList() { return list; } @SuppressWarnings("rawtypes") public void setList(List list) { this.list = list; } public int getAllRow() { return allRow; } public void setAllRow(int allRow) { this.allRow = allRow; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } /** *//** * 初始化分页信息 */ public void init(){ this.isFirstPage = isFirstPage(); this.isLastPage = isLastPage(); this.hasPreviousPage = isHasPreviousPage(); this.hasNextPage = isHasNextPage(); } /** *//** * 以下判断页的信息,只需getter方法(is方法)即可 * @return */ public boolean isFirstPage() { return currentPage == 1; // 如是当前页是第1页 } public boolean isLastPage() { return currentPage == totalPage; //如果当前页是最后一页 } public boolean isHasPreviousPage() { return currentPage != 1; //只要当前页不是第1页 } public boolean isHasNextPage() { return currentPage != totalPage; //只要当前页不是最后1页 } /** *//** * 计算总页数,静态方法,供外部直接通过类名调用 * @param pageSize 每页记录数 * @param allRow 总记录数 * @return 总页数 */ public static int countTotalPage(final int pageSize,final int allRow){ int totalPage = allRow % pageSize == 0 ? allRow/pageSize : allRow/pageSize+1; return totalPage==0?1:totalPage; } /** *//** * 计算当前页开始记录 * @param pageSize 每页记录数 * @param currentPage 当前第几页 * @return 当前页开始记录号 */ public static int countOffset(final int pageSize,final int currentPage){ final int offset = pageSize*(currentPage-1); return offset; } /** *//** * 计算当前页,若为0或者请求的URL中没有"?page=",则用1代替 * @param page 传入的参数(可能为空,即0,则返回1) * @return 当前页 */ public static int countCurrentPage(int page){ final int curPage = (page==0?1:page); return curPage; } }
分页代码的使用
1、业务逻辑层获取封装数据代码实例(注这里需要利用spring的Ioc 将memberService注入到你业务逻辑层里面去)
/**
* 获取日志信息
* @param displayedCount //从第几条开始取
* @param pageDisplayCount //每页显示数
* @return 每页的日志信息
*/
public PageBean getLogInfos(int displayedCount,int pageDisplayCount) {
String hql = "";
hql = "from LogInfo";
return this.packImportTaskList(memberService.queryForPage(pageDisplayCount, displayedCount, hql));
}
/**
* 封装导入任务列表
*
* @param pageBean
* @return
*/
@SuppressWarnings("rawtypes")
public PageBean packImportTaskList(PageBean pageBean) {
List list = pageBean.getList();
List<HashMap<String, Object>> newlist = new ArrayList<HashMap<String, Object>>();
if (list != null)
for (int i = 0; i < list.size(); i++) {
LogInfo it = (LogInfo) list.get(i);
HashMap<String, Object> map = new HashMap<String, Object>();
String getTime = it.getUpdateDataTime();
getTime=getTime.substring(0, 4)+"-"+getTime.substring(4, 6)+"-"+getTime.substring(6, 8)+" "+getTime.substring(8, 10)+":"+getTime.substring(10, 12)+":"+
getTime.substring(12, 14);
map.put("updateDataTime",getTime);//更新数据日期
map.put("errorFilePath", it.getErrorFilePath());//错误数据文件的存放路径
map.put("operatorNum", it.getOperatorNum());//操作员账号
map.put("pckageFileSize", it.getPckageFileSize());//文件包大小(MB)
map.put("updateFileName", it.getUpdateFileName());//更新的文件名
map.put("updateQuantity", it.getUpdateQuantity());//更新数量(书目数据数量,馆藏数据量)
map.put("updateState", it.getUpdateState());//更新状态:成功,失败
newlist.add(map);
}
pageBean.setList(newlist);
return pageBean;
}
2、在Action中的使用
//第几页 private int page = 1; //包含分页信息的bean private PageBean pageBean; /** * 日志分页显示(每页10条) * @return */ public String showLogInfo() { this.pageBean = importDataBusiness.getLogInfos(page, 10); return "showLogInfo"; }
页面代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <#include "../meta.ftl"> <title>查看覆盖日志</title> <meta name="Generator" content="EditPlus"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <script language="JavaScript" type="text/javascript" src="${request.contextPath}/js/shade.js"></script> <script language="JavaScript" type="text/javascript"> function submitForm(maxpage){ var page = document.getElementById("page").value; var patrn=/^[0-9]{1,20}$/; if (!patrn.exec(page)){ alert("请输入正整数字!"); } else if(page > maxpage){ alert("跳转页码不能超过总页数!"); }else if(page < 1){ alert("跳转页码不能小与1!"); }else{ document.getElementById("searchImportTask").submit(); } } function checkDel(){ var str = "/%E6%B1%A4%E5%A7%86%E7%8C%AB";alert(str); var str2 = decodeURIComponent(str);alert(str2); document.location.href("/downloads/汤姆猫.zip") var url = document.location.href; var name="" if (url.indexOf("=")>0) { name = url.substring(url.indexOf("=")+1,url.length) } alert(url); var qq = document.getelementbyid("jiema"); alert(qq); } </script> </head> <body> <!--引入头部 --> <#include "../head.ftl"> <!-- 内容 --> <div id="content"> <div id="allmain"> <form action="../importData/searchLogInfo.action" method="post" id="searchImportTask"> <h2>查看覆盖日志</h2> <TABLE cellpadding="0" cellspacing="1" class="t2" style=" margin-top:10px"> <tr class="a1"> <th width="15%">更新数据日期</th> <th width="15%">操作员账号</th> <th width="15%">更新的文件名</th> <th width="10%">文件包大小(MB)</th> <th width="10%">更新数量</th> <th width="10%">更新状态</th> <th width="25%">错误数据文件的存放路径</th> </tr> <#list pageBean.list as logInfo> <tr <#if (logInfo_index%2 != 0) >class="a1"</#if>> <td align="center">${logInfo.updateDataTime!}</td> <td align="center">${logInfo.operatorNum!}</td> <td align="center">${logInfo.updateFileName!}</td> <td align="center">${logInfo.pckageFileSize!}</td> <td align="center">${logInfo.updateQuantity!}</td> <td align="center"><#if logInfo.updateState=="0"><span class="red">失败</span><#elseif logInfo.updateState == "1"><span class="green">成功</span></#if></td> <td align="center">${logInfo.errorFilePath!}</td> </tr> </#list> </table> <style> .result{ text-align:center; padding-top:10px} .red{ color:#CE490F} a{ color:#656565} a:hover{ color:#CE490F} .input_blur{ width:35px; height:14px} </style> <p class="result"> 共 <b class="red">${pageBean.allRow!}</b> 条 共 <b class="red">${pageBean.totalPage!}</b> 页 第 <b class="red">${pageBean.currentPage!}</b> 页 <#if ("${pageBean.currentPage!}" == "1")>首页 上一页 <#else> <a href="searchLogInfo.action?page=1">首页</a> <a href="searchLogInfo.action?page=${pageBean.currentPage-1!}">上一页</a> </#if> <#if ("${pageBean.currentPage!}" == "${pageBean.totalPage!}")>下一页 末页 <#else> <a href="searchLogInfo.action?page=${pageBean.currentPage+1!}">下一页</a> <a href="searchLogInfo.action?page=${pageBean.totalPage!}">末页</a> </#if> <input name="page" id="page" type="text" class="input_blur" /> <a href="javascript:submitForm(${pageBean.totalPage!});" target="_self" class="go" >GO</a></p> </form> </div> </div> <!--页尾 --> <#include "../foot.ftl"> </body> </html>