Springmvc联合查询+分页+条件搜索+页⾯局部刷新Spring mvc+Hibernate 实现后台数据查询分页
jquery+jsp实现页⾯局部刷新
框架已经搭好
⼀、数据联合查询
联合查询的表结构应相似,利⽤表别名和构照的联合信息表存储查询结果
1.定义联合查询的实体表UnionStudent.java
class UnionStudent{
private String name;
private String sex;
getter()..
jquery框架使用setter()..
}
2.定义分页类PageConfig.java
class PageConfig{
private static int pageSize=10;//每页显⽰10条记录
private static int pageNo=1;//其实页为1
}
3.⽅法实现类  UnionStudentServiceImpl.java
@service
@Transactional(rollBackFor=RuntimeException.class)
class UnionStudentServiceImpl implments UnionStudentService{
@Autowired
private UserDao userDao;
/**
*分页⽅法
*pageConfig 分页类
*sex 搜索条件
*/
@Override
public List<UnionStudent> getInfoForPage(PageConfig pageConfig,String sex){
StringBuffer sBuffer=new StringBuffer();
String sql1="select t1.studentName as name,t2.studentSex as sex from student1 t1 ";
String sql1="select t2.studentName as name,t2.studentSex as sex from student2 t2 ";
String sql1="select t3.studentName as name,t3.studentSex as sex from student3 t3 ";
String conn="union all  ";
if(sex!=null){
sBuffer.append("select * from  ( ").append(sql1).append(conn).append(sql2).append(conn).append(sql3).append( " )  where sex='"+sex+"' ");
}else{
sBuffer.append(sql1).append(conn).append(sql2).append(conn).append(sql3);
}
Query query = userDao
.String())
.addScalar("name", StringType.INSTANCE)
.addScalar("sex ", StringType.INSTANCE)
.setResultTransformer(
Transformers.aliasToBean(UnionStudent.class));
query.setFirstResult((pageConfig.pageNo - 1) * pageConfig.pageSize);
query.setMaxResults(pageConfig.pageSize);
List<UnionStudent> list = query.list();
return list;
}
/**
*获取满⾜要求的数据条数
*sex 搜索条件
*/
@Override
public int getSize( String sex){
StringBuffer sBuffer=new StringBuffer();
String sql1="select t1.studentName as name,t2.studentSex as sex from student1 t1 ";
String sql1="select t2.studentName as name,t2.studentSex as sex from student2 t2 ";
String sql1="select t3.studentName as name,t3.studentSex as sex from student3 t3 ";
String conn="union all  ";
if(sex!=null){
sBuffer.append("select * from  ( ").append(sql1).append(conn).append(sql2).append(conn).append(sql3).append( " )  where sex='"+sex+"' ");
}else{
sBuffer.append(sql1).append(conn).append(sql2).append(conn).append(sql3);
}
Query query = userDao
.String())
.addScalar("name", StringType.INSTANCE)
.addScalar("sex ", StringType.INSTANCE)
.setResultTransformer(
Transformers.aliasToBean(UnionStudent.class));
List<UnionStudent> list = query.list();
if (list != null && list.size() != 0) {
return list.size();
} else {
return 0;
}
}
}
4.Spring控制器类studentController.java
@Controller
public class studentController{
@Autowired
private UnionStudentService unionStudentService;
@RequsetMapping(value="/forPage")
@ResponseBody
public Map<String,Object>  forPage(HttpServletRequest request,Model model){
String sex = Parameter("sex");
String curPage = Parameter("currentPage");
PageConfig pageConfig = new PageConfig();
if (StringUtils.hasText(curPage)) {
pageConfig.pageNo = Integer.parseInt(curPage);
}
List<UnionStudnet> unionStudnetList = ForPage(pageConfig, sex);
int allPage = Size(sex);
int pageSize = (int) il((double) allPage / pageConfig.pageSize);
Map<String, Object> map = new HashMap<String, Object>();
map.put("currentPage", pageConfig.pageNo);
map.put("allPage", allPage);
map.put("pageSize", pageSize);
map.put("unionStudnetList", unionStudnetList);
return map;
}
}
5.界⾯显⽰index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = ContextPath();
String basePath = Scheme()+"://"+ServerName()+":"+ServerPort()+path+"/"; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script src="<%=basePath%>statics/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
//选择页⾯
$(document).ready(function(){
$("#pageNav a").click(function(){
var idx = $("#pageNav a").index($(this));//返回当前元素在导航的超链接集合中索引
var curPageNo=parseInt($("#currentPage").html());//当前页数,因为要计算,注意类型转换
var pageSize=$("#pageSize").html();//总页数
var pageNo=1;//要请求的页⾯
switch(idx){
case 0:
pageNo=1;
break;
case 1:
pageNo=curPageNo-1;
break;
case 2:
pageNo=curPageNo+1;
break;
case 3:
pageNo=pageSize;
break;
}
if(pageNo>pageSize){
return;
}else if(pageNo<=0){
return;
}
$.ajax({
type:'POSt',
url:"<%=basePath%>forPage",
data:"sex="+sex+"¤tPage="+pageNo,
success:function(json){
$("#currentPage").html(json.currentPage);
var size=json.pageSize;
$("#pageSize").html(size);
if(size==0){
$("#selectPageUlNull").show();
$("#selectPageUlInfo").hide();
}else{
$("#selectPageUlNull").hide();
$("#selectPageUlInfo").show();
}
$("#productInfoArea").remove();//删除⾮第⼀⾏的元素
$("#productInfoAreaCenter").html("<ul id='productInfoArea'></ul>");
$.each(json.unionStudnetList,function(idx,unionStudnet){
$("#productInfoArea").append("<li>+"+unionStudnet.name+"+</li>"+"<li>+"+unionStudnet.sex+"+</li>");                });
},
dataType: 'json',
async:false
});
});
});
</script>
</head>
<body>
<div class="search-list" id="productInfoAreaCenter">
<ul id="productInfoArea"></ul>
</div>
<ul class="paging tc pdv30" id="selectPageUlNull" >
<span>查询信息不存在!</span>
</ul>
<ul class="paging tc pdv30" id="selectPageUlInfo">
<span>共</span>
<span id="pageSize">${pageSize}</span>
<span>页</span>    
<span>当前第</span>
<span id="currentPage">${currentPage}</span>
<span>页</span>
  
<span id="pageNav">
<a href="javascript:void(0)">⾸页</a>
<a href="javascript:void(0)">上⼀页</a>
<a href="javascript:void(0)">下⼀页</a>
<a href="javascript:void(0)">尾页</a>
</span>
<span>跳转到第</span>
<span>
<select name="pages"  id="pageSelect" οnchange="pageChange()"  boxWidth="60" selWidth="40"> <c:forEach var="index" begin="1" end="${pageSize}" step="1">
<c:if test="${currentPage==index}">
<option value="${index}" selected="selected">${index}</option>
</c:if>
<c:if test="${currentPage!=index}">
<option value="${index}">${index}</option>
</c:if>
</c:forEach>
</select>
</span>
<span>页</span>
</ul>
</body>
<html>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。