後端分頁外掛pageBar 使用 springmvc下 hibernate
阿新 • • 發佈:2018-11-13
pagebar外掛,具體說明在
https://blog.csdn.net/xushiyu1996818/article/details/83656610
可以使用下載
controller
@RequestMapping(value="/showCanSellProductsAjax") public ModelAndView showCanSellProductsAjax(String currentPageNum)throws Exception{ PageBar pb=new PageBar(); pb.setEveryPageNum("1"); pb.setCurrentPageNum(currentPageNum); adminService.showCanSellProductsPage(pb); ModelAndView modelAndView = new ModelAndView("/admin/products_cansell_ajax"); modelAndView.addObject("pb", pb); return modelAndView; }
service
@Override public void showCanSellProductsPage(PageBar pb){ String hqlCount="select count(*) from Product where canSell=1 and num!=0"; pb.buildPage((int)(long)baseDao.uniqueQuery(hqlCount, null)); String hql="from Product where canSell=1 and num!=0"; pb.setResultList((List<Product>)baseDao. queryForPageListByHql(hql, pb.getCurrentPageNum(), pb.getEveryPageNum(), null)); }
dao
/** * hql單一值查值 * System.out.println((int)baseDao.uniqueQuery("Select id From Activity Where id= ?0", 1)); * 如果查詢的是count 返回的是long 需要(int)(long)base.uniqueQuery(); * @param hql * @param args * @return */ @Override public Object uniqueQuery(String hql,Object...args) { Session session = sessionFactory.openSession(); Query q = session.createQuery(hql); if (null!=args&&args.length>0) { for (int i = 0; i < args.length; i++) { q.setParameter(i, args[i]); } } Object result=q.uniqueResult(); if(result==null){ System.out.println("uniqueQuery query nothing"); } else{ System.out.println("uniqueQuery "+result.toString()); } session.close(); return result; } /** * 分頁查詢 * @param hql * @param pageindex 從1開始 * @param limit 每頁的數目 * @param args * @return */ @Override public List<?> queryForPageListByHql(String hql,int pageindex,int limit,Object...args) { List<?> lis =null; Session s = sessionFactory.openSession(); Query q = s.createQuery(hql); if (null!=args&&args.length>0) { for (int i = 0; i < args.length; i++) { q.setParameter(i, args[i]); } } q.setFirstResult((pageindex-1)*limit); q.setMaxResults(limit); lis = q.list(); System.out.println("queryForPageListByHql , find "+lis.size()+"條資料"); s.close(); return lis; }
jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>上架商品-ajax-分頁</title>
<!-- Tell the browser to be responsive to screen width -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Font Awesome -->
<link rel="stylesheet" href="${pageContext.request.contextPath}/conf1/css/font-awesome.min.css">
<!-- Ionicons -->
<link rel="stylesheet" href="${pageContext.request.contextPath}/conf1/css/ionicons.min.css">
<!-- Theme style -->
<link rel="stylesheet" href="${pageContext.request.contextPath}/conf1/dist/css/adminlte.min.css">
<!-- Google Font: Source Sans Pro -->
<link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700" rel="stylesheet">
</head>
<script type="text/javascript">
function downProduct(id){
var params = {
id : id
};
var nowUrl=getRealPath()+"/admin/downProductAjax.do";
$.ajax({
type : "post",
url : "/campus_second_ssh/admin/downProductAjax.do",//訪問路徑
dataType : "json",
contentType : "application/json",
data : JSON.stringify(params),
timeout : 5000,
error : alertSuccess,
global : false,
success : alertSuccess,//查詢成功處理函式
});
}
function alertSuccess(result) {
// 處理返回的資料result
alert(result.info);
alert(result.product.name);
//通過處理result返回的結果集顯示到頁面
}
function searchDataPage() {
var currentPageNum=$("#currentPageNum").val();
var url="${pageContext.request.contextPath}/admin/showCanSellProductsAjax.do?currentPageNum="+currentPageNum;
window.location.href=url;
}
</script>
<body>
<div class="wrapper">
<jsp:include page="side_admin.jsp"></jsp:include>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1>下架商品</h1>
</div>
</div>
</div><!-- /.container-fluid -->
</section>
<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
<h3 class="card-title">商品列表</h3>
</div>
<!-- /.card-header -->
<div class="card-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>商品編號</th>
<th>賣家名字</th>
<th>商品名字</th>
<th>商品種類</th>
<th>商品價格</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${pb.resultList }" var="product" >
<tr>
<td>${product.id }</td>
<td>${product.sellerName }</td>
<td>${product.name }</td>
<td>${product.categoryName }</td>
<td>${product.price }</td>
<td>
<button type="button" class="btn btn-primary"
onclick="downProduct(${product.id })">下架</button>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<%@ include file="/views/util/pageBar.jsp" %>
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
</div>
<!-- /.col -->
</div>
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
<footer class="main-footer">
<div class="float-right d-none d-sm-block">
<b></b>
</div>
<strong>Copyright © 2018 <a href="#">中央財經大學</a>.</strong> All rights
reserved.
</footer>
<!-- Control Sidebar -->
<aside class="control-sidebar control-sidebar-dark">
<!-- Control sidebar content goes here -->
</aside>
<!-- /.control-sidebar -->
</div>
<!-- ./wrapper -->
<input id="PageContext" type="hidden" value="${pageContext.request.contextPath}" />
<!-- jQuery -->
<script src="${pageContext.request.contextPath}/conf1/plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="${pageContext.request.contextPath}/conf1/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- FastClick -->
<script src="${pageContext.request.contextPath}/conf1/plugins/fastclick/fastclick.js"></script>
<!-- AdminLTE App -->
<script src="${pageContext.request.contextPath}/conf1/dist/js/adminlte.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="${pageContext.request.contextPath}/conf1/dist/js/demo.js"></script>
</body>
</html>