欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java ssm框架实现分页功能的示例代码(oracle)

程序员文章站 2022-05-28 14:13:53
java web 实现分页功能,分享给大家,具体如下: 使用框架:ssm 数据库:oracle 话说 oracle 的分页查询比 mysql 复杂多了,在这里简单谈一...

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
 krry_blog
 order by create_time desc
limit 0,5

 ssm 框架的搭建,就不多说了,以前的博客有详细介绍,这里就谈谈实现 java web 分页的功能。

用到插件 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<hashmap<string, object>> findblogs(params params);
 
 /**
 * 计算博客数量
 * com.krry.dao.admin 
 * 方法名:countblogs
 * @author krry 
 * @param params
 * @return int
 * @exception 
 * @since 1.0.0
 */
 public long countblogs(); 
}

blogmapper.xml

<?xml version="1.0" encoding="utf-8" ?>
<!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.krry.mapper.blogmapper" > 
 <!-- 分页查询所有的博客信息 -->
 <select id="findblogs" resulttype="java.util.hashmap" parametertype="params">
 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>
 
 <!-- 查询博客数量 -->
 <select id="countblogs" resulttype="long">
 select
  count(*)
 from
  krry_blog
 </select> 
</mapper>

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<hashmap<string, object>> findblogs(params params);
 
 /**
 * 计算博客数量
 * @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<hashmap<string, object>> findblogs(params params) {
 
 //查询博客信息
 list<hashmap<string, object>> blog = blogmapper.findblogs(params);
 
 
 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<hashmap<string, object>> blogs = blogservice.findblogs(params);
 //查询博客数量
 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<string, object> loaddata(params params){
 
 hashmap<string, object> map = new hashmap<string, object>();
 list<hashmap<string, object>> blogs = blogservice.findblogs(params);
 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);
%>

<!doctype html>
<html>
 <head>
 <title>分页</title>
 <style>
  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;}
 </style>
 </head>
 
 <body>
  <div class="ke_tabbox">
   <p class="titless">分页展示</p>
  <table class="ke_table">
   <thead>
    <tr>
     <th style="width:25%">标题</th>
     <th style="width:25%">音乐人</th>
     <th style="width:30%">音乐标题</th>
     <th style="width:20%">发布时间</th>
    </tr>
   </thead>
   
   <tbody id="tbody" data-itemcount="${coutblogs}">
    <c:foreach var="blog" items="${blogs}">
     <tr>
      <td><a class="t_avbiaoq" title="${blog.title}">${blog.title}</a></td>
      <td><a class="t_avbiaoq" title="${blog.musictor}">${blog.musictor}</a></td>
      <td><a class="t_avbiaoq" title="${blog.musictitle}">${blog.musictitle}</a></td>
      <td>${blog.createtime}</td>
     </tr>
    </c:foreach>
   </tbody>
  </table>
  <div id="krrypage"></div>
 </div>
 <script type="text/javascript" src="${basepath}/resource/js/jquery-1.11.3.min.js"></script>
 <script type="text/javascript" src="${basepath}/resource/js/krry_page.js"></script>
 <script type="text/javascript">var basepath = "${basepath}";</script>
 <script type="text/javascript">
  
  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+= "<tr>"+
        " <td><a class='t_avbiaoq' title='"+json.title+"'>"+json.title+"</a></td>"+
        " <td><a class='t_avbiaoq' title='"+json.name+"'>"+json.musictor+"</a></td>"+
        " <td><a class='t_avbiaoq' title='"+json.musictitle+"'>"+json.musictitle+"</a></td>"+
        " <td>"+json.createtime+"</td>"+
        "</tr>";
       }
       $("#tbody").html(html);
      }
     }
    });
   }
  };
  
  krryadminblog.initpage($("#tbody").data("itemcount"));
  
 </script>
 </body>
</html>

分页效果图:

java ssm框架实现分页功能的示例代码(oracle)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。