MyBatis+Struts2分页查询

 xml

<mapper namespace="com.bdqn.dao.GoodsMapper">
<!-- 这个分页查询方法语句有个小于号是绝对报错的,所以把它替换成&lt; -->
	<select id="fenye" resultType="Goods">
		SELECT * FROM  (  SELECT A.*, ROWNUM RN  FROM (SELECT * FROM goods) A  
		WHERE ROWNUM &lt; #{one} ) WHERE RN >= #{two}
	</select>
</mapper>

dao

//分页方法:这两个one和two不是第几页,也不是每页几条,这是通过index和size计算出来的,笔记在有道笔记
	List<Goods> fenye(@Param("one")int one,@Param("two")int two);

action

package com.bdqn.action;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.SqlSession;
import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSONArray;
import com.bdqn.dao.GoodsMapper;
import com.bdqn.entity.Goods;
import com.bdqn.utils.MyBatis;
import com.opensymphony.xwork2.ActionSupport;


public class GoodsAction extends ActionSupport{
//默认设置第一次访问第一个页面
	private int index = 1;
	//每页显示两条数据
	private int size = 2;
	//一共有几页
	private int count = 1;

//分页查询方法
	public String fenye() throws Exception {
		SqlSession session = MyBatis.createSqlSession();
		//先获取数据库该表有几条数据total,直接写一个select count(1) from goods就可以了
		int total = session.getMapper(GoodsMapper.class).findCount();
		//用页面量size和total来计算一共该显示几页,就算最后一页只有一条数据,也应该用一页来显示,所以必须先计算total/size是不是等于0
		count = total%size==0?total/size:(total/size)+1;
		//当一直点击上一页,到了第一页时就不能让index继续减小,因为没有第0页
		if (index<1) {
			index=1;
		//同理到了最大页数的时候也不能让他继续加大
		}else if(index>count){
			index=count;
		}
		//这是计算分页语句需要的参数值小于第几条数据
		int one = (((index-1)*size)+1)+size;
		//这是计算该从第几条数据开始显示
		int two = ((index-1)*size)+1;
		
		//分页方法
		List<Goods> list = session.getMapper(GoodsMapper.class).fenye(one, two);
		//使用完关闭下
		MyBatis.closeSqlSession(session);
		
		//获取request和sqlsession 
		HttpServletRequest request= ServletActionContext.getRequest();
		HttpSession session1 = request.getSession();
		
		//在把值存入session之前先清空原先的值
		session1.removeAttribute("list");
		session1.setAttribute("list", list);

		return SUCCESS;
	}

public int getIndex() {
		return index;
	}
	public void setIndex(int index) {
		this.index = index;
	}
	public int getSize() {
		return size;
	}
	public void setSize(int size) {
		this.size = size;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
}

页面代码:

<div style="width: 400px;height: 400px;position: absolute;top: 50%;left: 50%;margin-left: -200px;margin-top: -200px;border: 1px solid black">
    	<table border="2">
    	<tr><td><a href="/封装变量/Users1Action/findALLAction"><button>全部查询</button></a></td></tr>
    	<tr>
    	<td>ID</td>
    	<td>商品名</td>
    	<td>价格</td>
    	<td>操作</td>
    	</tr>
    	<c:forEach items="${list}" var="li" varStatus="str">
    		<tr>
    			<td>${li.id}</td>
    			<td>${li.name}</td>
    			<td>${li.price}</td>
    			<td>
    			<a href="/封装变量/DeleteGoods?Delete=${li.id}"><button>删除</button></a>
    			<a><button name="updateButton" value="${li.id}">修改</button></a>
    			</td>
    		</tr>
    	</c:forEach>
    	</table>
    	<p>
    	共${count}/${index}页
    	<a href="/封装变量/GoodsAction/fenye?index=1&size=${size}&count=${count}"><button>首页</button></a>
    	<a href="/封装变量/GoodsAction/fenye?index=${index-1}&size=${size}"><button>上一页</button></a>
    	<a href="/封装变量/GoodsAction/fenye?index=${index+1}&size=${size}"><button>下一页</button></a>
    	<a href="/封装变量/GoodsAction/fenye?index=${count}&size=${size}"><button>尾页</button></a>
    	</p>
    	
    </div>

猜你喜欢

转载自blog.csdn.net/jinqianwang/article/details/82145001