mybatis分页

web项目中经常要用到分页,mybatis对分页操作进行了一个很好的封装!

主要代码:

Service:

package com.mfc.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mfc.entity.Work;
import com.mfc.inter.WorkDao;

/**
 * 2017年10月22日18:49:29
 * 工作的service
 * */
@Service("workService")
public class WorkService implements IWorkService {
	
	@Resource(name = "workDao")
	private WorkDao workDao;
	
	@Override
	public PageInfo<Work> getAllMyWork(int work_UserID,int pageNum,int pageSize){
		PageHelper.startPage(pageNum, pageSize);
		List<Work> list = workDao.getAllMyWork(work_UserID);
		PageInfo<Work> works=new PageInfo<Work>(list);
		return works;
	}
}

Controller:

package com.mfc.ctrl;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mfc.entity.User;
import com.mfc.service.IWorkService;

/**
 * 2017年10月22日18:19:27
 * 工作使用的控制器
 * */
@Controller
@RequestMapping("workCtrl")
public class WorkCtrl {

	@Resource(name = "workService")
	private IWorkService workService;
	
	//查询当前登录用户的所有工作
	@RequestMapping("getAllMyWork")
	public String getAllMyWork(HttpSession session,Model model,Integer pageNum){
		if(pageNum==null){
			pageNum=1;
		}
		User loginUser = (User) session.getAttribute("loginUser");
		model.addAttribute("pi", workService.getAllMyWork(loginUser.getUserId(),pageNum,5));
		return "admin-templates/right_allmywork";
	}
}

界面:

  <body>
  	<table border="1" cellpadding="0" cellspacing="0" width="80%" style="text-align: center;" align="center">
  		<tr>
  			<th>编号</th>
  			<th>标题</th>
  			<th>内容</th>
  			<th>起始时间</th>
  			<th>结束时间</th>
  			<th>状态</th>
  		</tr>
  		<c:forEach items="${pi.list}" var="work">
  		<tr>
  			<td>${work.workId}</td>
  			<td>
  				<c:if test="${fn:length(work.workTitle)>5}">
  					${fn:substring(work.workTitle, 0, 5)} ...
  				</c:if>
  				<c:if test="${fn:length(work.workTitle)<=5}">
  					${work.workTitle}
  				</c:if>
  			</td>
  			<td>
  				<c:if test="${fn:length(work.workContent)>5}">
  					${fn:substring(work.workContent, 0, 5)} ...
  				</c:if>
  				<c:if test="${fn:length(work.workContent)<=5}">
  					${work.workContent}
  				</c:if>
  			</td>
  			<td><fmt:formatDate value="${work.workStartTime}" pattern="yyyy-MM-dd"/></td>
  			<td><fmt:formatDate value="${work.workStopTime}" pattern="yyyy-MM-dd"/></td>
  			<td>
  				<c:if test="${work.workState == 0}">
  					未完成
  				</c:if>
  				<c:if test="${work.workState == 1}">
  					已完成
  				</c:if>
  			</td>
  		</tr>
  		</c:forEach>
  		<tr align="center">
    			<td colspan="6">
    			  <a href="workCtrl/getAllMyWork.do?pageNum=1">首页</a>  
                  <a href="workCtrl/getAllMyWork.do?pageNum=${pi.pageNum>1 ? (pi.pageNum-1):1 }">上一页</a>  
                  <a href="workCtrl/getAllMyWork.do?pageNum=${pi.pageNum<pi.pages ? (pi.pageNum+1):pi.pages }">下一页</a>  
                  <a href="workCtrl/getAllMyWork.do?pageNum=${pi.pages }">尾页</a>    
                  当前${pi.pageNum }/${pi.pages }页,共${pi.total }条
    			</td>
    		</tr>
  	</table>
  </body>

效果:

2017年12月26日16:25:10补充:

pagehelper里面的

PageHelper.startPage(1,10);只对该语句以后的第一个查询语句得到的数据进行分页,


参考代码:http://download.youkuaiyun.com/download/fancheng614/9907891

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值