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

网友投稿 269 2023-02-12


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> findBlogs(Params params);

/**

* 计算博客数量

* 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> 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> findBlogs(Params params) {

//查询博客信息

List> 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> 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 loadData(Params params){

HashMap map = new HashMap();

List> 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);

%>


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:intellij IDEA配置springboot的图文教程
下一篇:angular项目中bootstrap
相关文章

 发表评论

暂时没有评论,来抢沙发吧~