Java自学之SSM:对数据分页展示

学习目的:对SSM框架查询到的数据进行分页展示。

Part 1

新建一个控制分页的类:Page

start:开始的位置

count:每页展示的数量

last:最后一页的位置

getlast():计算最后一页的开始位置

package cn.vaefun.util;

public class Page {
    int start = 0;
    int count = 5;
    int last = 0;

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getLast() {
        return last;
    }

    public void setLast(int last) {
        this.last = last;
    }

    public void getlast(int total) {
        // 假设总数是50,是能够被5整除的,那么最后一页的开始就是45
        if (0 == total % count)
            last = total - count;
            // 假设总数是51,不能够被5整除的,那么最后一页的开始就是50
        else
            last = total - total % count;
    }
}

Part 2

Category.xml

total:查看category_中的总记录数。

list:从start的位置开始,继续查询出接下来的count个数据。

<?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="cn.vaefun.mapper.CategoryMapper">
    <insert id="add" parameterType="Category" >
            insert into category_ ( name ) values (#{name})
        </insert>
    <select id="list" resultType="Category">
        select * from   category_
        <if test="start!=null and count!=null">
            limit #{start},#{count}
        </if>
    </select>
    <select id="total" resultType="int">
            select count(*) from category_
    </select>
</mapper>

Part 3

CategoryMapper

增加list(Page page),用于分页查询数据。

增加total(),用于查询总数据的数量。

add(Category category),用于添加测试用的数据。

package cn.vaefun.mapper;

import cn.vaefun.pojo.Category;
import cn.vaefun.util.Page;

import java.util.List;

public interface CategoryMapper {
    public List<Category> list();
    public List<Category> list(Page page);
    public int total();

    void add(Category category);
}

Part 4

CategoryService

package cn.vaefun.service;

import cn.vaefun.pojo.Category;
import cn.vaefun.util.Page;

import java.util.List;

public interface CategoryService {
    List<Category> list();
    int total();
    List<Category> list(Page page);
}

Part 5

CategoryServiceImpl

package cn.vaefun.service.impl;

import cn.vaefun.mapper.CategoryMapper;
import cn.vaefun.pojo.Category;
import cn.vaefun.service.CategoryService;
import cn.vaefun.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CategoryServiceImp implements CategoryService {
    @Autowired
    CategoryMapper mapper;
    @Override
    public List<Category> list() {
        return mapper.list();
    }
    @Override
    public List<Category> list(Page page) {
        // TODO Auto-generated method stub
        return mapper.list(page);
    }

    @Override
    public int total() {
        return mapper.total();
    }
}

Part 6

CategoryController

给listCategory()方法加上Page page形参,用于接受分页信息注入

调用list(page),查询数据保存到cs中

page.getlast(service.total),根据总数计算出随后一页的信息。

package cn.vaefun.controller;

import cn.vaefun.pojo.Category;
import cn.vaefun.service.CategoryService;
import cn.vaefun.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
@Controller
public class CategoryController {
    @Autowired
    CategoryService service;
    @RequestMapping("listCategory")
    public ModelAndView listCategory(Page page){
        ModelAndView mav = new ModelAndView("listCategory");
        List<Category> cs = service.list(page);
        int total = service.total();
        page.getlast(total);
        mav.addObject("page",page);
//        上一行代码可以不写
        mav.addObject("cs",cs);
        return mav;
    }
}

Part 7

修改listCategory.jsp,添加首页、上一页、下一页、末页等链接,以及防止超出范围的判断。

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" import="java.util.*"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<div style="width:500px;margin:0px auto;text-align:center">
    <table align='center' border='1' cellspacing='0'>
        <tr>
            <td>id</td>
            <td>name</td>
        </tr>
        <c:forEach items="${cs}" var="c" varStatus="st">
            <tr>
                <td>${c.id}</td>
                <td>${c.name}</td>
            </tr>
        </c:forEach>
    </table>
    <div style="text-align:center">
        <a href="?start=0">首  页</a>
        <a href="?start=${page.start-page.count<0?0:page.start-page.count}">上一页</a>
        <a href="?start=${page.start+page.count>page.last?page.last:page.start+page.count}">下一页</a>
        <a href="?start=${page.last}">末  页</a>
    </div>
</div>

Part 8

测试代码块,运行tes测试代码块,向数据库中添加数据。

    @Autowired
    private CategoryMapper categoryMapper;

    @Test
    public void testAdd() {
        for (int i = 0; i < 100; i++) {
            Category category = new Category();
            category.setName("new Category");
            categoryMapper.add(category);
        }

    }

Part 9

部署项目,在浏览器地址栏访问:localhost:8080/ssm_war_

v2-bd3289b1fc6bdb5e1adf7ca3f472f749_b.jpg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值