mybatis加mysql分页

来源:互联网 发布:c语言线程锁 编辑:程序博客网 时间:2024/06/05 16:26

1.page类的书写

import java.util.List;public class Page<T> {    //显示的记录    private List<T> listRecords;    //总页数    private int totalPage;    //总记录数    private int totalRecords;    //当前页码    private int currentPageNum;    //每页开始的记录    private int startIndex;    //每页显示多少条数据    private int pageSize=5;    // 判断上一页    private int upPage;    // 判断下一页    private int downPage;    //判断是否有首页    private boolean isFistPage;    //判断是否有尾页    private boolean isLastPage;    //计算显示的页码    private int startPage;    private int endPage;    public Page(int currentPageNum,int totalRecords){        //当前页码,页面传过来        this.currentPageNum=currentPageNum;        //总记录,数据库查出来的        this.totalRecords=totalRecords;        //总页数        this.totalPage=totalRecords%pageSize==0?totalRecords/pageSize:(totalRecords/pageSize)+1;        //每页开始记录的索引        this.startIndex=(currentPageNum-1)*pageSize;        //计算上一页        this.upPage = currentPageNum<=1?1:currentPageNum-1;        //计算下一页        this.downPage = currentPageNum>=totalPage?totalPage:currentPageNum+1;        //判断是否有首页        this.isFistPage = (currentPageNum>1)?true:false;        //判断是否有尾页        this.isLastPage = (currentPageNum<totalPage)?true:false;        if(totalPage<5){            startPage=1;            endPage=totalPage;        }else{            startPage=currentPageNum-2;            endPage=currentPageNum+2;            if(startPage<1){                startPage=1;                endPage=startPage+4;            }            if(endPage>totalPage){                endPage=totalPage;                startPage=endPage-4;            }        }    }    public int getStartPage() {        return startPage;    }    public void setStartPage(int startPage) {        this.startPage = startPage;    }    public int getEndPage() {        return endPage;    }    public void setEndPage(int endPage) {        this.endPage = endPage;    }    public List<T> getListRecords() {        return listRecords;    }    public void setListRecords(List<T> listRecords) {        this.listRecords = listRecords;    }    public int getTotalPage() {        return totalPage;    }    public void setTotalPage(int totalPage) {        this.totalPage = totalPage;    }    public int getTotalRecords() {        return totalRecords;    }    public void setTotalRecords(int totalRecords) {        this.totalRecords = totalRecords;    }    public int getCurrentPageNum() {        return currentPageNum;    }    public void setCurrentPageNum(int currentPageNum) {        this.currentPageNum = currentPageNum;    }    public int getStartIndex() {        return startIndex;    }    public void setStartIndex(int startIndex) {        this.startIndex = startIndex;    }    public int getPageSize() {        return pageSize;    }    public void setPageSize(int pageSize) {        this.pageSize = pageSize;    }    public int getUpPage() {        return upPage;    }    public void setUpPage(int upPage) {        this.upPage = upPage;    }    public int getDownPage() {        return downPage;    }    public void setDownPage(int downPage) {        this.downPage = downPage;    }    public boolean getIsFistPage() {        return isFistPage;    }    public void setIsFistPage(boolean isFistPage) {        this.isFistPage = isFistPage;    }    public boolean getIsLastPage() {        return isLastPage;    }    public void setIsLastPage(boolean isLastPage) {        this.isLastPage = isLastPage;    }    public Page() {        super();    }}

只需要用具体的实体类继承page类就行了

2.mapper.xml的书写
这里写图片描述
代码如下:

 <!-- 查询分页总数 -->  <select id="selectCount" resultType="java.lang.Integer" parameterType="CaseDetial">    SELECT COUNT(*) FROM casedetial     <where>        <if test="cCarid!=null and cCarid!=''" >            and c_carId like '%' #{cCarid} '%'        </if>        <if test="cgdwnum!=null and cgdwnum!=''">            and cgdwnum like '%' #{cgdwnum} '%'        </if>    </where>  </select>  <!-- 查询分页数据 -->  <select id="selectCaseDetial" resultMap="BaseResultMap" parameterType="CaseDetial">    SELECT * FROM casedetial     <where>        <if test="cCarid!=null and cCarid!=''" >            and c_carId like '%' #{cCarid} '%'        </if>        <if test="cgdwnum!=null and cgdwnum!=''">            and cgdwnum like '%' #{cgdwnum} '%'        </if>    </where>    limit #{startIndex},#{pageSize}  </select>

3.service的书写

public CaseDetial selectCaseDetial(CaseDetial caseDetial,HttpServletRequest request) {        // TODO Auto-generated method stub        String cgdwnum = caseDetial.getCgdwnum();        // 处理乱码        String carid = caseDetial.getcCarid();        String cCarid = null;        if (carid != null && carid != "") {            try {                cCarid = new String(carid.getBytes("iso-8859-1"), "utf-8");            } catch (UnsupportedEncodingException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }        caseDetial.setcCarid(cCarid);        //处理当前页码        int pageNum = 1;        if (caseDetial.getCurrentPageNum() != 0) {            pageNum = caseDetial.getCurrentPageNum();        }        CaseDetial cd = new CaseDetial(pageNum,caseMapper.selectCount(caseDetial));        cd.setcCarid(cCarid);        cd.setCgdwnum(cgdwnum);        //查询分页数据        List<CaseDetial> listCaseDetial = caseMapper.selectCaseDetial(cd);        if(listCaseDetial!=null){            cd.setListRecords(listCaseDetial);            request.setAttribute("cgdwnum", cgdwnum);            request.setAttribute("cCarid", cCarid);            request.setAttribute("cd", cd);        }        return cd;    }

4.jsp页面代码
①.查询时提交方法

<div class="ssanbox">             <span>             车牌号码:             </span>             <span>           <input class="tablesrk" id="cCarid" type="text" value="${cCarid}" placeholder="请输入">        </span>         <span>        &nbsp;抄告单位编号:        </span>        <span>           <input class="tablesrk" id="cgdwnum" type="text" value="${cgdwnum}" placeholder="请输入">&nbsp;&nbsp;        </span>        <span>            <input class="btn_blue w90" type="submit"  value="搜索" onclick="sub()">            </span>         <script type="text/javascript">            function sub(){                var c=$("#cCarid").val();                var cg=$("#cgdwnum").val();                window.location.href="${pageContext.request.contextPath}/listCaseDetial.action?cCarid="+c+"&cgdwnum="+cg;            }         </script>          </div>

②.点击页码时的提交

<script type="text/javascript">
function submitPage(obj){
var c=$("#cCarid").val();
var cg=$("#cgdwnum").val();
window.location.href="${pageContext.request.contextPath}/listCaseDetial.action?currentPageNum="+obj+"&cCarid="+c+"&cgdwnum="+cg;
}
</script>

原创粉丝点击