MyException - 我的异常网
当前位置:我的异常网» J2EE » (J2EE)Struts+Spring+MyBatis调整实现分页分页查询

(J2EE)Struts+Spring+MyBatis调整实现分页分页查询

www.MyException.Cn  网友分享于:2013-03-28  浏览:475次
(J2EE)Struts+Spring+MyBatis整合实现分页分页查询

首先引用一个写好的翻页模板工具类

    import java.util.List;  
      
    /* 
     * 翻页模板工具类 
     *  
     * */  
    public final class PageModel<T> {  
        private int currentPage;// 当前页  
        private int pageSize;// 每页显示条数  
        private int totalPage;// 总页数  
        private int totalRecord;// 总记录数  
        private List<T> dataList;// 分页数据  
      
        private PageModel() {  
        }  
      
        /* 
         * 初始化PageModel实例 
         */  
        private PageModel(final int pageSize, final String page, final int totalRecord) {  
            // 初始化每页显示条数  
            this.pageSize = pageSize;  
            // 设置总记录数  
            this.totalRecord = totalRecord;  
            // 初始化总页数  
            setTotalPage();  
            // 初始化当前页  
            setCurrentPage(page);  
      
        }  
      
        /* 
         * 外界获得PageModel实例 
         */  
        public static PageModel newPageModel(final int pageSize, final String page,  
                final int totalRecord) {  
      
            return new PageModel(pageSize, page, totalRecord);  
        }  
      
        // 设置当前请求页  
        private void setCurrentPage(String page) {  
            try {  
                currentPage = Integer.parseInt(page);  
      
            } catch (java.lang.NumberFormatException e) {  
                // 这里异常不做处理,当前页默认为1  
                currentPage = 1;  
            }  
            // 如果当前页小于第一页时,当前页指定到首页  
            if (currentPage < 1) {  
      
                currentPage = 1;  
            }  
      
            if (currentPage > totalPage) {  
      
                currentPage = totalPage;  
      
            }  
      
        }  
      
        private void setTotalPage() {  
            if (totalRecord % pageSize == 0) {  
      
                totalPage = totalRecord / pageSize;  
            } else {  
                totalPage = totalRecord / pageSize + 1;  
            }  
        }  
      
        /* 
         * 获得当前页 
         */  
        public int getCurrentPage() {  
            return currentPage;  
        }  
      
        /* 
         * 获得总页数 
         */  
        public int getTotalPage() {  
            return totalPage;  
      
        }  
      
        /* 
         * 获得开始行数 
         */  
        public int getStartRow() {  
            return (currentPage - 1) * pageSize;  
        }  
      
        /* 
         * 获得结束行 
         */  
        public int getEndRow() {  
            return currentPage * pageSize;  
        }  
      
        /* 
         * 获得翻页数据 
         */  
        public List<T> getDataList() {  
            return dataList;  
        }  
      
        /* 
         * 设置翻页数据 
         */  
        public void setDataList(List<T> dataList) {  
            this.dataList = dataList;  
        }  
      
        //首页  
         public int getFirst() {  
          
         return 1;  
         }  
        //  
        //上一页  
          
         public int getPrevious() {  
          
         return currentPage - 1;  
         }  
        //  
        // // 下一页  
         public int getNext() {  
          
         return currentPage + 1;  
         }  
        //  
        // // 尾页  
        //  
         public int getLast() {  
          
         return totalPage;  
         } 
} 

