github上有一个专门针对mybatis的物理分页开源项目:mybatis-paginator,兼容目前绝大多数主流数据库,十分好用,下面是使用步骤:
环境:struts2 + spring + mybatis
一、pom.xml中添加依赖项
1 <dependency> 2 <groupId>com.github.miemiedev</groupId> 3 <artifactId>mybatis-paginator</artifactId> 4 <version>1.2.15</version> 5 </dependency>
二、mybatis映射文件中按常规写sql语句
1 <select id="getFsuList" resultType="N_CA_FSU"> 2 Select t.RECID recId, 3 t.GROSSWEIGHT grossWeight, 4 t.TOTALGROSSWEIGHT totalGrossWeight, 5 t.GROSSWEIGHTUNITCODE grossWeightUnitCode, 6 ... 7 8 From N_CA_FSU t 9 Where ... 10 </select>
三、服务层基类
1 package acc.service.support; 2 3 import java.io.Serializable; 4 import java.util.List; 5 6 import org.apache.ibatis.session.SqlSession; 7 import org.apache.ibatis.session.SqlSessionFactory; 8 import org.mybatis.spring.SqlSessionFactoryBean; 9 import org.mybatis.spring.SqlSessionUtils; 10 import org.slf4j.Logger; 11 import org.slf4j.LoggerFactory; 12 import org.springframework.beans.factory.annotation.Autowired; 13 14 import com.github.miemiedev.mybatis.paginator.domain.PageBounds; 15 16 public class BaseServiceImpl implements Serializable { 17 18 private static final long serialVersionUID = 1293567786956029903L; 19 20 protected Logger logger = LoggerFactory.getLogger(this.getClass()); 21 22 @Autowired 23 protected SqlSessionFactoryBean sqlSessionFactory; 24 25 /** 26 * 查询分页数据 27 * 28 * @param mapperClass 29 * @param sqlId 30 * @param sqlParameter 31 * @param pageIndex 32 * @param pageSize 33 * @return 34 * @throws Exception 35 */ 36 protected List<?> getPageList(Class<?> mapperClass, String sqlId, 37 Object sqlParameter, int pageIndex, int pageSize) throws Exception { 38 SqlSession session = null; 39 try { 40 SqlSessionFactory sessionFactory = sqlSessionFactory.getObject(); 41 session = SqlSessionUtils.getSqlSession(sessionFactory); 42 if (pageIndex <= 0) { 43 pageIndex = 1; 44 } 45 if (pageSize <= 0) { 46 pageSize = 10; 47 } 48 PageBounds pageBounds = new PageBounds(pageIndex, pageSize); 49 return session.selectList(mapperClass.getName() + "." + sqlId, 50 sqlParameter, pageBounds); 51 } finally { 52 session.close(); 53 } 54 55 } 56 57 }
四、具体的服务层子类调用
1 package acc.service.support; 2 3 ... 4 5 @Service 6 public class FsuServiceImpl extends BaseServiceImpl implements FsuService { 7 8 private static final long serialVersionUID = 6560424159072027262L; 9 10 @Autowired 11 FsuMapper fsuMapper; 12 13 ... 14 15 16 @SuppressWarnings("unchecked") 17 @Override 18 public PageList<N_CA_FSU> getAll(int pageIndex, int pageSize) 19 throws Exception { 20 return (PageList<N_CA_FSU>) getPageList(FsuMapper.class, "getFsuList", 21 null, pageIndex, pageSize); 22 } 23 24 ... 25 26 27 }
服务层就处理完了,接下来看Action层
五、Action基类
1 package acc.action; 2 3 import org.apache.struts2.ServletActionContext; 4 import org.apache.struts2.convention.annotation.ParentPackage; 5 import org.slf4j.Logger; 6 import org.slf4j.LoggerFactory; 7 8 import acc.lms.invoker.utils.StringUtils; 9 10 import com.opensymphony.xwork2.ActionSupport; 11 12 @ParentPackage("default") 13 public class BaseController extends ActionSupport { 14 15 protected Logger logger = LoggerFactory.getLogger(this.getClass()); 16 17 private static final long serialVersionUID = -8955001188163866079L; 18 19 private int pageSize = 15; 20 21 private int pageIndex = 1; 22 23 private int totalCounts = 0; 24 private int totalPages = 0; 25 26 public int getPageSize() { 27 return pageSize; 28 } 29 30 public void setPageSize(int pageSize) { 31 this.pageSize = pageSize; 32 } 33 34 public int getPageIndex() { 35 String t = ServletActionContext.getRequest().getParameter("pageIndex"); 36 if (!StringUtils.isEmpty(t)) { 37 pageIndex = Integer.parseInt(t); 38 } 39 return pageIndex; 40 } 41 42 public int getTotalCounts() { 43 return totalCounts; 44 } 45 46 public void setTotalCounts(int totalCounts) { 47 this.totalCounts = totalCounts; 48 } 49 50 public int getTotalPages() { 51 return totalPages; 52 } 53 54 public void setTotalPages(int totalPages) { 55 this.totalPages = totalPages; 56 } 57 58 }
注:约定分页时,url类似 /xxx.action?pageIndex=N
六、具体的Action子类调用
1 package acc.action; 2 3 ... 4 5 @Results({ @Result(name = "success", type = "redirectAction", params = { 6 "actionName", "fsu" }) }) 7 public class FsuController extends BaseController implements 8 ModelDriven<Object> { 9 10 ... 11 @Autowired 12 FsuService fsuService; 13 14 ... 15 16 17 18 // GET /fsu 19 public HttpHeaders index() throws Exception { 20 list = fsuService.getAll(getPageIndex(), getPageSize()); 21 22 setPageSize(list.getPaginator().getLimit()); 23 setTotalCounts(list.getPaginator().getTotalCount()); 24 setTotalPages(list.getPaginator().getTotalPages()); 25 26 return new DefaultHttpHeaders("index").disableCaching(); 27 } 28 29 ... 30 31 }
七、前端页面
1 <link href="${pageContext.request.contextPath}/resources/css/simplePagination/simplePagination.css" rel="stylesheet" type="text/css"/> 2 <script type="text/javascript" src="${pageContext.request.contextPath}/resources/js/common/jquery-1.7.1.min.js"></script> 3 <script type="text/javascript" src="${pageContext.request.contextPath}/resources/js/common/simplePagination/jquery.simplePagination.js"></script> 4 <script type="text/javascript"> 5 var pageIndex = ${pageIndex}; 6 var pageSize = ${pageSize}; 7 var totalPages = ${totalPages}; 8 var totalCounts = ${totalCounts}; 9 10 $(document).ready(function() { 11 12 $("#page-box").pagination({ 13 items: totalCounts, 14 itemsOnPage: pageSize, 15 currentPage:pageIndex, 16 cssStyle: ‘light-theme‘, 17 prevText:‘<‘, 18 nextText:‘>‘, 19 onPageClick:function(page){ 20 gotoPage(page); 21 } 22 }); 23 showPageInfo(); 24 25 }); 26 27 function gotoPage(page) { 28 window.location = "${pageContext.request.contextPath}/fsu?pageIndex=" + page; 29 } 30 31 function showPageInfo(){ 32 $("#page-info").html(pageSize + "条/页,共" + totalCounts + "条,第" + pageIndex + "页,共" + totalPages + "页"); 33 } 34 </script> 35 36 37 <table class="tableE"> 38 <thead> 39 <tr> 40 <th>运单号</th> 41 <th>起始站</th> 42 ... 43 </tr> 44 </thead> 45 46 <tbody> 47 <s:iterator value="list"> 48 <tr> 49 <td>${waybillNumber}</td> 50 <td>${origin}</td> 51 ... 52 </tr> 53 </s:iterator> 54 </tbody> 55 </table> 56 57 58 <div id="page-box"></div>
解释:jquery的分页插件,网上一搜索一大堆,我用的是jquery.simplePagination,${pageIndex}、${pageSize}...包括list,这些属性都是后台Action中的model属性
时间: 2024-10-08 19:56:43