1.基于maven管理的情况下SSH的分页实现
1.struts2进行前端请求拦截 具体的情况如下,要用到具体的struts2相关配置
1.1web.xml中的配置不多说,我上传过很多次了;
1.2.根据struts2的拦截请求找到 相应的struts.xml文件
主要要导入json-struts包 ,因为后端使用json进行传输数据
</package>
<package name="Products" namespace="/Products" extends="json-default" >
<action name="getList1" class="com.web.Check" method="getlist"/>
</package>根据前端请求 找到getlist方法
public void getlist() {
int conut=imp1.conut();
// System.out.println(111111);
PageUtil pageU=new PageUtil(1, conut,10 );
PageUtil p=imp1.listByPage(pageU);
List<Product> vipList= (List<Product>) p.getList();
JSONObject arr=new JSONObject();
arr.put("total", conut);
JSONArray jArray =JSONArray.fromObject(vipList);
arr.put("rows", jArray);
// System.out.println(arr.toString());
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
try {
response.getWriter().write(arr.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
2,spring对bean进行管理,如ioc 和aop 包括dao层和service层
Dao层的数据库请求采用的是hibernate分页查询的方法,采用ORM对象映射,和mysql分页查询有很大的区别
以下为具体的Dao层的分页代码
SessionFactory 以前由hibenate创建 现在由Spring创建 ,现在只有注入即可
@Autowired
SessionFactory sessionFactory;
public Session getsession(){
System.out.println(sessionFactory);
return sessionFactory.openSession();
}
public int conut() {
Session session = this.getsession();
String hqlCount="select count(productid) from Product ";
long lon= (long) session.createQuery(hqlCount).uniqueResult();
int count=(int) lon;
return count;
}
@Override
public PageUtil listByPage(PageUtil pageU) {
Session session = this.getsession();
String hql = " from Product";
Query query=session.createQuery(hql);
/*
setFirstResult(int firstResult) 设置返回的结果集从第几条记录开始
setMaxResult(int maxResult) 设置本次查询返回的结果数目
*/
query.setFirstResult(pageU.getRowStart());//将设置的参数返回
query.setMaxResults(pageU.getPageSize());
pageU.setList(query.getResultList());
return pageU;
}
3.需要一个分页类
public class PageUtil {
private int pageSize=3;//每页大小
private int currPage=1;//当前页数
private int count;//总记录数
private int countPage;//总页数
private int rowStart;//limit ? ,2; ?代表:rowStart (当前页数-1)*页大小
private List<?> list;//用于封装查询的数据,对其完成分页功能 ?代表示知的数据类型
public PageUtil(int currPage,int count,int pageSize){
//设定每页显示的大小数目,如果大于0 ,以传值为准。否则以默认为准
if(pageSize>0){
this.pageSize=pageSize;
}
this.currPage=currPage;
this.rowStart=(this.currPage-1)*this.pageSize;
this.count=count;//总记录数
//求出总页数
if(this.count%pageSize>0){
countPage=count/pageSize+1;
}else{
countPage=count/pageSize;
}
}
public static void main(String[] args) {
//当前总数是10条,
//每页显示2条数
//总共多少页: 5
int pageSize=2;//每页大小
int count=11;//总数是9
int countPage=0;//求:总共多少页
if(count%pageSize>0){
countPage=count/pageSize+1;
}else{
countPage=count/pageSize;
}
System.out.println("总共:"+countPage);
}
4.至于spring配置文件 如aplication.xml中主要是hibernate 数据源是c3p0
<!-- 注册jdbc.properties方法 2种:context方式注册-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 数据源配置:可以C3p0-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
<bean id="SessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" lazy-init="false" >
<!-- 注入datasource,给sessionfactoryBean内setdatasource提供数据源 -->
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
至于具体的hibernate.cfg.xml 和具体的映射类如 srudnt.hbm.xml很简单 ,就不说了;
hibernate相关配置只需要仔细点就可以,
前端的是用easyui写的因此需要Jquery easy ui 库
title>后台商品列表</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/js/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/js/easyui/themes/icon.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/js/easyui/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/product1.css"></script>
<script type="text/javascript">
$(function(){
var dataJson={"total":28,"rows":[{"id":"1","name":"Koi1","age":20},{"id":"2","name":"Koi2","age":21},{"id":"3","name":"Koi3","age":22}]};
$('#dg').datagrid({
//data: dataJson,
url:"${pageContext.request.contextPath}/Products/getList1.action",
pagination:true,
pageList: [2,6,10],
columns:[[
{field:'pname',title:'商品名称',width:100},
{field:'marketprice',title:'售价',width:100,align:'left'},
{field:'stock',title:'库存',width:100,align:'left'},
{field:'collect',title:'收藏量',width:300,align:'left'},
{field:'ptime',title:'上架时间',width:100,align:'left'},
{field:'photo',title:'产品示例',width:100,align:'left'},
{field:'content',title:'商品描述',width:300,align:'left'},
{field:'operate',title:'操作',align:'center',width:$(this).width()*0.1,
formatter:function(value, row, index){
var str1 = '<a href="${pageContext.request.contextPath}/Products/deleteProduct.action'+'?productid=${prd.productid}'+' name="opera" class="easyui-linkbutton" >删除</a>';
var str = '<a href="${pageContext.request.contextPath}/Products/checkDetail.action'+'?productid=${prd.productid}'+' name="opera" class="easyui-linkbutton" >修改</a>';
return str+" "+str1;
}}
]]
});
});
function doSearch(){
$('#dg').datagrid('load',{
itemid: $('#itemid').val(),
productid: $('#productid').val()
});
}
</script>
<style type="text/css">
a{
text-decoration: none;
}
</style>
</head>
<body>
<div id="tb" style="padding:3px">
<span>Item ID:</span>
<input id="itemid" style="line-height:26px;border:1px solid #ccc">
<span>Product ID:</span>
<input id="productid" style="line-height:26px;border:1px solid #ccc">
<a href="#" class="easyui-linkbutton" plain="true" οnclick="doSearch()">查询</a>
</div>
<div style="width: 1250px;height: 400px;" >
<table id="dg" align='center' ></table>
</div>
基本流程就是这样,想要源代码,可以在我的下载里面找,ssh.rar有具体的源码!