java分页(基于mybatis)
该分页功能中心思想:001、为Page类设置 当前页,总记录数,每页记录数
002、将查询的结果集复制给Page的属性容器
003、向前台页面返回Page
做到这三点,其他过程可自己发挥
1、系统常量类
public class Conv
{
// 每页显示条数
public static int pageSize=10;
//登录后放在Session中的User信息
}
2、Page类,包含了可以存放List的容器
public class Page
{
/**当前页*/
private int curPage;
/**总页数*/
private int total;
/**总的记录数*/
private int sum;
/**结果集*/
private List result;
public Page(){}
public Page(int curPage,int sum) {
this.curPage = curPage;
this.sum = sum;
int totalPage =
(int) Math.ceil(sum/Double.parseDouble(Conv.pageSize + ""));
if (totalPage == 0)
totalPage = 1;
this.total=totalPage;
}
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
public List getResult() {
return result;
}
public void setResult(List result) {
this.result = result;
}
}
3、UnitService类获取SqlSession,利用mybatis的RowBounds进行分页,执行sql语句获得结果集
public class UnitService
{
private static UnitService unitService = null;
Logger LOG = Logger.getLogger(DeptService.class);
private UnitService(){}
public static UnitService getInstance(){
if(unitService==null){
unitService=new UnitService();
}
return unitService;
}
//分页查询单位
public Page queryQyDw(Map<String,Object> params,int currentPage,int pageSize) throws Exception{
List<QyDw> units = null;
int count = 0;
SqlSession session=MybatisUtil.getSqlSessionFactory().openSession();
try{
IunitDao manager=session.getMapper(IunitDao.class);
units=(List<QyDw>)session.selectList("com.xinfeijinxin.qy.dao.IunitDao.queryQyDw", params,new RowBounds((start-1)*Conv.pageSize, limit));
count = manager.queryQyDwCount();
}catch(Exception e){
}finally{
session.close();
}
Page page=new Page(start,count);//初始化分页数据
page.setResult(units);//封装对象
return page;
}
}
4、IunitDao.java 对应UnitDao.xml查询语句
public interface IunitDao
{
/**
* 查询单位列表
*/
public List<QyDw> queryQyDw(Map<String,Object> map);
}
5、UnitDao.xml
<?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">
<!-- namespace应对应接口路径-->
<mapper namespace="com.xinfeijinxin.qy.dao.IunitDao">
<!--查询记录,map参数可以根据情况去掉-->
<select id="queryQyDw" parameterType="map" resultType="com.xinfeijinxin.qy.bean.QyDw">
select dwid,dwmc,dwjc,sjdw,dwlx from qy_dw where 1=1
<if test="keyword!=null and keyword !=''">
and dwmc like CONCAT('%','${keyword}','%' )
</if>
<if test="keyword!=null and keyword !=''">
and dwjc like CONCAT('%','${keyword}','%' )
</if>
<if test="keyword!=null and keyword !=''">
and sjdw like CONCAT('%','${keyword}','%' )
</if>
<if test="keyword!=null and keyword !=''">
and dwlx like CONCAT('%','${keyword}','%' )
</if>
</select>
<!-- 查询记录总数-->
<select id="queryQyDwCount" resultType="int">
select count(*) from qy_dw
</select>
</mapper>
6、Action封装前台数据
public class UnitAction extends BaseAction
{
//getter setter()。。。。
private List<QyDw> units=null;
private Page page=null;
private QyDw unit;
//分页查询单位
public String list()
{
// 分页信息设置,接受前台参数
if (page == null)
page = new Page();
// 如果当前页为0,则设置当前页为1
if (page.getCurPage() == 0) {
page.setCurPage(1);
}
try{
Map<String,Object> map=new HashMap<String,Object>();
//map.put("keyword","学");
//将page传至前台
page=UnitService.getInstance().queryQyDw(map, page.getCurPage(), Conv.pageSize);
}catch(DBException e){
logger.error(e.getMessage());
}
return LIST;
}
}
7、BaseAction,设置统一返回的字符串,获取request和response
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class BaseAction extends ActionSupport{
protected HttpServletRequest request; // request
protected HttpServletResponse response; // response
protected ServletContext servletConext; // servletContext
protected HttpSession session; // session
public final String SUCCESS = "success";//成功
protected String ADD = "add"; // 返回增加
protected String DELETE = "delete"; // 返回删除
protected String UPDATE = "update"; // 返回修改
protected String LIST = "list"; // 返回修改
protected String EXCEPTION="exception"; //返回异常
Logger logger = Logger.getLogger(BaseAction.class);
public HttpServletRequest getRequest() {
return ServletActionContext.getRequest();
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public HttpServletResponse getResponse() {
return ServletActionContext.getResponse();
}
public void setResponse(HttpServletResponse response) {
this.response = response;
}
public ServletContext getServletConext() {
return ServletActionContext.getServletContext();
}
public void setServletConext(ServletContext servletConext) {
this.servletConext = servletConext;
}
public HttpSession getSession() {
return ServletActionContext.getRequest().getSession();
}
}
8、前台链接
<div class="btn" style="text-align: center;">
<s:if test="page.curPage <= 1">
<img src="images/firstpage2.png" />
<img src="images/prepage2.png" />
</s:if>
<s:else>
<a href="admin/Unit_list.action?page.curPage=1"><img
src="images/firstpage.png" /> </a>
<a
href="admin/Unit_list.action?page.curPage=<s:property value="%{page.curPage-1}" />"><img
src="images/prepage.png" /> </a>
</s:else>
<s:if test="page.curPage < page.total">
<a
href="admin/Unit_list.action?page.curPage=<s:property value="%{page.curPage+1}" />"><img
src="images/nextpage.PNG" /> </a>
<a
href="admin/Unit_list.action?page.curPage=<s:property value="%{page.total}" />"><img
src="images/lastpage.png" /> </a>
</s:if>
<s:else>
<img src="images/nextpage2.PNG" />
<img src="images/lastpage2.png" />
</s:else>
<select id="page" name="page.curPage"
style="width: 50px; vertical-align: middle;" onchange="jump()">
<s:bean name="org.apache.struts2.util.Counter" id="counter">
<s:param name="first" value="1" />
<s:param name="last" value="page.total" />
<s:iterator>
<option value="<s:property value="current-1" />">
<s:property value="current-1" />
</option>
</s:iterator>
</s:bean>
</select>
<script type="text/javascript">
var num = '${page.curPage}';
document.getElementById('page').options.selected = true;
</script>
第
<s:property value="%{page.curPage}" />
页 共
<s:property value="%{page.total}" />
页 共
<s:property value="%{page.sum}" />
记录
</div>
页:
[1]