SSM分页查询

在做项目的时候,经常使用到分页查询,有的直接使用模板,这儿是实现最基本的用法,还有很多没优化,就在此写下基本能实现的功能。

我们创建一个工具类,用来实现分页需要的数据信息

package com.brick.squad.util;
public class Pagination {
private int skipNum;//当前第几条记录开始
private int takeNum;//从开始记录开始每页显示条数
private int curentPage;//当前页
private int pageSize;//每页显示记录数
public int getSkipNum() {

return (curentPage-1)*pageSize;
}

public int getTakeNum() {
return pageSize;
}
public int getCurentPage() {
return curentPage;
}
public void setCurentPage(int curentPage) {
this.curentPage = curentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

}

实现完成后,在controller 的类中,实现并显示


@Controller
@RequestMapping("/orders")
public class OrdersController {
@Autowired
@Qualifier("ordersService")
private OrdersService ordersService;

@RequestMapping("ordersPagination")
public String ordersPagination(Pagination pagination,HttpServletRequest request,Integer currentPage) throws Exception {
//获取总记录数
Integer count=ordersService.findOrdersCount();
request.setAttribute("count", count);
if(currentPage==null){
pagination.setCurentPage(1);
//设置每页显示多少条记录
pagination.setPageSize(3);
}else{
//设置当前第几页
pagination.setCurentPage(currentPage);
//设置每页显示多少条记录
pagination.setPageSize(3);
}
//总页数:向上取整
Integer totalPage=(int) Math.ceil(count/pagination.getPageSize());
request.setAttribute("totalPage", totalPage);
request.setAttribute("pagination" ,pagination);
List<Orders> ordersList=ordersService.ordersPagination(pagination);
request.setAttribute("ordersList", ordersList);
return "/backstage_managed/jsp/ordersList";
}

}

上面需要的service,以及mapper类等自己去实现就可以;

service实现方法:

@Override
/** Orders订单分页查询*/
public List<Orders> ordersPagination(Pagination pagination) throws Exception{
List<Orders> ordersList=ordersMapper.ordersPagination(pagination);
return ordersList;
}
@Override
/**查询订单总数*/
public Integer findOrdersCount() throws Exception {
Integer ordersCount=ordersMapper.findOrdersCount();
return ordersCount;
}

service接口:

/**
* 查询订单总数
* */
public Integer findOrdersCount()throws Exception;
/**
* Orders订单分页查询
* */
public List<Orders> ordersPagination(Pagination pagination)throws Exception;

mapper接口:

public void deleteOrdersById(String id);
/**
* 查询订单总数
* */
public Integer findOrdersCount();
/**
* Orders订单分页查询
* */
public List<Orders> ordersPagination(Pagination pagination);

mapper.xml配置文件:SQL语句

<sql id="Base_Column_List">
id, no, state_id stateId, production_date productionDate,
buy_id buyId,
number, money
</sql>

<select id="ordersPagination" parameterType="com.brick.squad.util.Pagination" resultType="Orders">
SELECT 
<include refid="Base_Column_List" />
FROM orders limit #{skipNum},#{takeNum}
</select>

完成上面的功能后便可以在页面中显示了:

<center>
<table border="1" style="text-align:center">
<th>订单ID</th>
<th>订单编号</th>
<th>订单状态</th>
<th>订单产生时间</th>
<th>买家ID</th>
<th>数量</th>
<th>总额</th>
</tr>
<c:forEach var="orders" items="${ordersList}">
<tr>
<td>${orders.id}</td>
<td>${orders.no}</td>
<td>${orders.stateId}</td>
<td>${orders.productionDate}</td>
<td>${orders.buyId}</td>
<td>${orders.number}</td>
<td>${orders.money}</td>
</tr>
</c:forEach>
</table>
<br />
<label>第${pagination.curentPage}/${totalPage}页
共${count}条</label> 
<a href="ordersPagination?currentPage=1">首页</a> 
<a href="ordersPagination?currentPage=${pagination.curentPage-1}"
οnclick="return checkFirst()">上一页</a> 
<a href="ordersPagination?currentPage=${pagination.curentPage+1}"
οnclick="return checkNext()">下一页</a> 
<a href="ordersPagination?currentPage=${totalPage}">尾页</a> 
跳转到: <input type="text" style="width:30px" id="turnPage" />页
<input type="button" οnclick="startTurn()" value="跳转" />
</center>
<script type="text/javascript">
function checkFirst() {
if (${pagination.curentPage>1}) {
return true;
}
alert("已到页首,无法加载更多");
return false;
}
function checkNext() {
if (${pagination.curentPage<totalPage}) {
return true;
}
alert("已到页尾,无法加载更多页");
return false;
}
function startTurn() {
var turnPage = document.getElementById("turnPage").value;
if (turnPage > ${totalPage}) {
alert("对不起已超过最大页数");
return false;
}
var shref = "ordersPagination?currentPage=" + turnPage;
window.location.href = shref;
}
</script>


注意:上面有的没有实现,比如service的bean,自己去实现。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值