您的位置:首页 > 编程语言 > Java开发

后端分页插件pageBar 使用 springmvc下 hibernate

2018-11-02 16:25 267 查看
版权声明:此文章为许诗宇所写,如需转载,请写下转载文章的地址 https://blog.csdn.net/xushiyu1996818/article/details/83657208

pagebar插件,具体说明在

https://blog.csdn.net/xushiyu1996818/article/details/83656610

可以使用下载

controller

[code]@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

[code]@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

[code]/**
* 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

[code]<%@ 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 &copy; 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>

 

 

 

 

 

 

 

 

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: