SSM系列教材 (二)- 分页示例

步骤1:先运行,看到效果,再学习
步骤2:模仿和排错
步骤3:基于前面的知识点
步骤4:本知识点效果
步骤5:分页类:Page
步骤6:Category.xml
步骤7:CategoryMapper
步骤8:CategoryService
步骤9:CategoryServiceImpl
步骤10:CategoryController
步骤11:listCategory.jsp
步骤12:增加100个对象,用于测试
步骤13:测试

步骤 1 : 先运行,看到效果,再学习

老规矩,先下载下载区(点击进入)的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。

步骤 2 : 模仿和排错

在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。 
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。 
采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。 

推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。 
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来 
这里提供了绿色安装和使用教程:diffmerge 下载和使用教程

步骤 3 : 基于前面的知识点

本知识点基于SSM整合进行

步骤 4 : 本知识点效果

访问页面看到如图所示效果

http://127.0.0.1:8080/ssm/listCategory

本知识点效果

步骤 5 : 分页类:Page

Page类用于存放分页信息:
start: 开始位置
count: 每页的个数
last: 最后一页的位置
caculateLast()方法: 通过总数total和每页的个数计算出最后一页的位置

package com.how2java.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 caculateLast(int total) {

        // 假设总数是50,是能够被5整除的,那么最后一页的开始就是45

        if (0 == total % count)

            last = total - count;

        // 假设总数是51,不能够被5整除的,那么最后一页的开始就是50

        else

            last = total - total % count;       

    }

}

步骤 6 : Category.xml

修改list,根据当有分页信息的时候,进行分页查询
增加total sql语句

<?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.how2java.mapper.CategoryMapper">

        <insert id="add" parameterType="Category" >

            insert into category_ ( name ) values (#{name})    

        </insert>

         

        <delete id="delete" parameterType="Category" >

            delete from category_ where id= #{id}   

        </delete>

         

        <select id="get" parameterType="_int" resultType="Category">

            select * from   category_  where id= #{id}    

        </select>

        <update id="update" parameterType="Category" >

            update category_ set name=#{name} where id=#{id}    

        </update>

        <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>

步骤 7 : CategoryMapper

增加total方法用于调用Category.xml 中total对应的sql语句
增加 list(Page page),根据分页来查询数据

package com.how2java.mapper;

  

import java.util.List;

import com.how2java.pojo.Category;

import com.how2java.util.Page;

  

public interface CategoryMapper {

  

    public int add(Category category);  

        

    public void delete(int id);  

        

    public Category get(int id);  

      

    public int update(Category category);   

        

    public List<Category> list();

     

    public List<Category> list(Page page);

     

    public int total();  

     

}

步骤 8 : CategoryService

增加total用于获取所有
增加 list(Page page),根据分页来查询数据

package com.how2java.service;

import java.util.List;

import com.how2java.pojo.Category;

import com.how2java.util.Page;

public interface CategoryService {

    List<Category> list();

    int total();

    List<Category> list(Page page);

}

步骤 9 : CategoryServiceImpl

实现total()和list(Page page) 方法

package com.how2java.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.how2java.mapper.CategoryMapper;

import com.how2java.pojo.Category;

import com.how2java.service.CategoryService;

import com.how2java.util.Page;

@Service

public class CategoryServiceImpl  implements CategoryService{

    @Autowired

    CategoryMapper categoryMapper;

     

    public List<Category> list(){

        return categoryMapper.list();

    }

    @Override

    public List<Category> list(Page page) {

        // TODO Auto-generated method stub

        return categoryMapper.list(page);

    }

    @Override

    public int total() {

        return categoryMapper.total();

    };

}

步骤 10 : CategoryController

修改listCategory,接受分页信息的注入

listCategory(Page page)


根据分页对象,进行查询获取对象集合cs

List<Category> cs= categoryService.list(page);


根据总数,计算出最后一页的信息

int total = categoryService.total();

package com.how2java.controller;

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.servlet.ModelAndView;

import com.how2java.pojo.Category;

import com.how2java.service.CategoryService;

import com.how2java.util.Page;

// 告诉spring mvc这是一个控制器类

@Controller

@RequestMapping("")

public class CategoryController {

    @Autowired

    CategoryService categoryService;

    @RequestMapping("listCategory")

    public ModelAndView listCategory(Page page){

     

        ModelAndView mav = new ModelAndView();

        List<Category> cs= categoryService.list(page);

        int total = categoryService.total();

         

        page.caculateLast(total);

         

        // 放入转发参数

        mav.addObject("cs", cs);

        // 放入jsp路径

        mav.setViewName("listCategory");

        return mav;

    }

}

步骤 11 : listCategory.jsp

修改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="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}">上一页</a>

        <a href="?start=${page.start+page.count}">下一页</a>

        <a href="?start=${page.last}">末  页</a>

    </div>

 </div>

步骤 12 : 增加100个对象,用于测试

修改MybatisTest 类,新增100个对象,用于分页测试

package com.how2java.test;

import java.util.List;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.how2java.mapper.CategoryMapper;

import com.how2java.pojo.Category;

import com.how2java.util.Page;

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:applicationContext.xml")

public class MybatisTest {

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

        }

    }

     

    @Test

    public void testTotal() {

        int total = categoryMapper.total();

        System.out.println(total);

    }

    @Test

    public void testList() {

        Page p = new Page();

        p.setStart(2);

        p.setCount(3);

        List<Category> cs=categoryMapper.list(p);

        for (Category c : cs) {

            System.out.println(c.getName());

        }

    }

}

步骤 13 : 测试

访问页面看到如图所示效果

http://127.0.0.1:8080/ssm/listCategory

测试


更多内容,点击了解: https://how2j.cn/k/ssm/ssm-pagination/1139.html

原文地址:https://www.cnblogs.com/Lanht/p/12789317.html