ibatis分页实现主要步骤:
a. 创建Page类。
b.创建bean类,并继承Page类。
b. 在dao层,定义两个方法。并实现该方法。
c. 在业务层,同样定义两个方法。并实现它。
d. 在配置文件中注入有关的page类。并设置相应的get和set方法。
e. 在action中调用有关的方法。
f.进行配置文件的设置。
详细步骤:
1. Page类
package com.exeerp.sm.bean;
public class Page
{
private int pagesize;//单页记录
private int currentpage = 1;//当前页面
private int countdate;//总记录数
private int lastPage;
private int nextPage;
private int countPage;
public int getCurrentpage()
{
return currentpage;
}
public void setCurrentpage(int currentpage)
{
if(currentpage>getCountpage())
{
this.currentpage = getCountpage();
}else {
if(currentpage < 1)
{
this.currentpage = 1;
}
else
{
this.currentpage = currentpage;
}
}
}
public int getPagesize() {
return pagesize;
}
public void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
public int getCountdate()
{
return countdate;
}
public void setCountdate(int countdate)
{
this.countdate = countdate;
}
//由记录数设定有关的页面数。
public int getCountpage()
{
int i = this.countdate / this.pagesize;
if((this.countdate % this.pagesize)!=0)
{
i+=1;
}
return i;
}
public int getLastPage()
{
return this.getCurrentpage()-1;
}
public int getNextPage()
{
return this.getCurrentpage()+1;
}
//给ibatis用数据
public int getCurrent()
{
return (this.currentpage-1)*pagesize;
}
public int getNext() {
return this.currentpage*pagesize;
}
public int getCountPage() {
return countPage;
}
public void setCountPage(int countPage) {
this.countPage = countPage;
}
public void setLastPage(int lastPage) {
this.lastPage = lastPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
}
2. 创建bean类,并继承Page类
package com.exeerp.sm.bean;
import com.exeerp.sm.bean.Page;
import java.util.*;
/**
* 实体类 审批流程信息描述
*
*/
public class Flow extends Page
{
//审批编号
private String flowId;
//审批名称
private String flowName;
//审批路径
private String flowUrl;
//审批描述
private String flowClass;
public String getFlowId() {
return flowId;
}
public void setFlowId(String flowId) {
this.flowId = flowId;
}
public String getFlowName() {
return flowName;
}
public void setFlowName(String flowName) {
this.flowName = flowName;
}
public String getFlowUrl() {
return flowUrl;
}
public void setFlowUrl(String flowUrl) {
this.flowUrl = flowUrl;
}
public String getFlowClass() {
return flowClass;
}
public void setFlowClass(String flowClass) {
this.flowClass = flowClass;
}
}
3.在dao层,定义两个方法,并实现该方法
dao层接口
package com.exeerp.sm.dao;
import java.util.List;
import com.exeerp.pub.exception.BizException;
import com.exeerp.pub.exception.DaoException;
import com.exeerp.sm.bean.Flow;;
public interface FlowDAO
{
public List getPageList(Object obj)throws BizException, DaoException;
public int getCountSize(Object obj)throws BizException, DaoException;
}
dao层接口实现
package com.exeerp.sm.dao.impl;
import java.util.*;
import com.exeerp.sm.bean.Flow;
import com.exeerp.sm.dao.FlowDAO;
import com.exeerp.pub.dao.AbstractBaseDAO;
import com.exeerp.pub.exception.BizException;
import com.exeerp.pub.exception.DaoException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
/**
* 审批流程管理数据持久层实现
*
*/
public class FlowDAOImpl extends AbstractBaseDAO implements FlowDAO
{
public List getPageList(Object obj)throws BizException, DaoException
{
return this.getSqlMapClientTemplate().queryForList("select-Flow", obj);
}
public int getCountSize(Object obj)throws BizException, DaoException
{
return(Integer)this.getSqlMapClientTemplate().queryForObject("selectcountsize-Flow");
}
}
4.在业务层,同样定义两个方法。并实现它。
package com.exeerp.sm.biz;
import java.util.List;
import com.exeerp.sm.bean.Flow;
import com.exeerp.pub.exception.BizException;
import com.exeerp.pub.exception.DaoException;
/**
* 业务层接口 审批流程信息接口
*
*/
public interface FlowManager
{
public List<Flow> findAll(Object obj)throws BizException,DaoException,Exception;
public int getCountSize(Object obj)throws BizException,DaoException;
}
业务层实现类
package com.exeerp.sm.biz.impl;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.BeanFactory;
org.springframework.context.support.ClassPathXmlApplicationContext;
import com.exeerp.sm.dao.*;
import com.exeerp.sm.bean.Flow;
import com.exeerp.sm.biz.*;
import com.exeerp.pub.exception.BizException;
import com.exeerp.pub.exception.DaoException;
import com.exeerp.pub.util.ExceptionMessage;
import java.util.*;
/**
* 审批环节管理业务逻辑层实现
*
*/
public class FlowManagerImpl implements FlowManager
{
static Logger logger = Logger.getLogger(FlowManagerImpl.class);
private FlowDAO flowDao;
public int getCountSize(Object obj)throws BizException,DaoException
{
return flowDao.getCountSize(obj);
}
public List<Flow> findAll(Object obj)throws BizException,DaoException,Exception
{
List<Flow> all=new ArrayList();
try
{
//all=flowDao.findAll(obj);
all = flowDao.getPageList(obj);
if(all!=null)
{
System.out.println("-----FlowManagerImpl.findAll()"+all);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
return all;
}
public FlowDAO getFlowDao() {
return flowDao;
}
public void setFlowDao(FlowDAO flowDao) {
this.flowDao = flowDao;
}
}
Action类:
package com.exeerp.sm.ctrl;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.exeerp.sm.biz.FlowManager;
import com.exeerp.sm.dao.*;
import com.exeerp.pub.action.BaseAction;
import com.exeerp.pub.exception.BizException;
import com.exeerp.pub.exception.DaoException;
import com.exeerp.pub.util.ExceptionMessage;
import com.exeerp.sm.bean.*;
import org.apache.struts2.ServletActionContext;//引出request对象
import java.io.*;
/**
* Action类 审批环节信息管理
*
*/
public class FlowManagerAction extends BaseAction
{
//记录日志信息
static Logger logger = Logger.getLogger(FlowManagerAction.class);
//引用的业务层的对象,
private FlowManager flowManager;//
//引用审批的bean对象
private Flow flow;
private List<Flow> all;
private final static int pagesize = 10;
private int countdate;//总记录
private Page page;//在action中注入该对象
public List<Flow> getAll()
{
return all;
}
public void setAll(List<Flow> all)
{
this.all = all;
}
public FlowManager getFlowManager() {
return flowManager;
}
public void setFlowManager(FlowManager flowManager) {
this.flowManager = flowManager;
}
public Flow getFlow() {
return flow;
}
public void setFlow(Flow flow) {
this.flow = flow;
}
public int getCountdate() {
return countdate;
}
public void setCountdate(int countdate) {
this.countdate = countdate;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public static int getPagesize() {
return pagesize;
}
/**
* 显示所有的审批流程信息
* @return String SUCCESS则查找成功,反之查找失败。
* @throws Exception
*/
public String findAll()throws Exception
{
String result=this.ERROR;
try
{
//查询并获取该记录数
countdate = flowManager.getCountSize(new Flow());
flow.setPagesize(10);
flow.setCountdate(countdate);
all=flowManager.findAll(flow);
if(all.size()>0)
{
result=this.SUCCESS;
//保存查找的对象到request
ServletActionContext.getRequest().setAttribute("all",all);
}
}
catch (BizException e)//进行业务层异常的捕获
{
e.printStackTrace()
;
logger.error(e.getMessage());
this.setMessage(e.getMessage());
result ="error";
}
catch (DaoException e) //进行数据访问层异常的捕获
{
e.printStackTrace();
logger.error(e.getMessage());
this.setMessage(e.getMessage());
}
catch (Exception e)//进行其他异常的捕获
{
e.printStackTrace();
logger.error(ExceptionMessage.getMessage("common.serverException"));
this.setMessage(e.getMessage());
}
finally
{
return result;
}
}
Spring的xml的配置:
<bean id="flow" class="com.exeerp.sm.bean.Flow"></bean>
<bean id="page" class="com.exeerp.sm.bean.Page"></bean>
<bean id="app-FlowManagerAction" class="com.exeerp.sm.ctrl.FlowManagerAction" scope="prototype">
<property name="flowManager" ref="flowManager"/>
<property name="flow" ref="flow"/>
<property name="page" ref="page"/>
</bean>
Ibetis配置:
<!-- 显示所有的审批信息 -->
<select id="select-Flow" resultClass="hashmap" parameterClass="Flow">
<![CDATA[
select * from (
select ROWNUM r,t.* from (
select * from Pub_Flow ) t)
where r > #current# and r <= #next#
]]>
<!-- 显示时只要修改对应的表名即可 -->
<!-- 注:t代表select * from Pub_Flow的字符集
t.* 代表他t下的所有字符集
ROWNUM :行数
current next起止页
-->
</select>
<select id="selectcountsize-Flow" resultClass="Integer">
<![CDATA[select count(*) from Pub_Flow ]]>
</select>
Main.jsp页面:
<%@ page language="java" contentType="text/html;charset=GBK"%>
<%@page import="com.exeerp.sm.bean.Page"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page import="java.util.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>审批流程管理</title>
<link href="css/style.css" rel="stylesheet"/>
<script language="JavaScript" type="text/javascript">
function choice(who)//全选和反选
{
var inputs=document.getElementsByName("check");
var selectedNum = 0;
for (var j=0;j<inputs.length;j++) //获取选中数量
{
if (inputs[j].checked == true) {selectedNum += 1;}
}
if (selectedNum = inputs.length) {who = 0;} //如果checkbox处于全部选中状态,则实现取消全选
if (who == 1) //全选
{
for (var i=0;i<inputs.length;i++)
{inputs.checked = true;}
}
else //反选
{
for (var i=0;i<inputs.length;i++)
{inputs.checked = !(inputs.checked);}
}
}
function panduan()//在删除删除进行选中的判定
{
var cked = false;
var inputs=document.getElementsByName("check");
var selectedNum = 0;
for (var j=0;j<inputs.length;j++) //获取选中数量
{
if (inputs[j].checked == true)
{
selectedNum += 1;
}
}
if (selectedNum==0) {
alert("必须选择一个");
return false;
}else{
if(confirm("您确定要删除"))
{
cked=true;
return true;
}else{
return false;
}
}
}
function selOpenPage()//选择有关的页面数时,进行页面的跳转
{
var aa=document.getElementById("selpage").value;//获取选项的值
document.location.href="findAllAction-Flow.action?flow.currentpage="+aa; //转向查询所有的action并传递值
}
</script> </head>
<body>
<style>
body{
text-align:center;
}
.datatable{
border:1px solid #d6dde6;
border-collapse:collapse;
}
.datatable td{
border:1px solid #d6dde6;
text-align:right;
padding:4px;
}
.datatable th{
border:1px solid #828282;
background:#bcbcbc;
font-weight:bold;
text-align:left;
padding:4px;
}
.datatable caption{
font:bold 1.9em "Times New Roman",Times,serif;
background:#b0c4de;
color:333517;
border:1px solid #78a367;
}
.datatable tr:hover, .datatable .highlight{
background:#ffff00;
}
.altrow{
background:#eefeef;
color:#000000;
}
</style>
<%int i=1;%>
你好
<h3><a href="flowadd.jsp">新增审批流程信息</a></h3>
<a href="../sm_flowstep/findAllAction-Flowstep.action?flowstep.currentpage=1">进入审批环节管理主页</a>
<a href="../sm_flowstepemp/findAllAction-Flowstepemp.action?flowstepemp.currentpage=1">进入审批环节处理管理主页</a>
<a href="../sm_coderule/findAllAction-Coderule.action?coderule.currentpage=1">进入编码规则管理主页</a>
<s:form theme="simple" action="./deleteAction-Flow.action" οnsubmit="return panduan()">
<table>
<tr height="20px" bgcolor="red">
<td>选择<br></td>
<td>序号<br></td>
<td>审批编号<br></td>
<td>审批名称<br></td>
<td>审批路径<br></td>
<td>审批描述<br></td>
<td>修改<br></td>
<td>添加审批环节<br></td>
</tr>
<s:if test="#request.all==null">
<tr>
<td>记录不存在!!!</td>
</tr>
</s:if>
<s:iterator value="#request.all">
<tr οnmοuseοver="this.style.backgroundColor='#cccccc'" οnmοuseοut="this.style.backgroundColor=''">
<td><s:checkbox name="check" id="check"fieldValue="%{FLOWID}" > </s:checkbox> <br></td>
<td><%=i++%></td>
<td><s:property value="FLOWID"/></td>
<td><s:property value="FLOWNAME"/></td>
<td><s:property value="FLOWURL"/></td>
<td><s:property value="FLOWCLASS"/></td>
<td><a href="findByIdAction-Flow.action?id=<s:property value="FLOWID"/>">修改</a><br></td>
<td><a href="../sm_flowstep/flowstepadd.jsp?id=<s:property value="FLOWID"/>">添加审批环节</a><br></td>
</tr>
</s:iterator>
<tr><td><s:submit value="删除" οnclick="return panduan()"/></td>
<td><s:if test="#request.flow.currentpage>1"><a href="findAllAction-Flow.action?flow.currentpage=1">首页</a></s:if>
<s:if test="#request.flow.currentpage>1"><a href="findAllAction-Flow.action?flow.countpage=<s:property value="flow.lastPage"/>">上一页</a></s:if>
<s:if test="#request.flow.countpage>1 && #request.flow.currentpage<#request.flow.countpage"><a href="findAllAction-Flow.action?flow.currentpage=<s:property value="flow.nextPage"/>">下一页</a></s:if>
<s:if test="#request.flow.currentpage<#request.flow.countpage"><a href="findAllAction-Flow.action?flow.currentpage=<s:property value="flow.countpage"/>">尾页</a></s:if>
<s:property value="flow.currentpage"/>/<s:property value="flow.countpage"/>
</td>
<td width="80px">
共<s:property value='countdate'/>条数据</td>
<%
int maxPage=(Integer)request.getAttribute("flow.countpage");
int cur=(Integer)request.getAttribute("flow.currentpage");
%>
<td>跳转到
<select name="selpage" id="selpage" onChange="selOpenPage()">
<%
for (int x = 1; x <= maxPage; x++) {
%>
<option value="<%=x%>"
<%=cur== x ? "selected"
: ""%>><%=x%></option>
<%
}
%>
</select>
页</td>
<td><input type="button" value="全选/全不选"οnclick="choice(1)"/>|</td>
<td><input type="button" value="反 选"οnclick="choice(0)"/></td>
</tr>
</table>
</s:form>
</body>
</html>
<script language="javascript">
οnlοad=function(){
var rows=document.getElementsByTagName("tr");
for(var i=1;i<rows.length;i++)
{
rows.index=i
rows.className=(i%2==0)?"":"altrow"
rows.οnmοuseοver=function()
{
this.className="highlight";
}
rows.οnmοuseοut=function()
{
this.className=this.index%2==0?"":"altrow";
}
}
}
</script>