java ssm框架实现分页功能的示例代码(oracle)
java ssm框架实现分页功能的示例代码(oracle)
java web 实现分页功能,分享给大家,具体如下:
使用框架:ssm
数据库:oracle
话说 oracle 的分页查询比 mysql 复杂多了,在这里简单谈一下:
查询 前十条数据:
SELECT * FROM(
SELECT ROWNUM WN,RN.* FROM (
SELECT
id,
title,
create_time as createTime,
musictor,
musictitle
FROM
krry_blog
ORDER BY create_time desc
)RN
)WN
WHERE WN <= 10 AND WN > 0
语法较为复杂,
同样的结果,mysql 的语法是:用一个 LIMIT 就可以解决。
SELECT
id,
title,
create_time as createTime,
musictor,
musictitle
FROM
kMPWXgZLZGArry_blog
ORDER BY create_time desc
LIMIT 0,5
SSM 框架的搭建,就不多说了,以前的博客有详细介绍,这里就谈谈实现 java web 分页的功能。
用到插件 js : krry_page.js,还有jquery
mapper 持久层:
BlogMapper.java
package com.krry.mapper;
import java.util.HashMap;
import java.util.List;
import com.krry.entity.Params;
/**
*
* Mapper:操作数据库
* @author krry
* @version 1.0.0
*
*/
public interface BlogMapper {
/**
* 查询所有博客
* @param params
* @return
*/
public List
/**
* 计算博客数量
* com.krry.dao.admin
* 方法名:countBlogs
* @author krry
* @param params
* @return int
* @exception
* @since 1.0.0
*/
public long countBlogs();
}
BlogMapper.xml
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
SELECT * FROM(
SELECT ROWNUM WN,RN.* FROM (
SELECT
id,
title,
create_time as createTime,
musictor,
musictitle
FROM
krry_blog
ORDER BY create_time desc
)RN
)WN
WHERE WN <= #{pageSize} AND WN > #{pageNo}
SELECT
count(*)
FROM
krry_blog
service业务层:
接口类:IBlogService.java
package com.krry.service;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.ibatis.annotations.Param;
import com.krry.entity.Blog;
import com.krry.entity.Params;
/**
* service层:处理业务逻辑(impl里面实现)
* @author asusaad
*
*/
public interface IBlogService {
/**
* 分页查询所有博客
* @param params
* @return
*/
public List
/**
* 计算博客数量
* @param params
* @return
*/
public long countBlogs();
}
impl 实现类:BlogService.java
package com.krry.service.impl;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
import com.krry.entity.Params;
import com.krry.mapper.BlogMapper;
import com.krry.service.IBlogService;
/**
* 实现service层接口
* @author asusaad
*
*/
@Service
public class BlogService implements IBlogService{
@Autowired
private BlogMapper blogMapper;
/**
* 查询博客
*/
public List
//查询博客信息
List
return blog;
}
/**
* 计算博客数量
* @param params
* @return
*/
public long countBlogs(){
long coutBlogs = blogMapper.countBlogs();
return coutBlogs;
}
}
controller控制层:
KrryController.java
package com.krry.controller;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.krry.entity.Params;
import com.krry.service.IBlogService;
/**
* KrryController
* controller层,作为请求转发
* @author asusaad
*
*/
@Controller //表示是多例模式,每个用户返回的web层是不一样的
public class KrryController {
@Autowired
private IBlogService blogService;
/**
* 首页,并且分页查询
* @return
*/
@RequestMapping("/index")
public ModelAndView index(Params params){
params.setPageNo(0);
params.setPageSize(10); //一开始只查询10条
//调用业务层
List
//查询博客数量
long coutBlogs = blogService.countBlogs();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("blogs", blogs);
modelAndView.addObject("coutBlogs", coutBlogs);
modelAndView.setViewName("index");
return modelAndView;
}
/**
* ajax请求 的 分页查询
* @param params
* @return
*/
@ResponseBody
@RequestMapping("/loadData")
public HashMap
HashMap
List
map.put("blogs", blogs);
return map;
}
}
这里要有两个实体类,作为数据库查询的注入 Blog,还有分页查询的两个参数 Params:
设置data参数:pageNo(下一页):就是当前页数 * 下一页要显示的数量
pageSize(下一页):已经查询出来的数量(pageNo) + 每页要显示的数量
在数据库中是 WN <= pageSize and WN > pageNo 来查询分页数据
Blog.java
package com.krry.entity;
/**
*
* User
* @author krry
* @version 1.0.0
*
*/
public class Blog {
// 主键
private String id;
//博客标题
private String title;
private String musictor;
//音乐标题
private String musictitle;
//创建时间
private String createTime;
public Blog(String id, String title, String musictor, String musictitle,
String createTime) {
this.id = id;
this.title = title;
this.musictor = musictor;
this.musictitle = musictitle;
this.createTime = createTime;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getMusictor() {
return musictor;
}
public void setMusictor(String musictor) {
this.musictor = musictor;
}
public String getMusictitle() {
return musictitle;
}
public void setMusictitle(String musictitle) {
this.musictitle = musictitle;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
}
Params.java
package com.krry.entity;
/**
*
* Params
* @author krry
* @version 1.0.0
*
*/
public class Params {
private Integer pageSize = 0;
private Integer pageNo = 0;
public Integer getPageNo() {
return pageNo;
}
public void setPageNo(Integer pageNo) {
this.pageNo = pageNo;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
}
web 页面 index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
pageContext.setAttribute("basePath", basePath);
%>
body{background:url("resource/images/78788.jpg");background-size:cover;}
.titless{font-size: 34px;text-align: center;color: black;margin-bottom: 16px;}
.ke_tabbox{min-height:556px;width:900px;background:#f9f9f9;margin:20px auto 0;padding:6px;position:relative;}
.ke_tabbox .sendMy{text-align: center;
font-family: "微软雅黑";
font-size: 28px;
-webkit-text-fill-color: transparent;
background: -webkit-gradient(linear,left top,left bottom,from(#FD8700),to(#FF00B1));
-webkit-background-clip: text;
margin:8px auto 0;line-height: 35px;}
.ke_tabbox .ke_table{width:100%;margin-top: 26px;}
.ke_tabbox th{background:#ccc;font-weight:bold;}
.ke_tabbox .ke_table td,th{overflow: hidden;white-space: nowrap;text-overflow: ellipsis;border:1px solid #fff;padding:4px 12px;color:#666;font-size:12px;}
/*分页相关*/
.tzPage{font-size: 12px;position: absolute;top: 480px;right: 0px;}
#tbody tr:hover{background:#eaeaea;}
#tbody .t_mode{padding-right:4px;}
#tbody .t_avbiaoq:hover{color:#FF6857;transition:.4s}
#tbody .t_dele{padding-left:4px;}
.tzPage a{text-decoration:none;border:none;color:#7d7d7d;background-color:#f2f2f2;border-radius: 3px;}
.tzPage a:hover{background:#dd5862;color:#FFF;}
.tzPage a,.tzPage span{display:block;float:left;padding:0em 0.5em;margin-right:5px;margin-bottom:5px;min-width:1em;text-align:center;line-height: 22px;height: 22px;}
.tzPage .current{background:#dd5862;color:#FFF;border:none;border-radius: 3px;}
.tzPage .current.prev,.tzPage .current.next{color:#999;border:1px solid #e5e5e5;background:#fff;}
.tm_psize_go{margin-right:4px;float:left;height:24px;line-height:33px;position:relative;border:1px solid #e5e5e5;color:#999}
#tm_pagego{border-radius:3px;height:18px;width:30px;float:left;text-align:center;border:1px solid #e5e5e5;line-height: 22px;color:#999}
.sortdesc{border-top:5px solid;width:0px;height:0px;display:inline-block;vertical-align:middle;border-right:5px solid transparent;border-left:5px solid transparent;margin-left:5px;}
.sortasc{border-bottom:5px solid;width:0px;height:0px;display:inline-block;vertical-align:middle;border-right:5px solid transparent;border-left:5px solid transparent;margin-left:5px;}
.red{color:red}
.green{color:green}
.hideAdd{height: 300px;
text-align: center;
line-height: 300px;
margin-top: 16px;display:none;}
.hideAdd a{font-size:28px;-webkit-text-fill-color:transparent;background: -webkit-gradient(linear,left top,left bottom,from(#FD0051),to(#A22C93));-webkit-background-clip: text;}
&lMPWXgZLZGAt;/style>
<script type="text/javascript">var basePath = "${basePath}";
var krryAdminBlog = {
initPage:function(itemCount){
$("#krryPage").tzPage(itemCount, {
num_display_entries : 5, //主体页数
num_edge_entries : 4,//边缘页数
current_page : 0,//指明选中页码
items_per_page : 10, //每页显示多少条
prev_text : "上一页",
next_text : "下一页",
showGo:true,//显示
showSelect:false,
callback : function(pageNo, psize) {//会回传两个参数,第一个是当前页数,第二个是每页要显示的数量
krryAdminBlog.loadData(pageNo,psize);
}
});
},
//设置data参数:pageNo(下一页):就是当前页数 * 下一页要显示的数量
// pageSize(下一页):已经查询出来的数量(pageNo) + 每页要显示的数量
//在数据库中是 WN <= pageSize and WN > pageNo 来查询分页数据
loadData:function(pageNo,pageSize){
pageNo = pageNo * pageSize;
pageSize = pageNo + 10;
$.ajax({
type:"post",
url:basePath+"/loadData",
data:{pageNo:pageNo,pageSize:pageSize},
success:function(data){
if(data){
var html = "";
var blogArr = data.blogs;
for(var i=0,len=blogArr.length;i < len;i++){
var json = blogArr[i];
html+= "
"
"
"
"
"
}
$("#tbody").html(html);
}
}
});
}
};
krryAdminBlog.initPage($("#tbody").data("itemcount"));