Action中

    public String showBorrowPage() {  
              
            Integer currentPage;  
      
            String page = this.getParameter("page");  
            // totalRecor计算  
               
            int totalRecord = 0;  
            try {  
                totalRecord = borrowManagerService.selectTotalRecord();  
            } catch (SQLException e1) {  
                // TODO Auto-generated catch block  
                e1.printStackTrace();  
            }  
              
            PageModel pageModel = PageModel.newPageModel(3, page,  
                    totalRecord);  
            List<Borrow> dataList = new ArrayList<Borrow>();  
            try {  
                dataList = borrowManagerService.pageSelect(pageModel);  
            } catch (SQLException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
            pageModel.setDataList(dataList);  
            this.getReqeust().put("pageModel", pageModel);  
            this.getReqeust().put("currentPage",pageModel.getCurrentPage());  
              
            //System.out.println(pageModel.getTotalPage());  
            return "showBorrowPage";  
        }  

serviece层

package com.qianyan.bm.rack.service;  
public class BorrowManagerServiceImpl implements BorrowManagerService {  
    private BorrowDao borrowDao;  
  
    public final void setBorrowDao(BorrowDao borrowDao) {  
        this.borrowDao = borrowDao;  
    }  
  
    //分页查询  
    public List<Borrow> pageSelect(PageModel pageModel) throws SQLException {  
        List<Borrow> borrowList = new ArrayList<Borrow>();  
        borrowList = borrowDao.pageSelect(pageModel.getStartRow(), pageModel  
                .getEndRow());  
        return borrowList;  
    }  
    //计算总条数  
    public int selectTotalRecord() throws SQLException {  
         int totalRecord = borrowDao.selectTotalRecord();  
        return totalRecord;  
    }  
  
}

dao层

package com.weixin.bm.rack.dao;  
import org.mybatis.spring.support.SqlSessionDaoSupport;  
  
import com.qianyan.bm.rack.po.Borrow;  
  
public class BorrowDaoImpl extends SqlSessionDaoSupport implements BorrowDao {  
    private final static String BM_BORROW_NAMESPACE = "com.weixin.bm.rack.po.BorrowMapper.";  
         //分页查询  
    public List<Borrow> pageSelect(int startRow,int endRow) throws SQLException {  
        List<Borrow> borrowList = new ArrayList<Borrow>();  
         Map params =new HashMap();  
         params.put("startRow", new Integer(startRow));  
         params.put("endRow", new Integer(endRow));  
          
        borrowList = this.getSqlSession().selectList(BM_BORROW_NAMESPACE+"pageSelect",params);  
          
        return borrowList;  
    }  
         //得到总条数  
    public int selectTotalRecord() throws SQLException {  
        int totalRecord = (Integer)this.getSqlSession().selectOne(BM_BORROW_NAMESPACE+"selectTotalRecord");  
        return totalRecord;  
    }  
  
}

Mybatis配置

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"   
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  
<mapper namespace="com.qianyan.bm.rack.po.BorrowMapper">  
  
    <!-- 结果集映射 -->  
    <resultMap type="Borrow" id="BorrowResultMap">  
        <result property="id" column="id" />  
        <result property="managerId" column="manager_id" />  
        <result property="borrowerId" column="borrower_id" />  
        <result property="bookId" column="book_id" />  
        <result property="borrowDate" column="borrow_date" />  
        <result property="returnDate" column="return_date" />  
        <result property="returnFlag" column="return_flag" />  
      
    </resultMap>  
      
    <!-- 一对多配置 -->  
    <resultMap type="Borrow" id="borrow2Book" extends="BorrowResultMap">  
        <association property="book" column="book_id" javaType="Book"  
            select="com.qianyan.bm.rack.po.BookMapper.selectBookById" />  
    </resultMap>  
          
        <!-- 分页查询 -->  
    <select id="pageSelect" resultMap="borrow2Book" parameterType="map">  
        <![CDATA[  
        select * from (select * from  (select  id,              
           manager_id ,        
           borrower_id ,       
           book_id,            
           borrow_date,          
           return_date,         
           return_flag,  
           rownum rn  
        FROM bm_borrow)   
        where rn <= #{endRow} )   
        where rn > #{startRow}  
         ]]>   
    </select>  
    <!-- 查询总条数-->  
    <select id="selectTotalRecord" resultType="int">  
    select count(*) from bm_borrow  
    </select>  
      
      
</mapper>

实体层

package com.qianyan.bm.rack.po;  
  
import java.util.Date;  
public class Borrow{  
      
    private int id;  
    private int managerId;  
    private int borrowerId;  
    private int bookId;  
    private Date borrowDate;  
    private Date returnDate;  
    private String returnFlag;  
    private Book book;  
    public Borrow(){  
          
    }  
省略Set和Get方法
}

展示层

<s:set var="pageModel" value="#request.pageModel"/>  
<s:iterator var="borrow" value="#pageModel.dataList">  
  
<tr>  
  <td width="200" height="35" align="center"><s:property value="#borrow.id"/></td>  
  <td height="35" align="center"><s:property value="#borrow.book.category.categoryName"/></td>  
  <td height="35" align="center"><s:property value="#borrow.book.bookName"/></td>  
  <td height="35" align="center"><s:property value="#borrow.book.bookAuthor"/></td>  
  <td height="35" align="center"><s:property value="#borrow.book.bookPublish"/></td>  
  <td height="35" align="center"><s:property value="#borrow.borrowDate"/></td>  
  <td height="35" align="center"><s:property value="#borrow.returnDate"/></td>  
  <td height="35" align="center"><s:property value="#borrow.returnFlag"/></td>  
</tr>  
</s:iterator>  
  
   
<tr>  
  <td width="200" height="35" align="center"><a href="book_showMainPage">首页</a></td>  
  <td height="35" colspan="5" align="center"><a><s:property value="#request.currentPage"/></a>/<a><s:property value="#request.totalPage"/></a>     
    <a href="borrow_showBorrowPage?page=<s:property value='#pageModel.first'/>">首页</a>  
    <a href="borrow_showBorrowPage?page=<s:property value='#pageModel.previous'/>">上一页</a>  
    <a href="borrow_showBorrowPage?page=<s:property value='#pageModel.next'/>">下一页</a>  
    <a href="borrow_showBorrowPage?page=<s:property value='#pageModel.last'/>">尾页</a>  
  <td height="35" align="center" ><a onclick="saveAsExcel(table1)">导出Excel</a></td>  
  <td height="35" align="center" ><a onclick="javascript:window.print();">打印</a></td>  
</tr>


文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有