自我学习SSM框架集成(二)

手动SQL方式分页 

 

一.首先来看看最重要的Page类。

首先我们让start默认为0,count=5。count为一页的容量,而令start为0,是如果浏览器访问不输入start参数就默认为从头开始浏览页信息

而public void caculateLast是用来获取最后一页的开始 ,这样就可以一键点到最后一页

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

 

 

二、然后再看控制器

控制器先拦截路径/listCategory的访问,然后Spring将自动注入一个Page,若有start参数则是start参数,若没有则start=0

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

}

 

其中通过.list.(page)来获取查询也得Category内容,这里跳过中间步骤直接看其SQL语句

这里通过start 和count则可获取Page里的内容,并List来捕捉获取集合。

    <select id="list" resultType="Category">
        select * from   category_
        <if test="start!=null and count!=null">
            limit #{start},#{count}
        </if>
    </select>

而int total = categoryService.total();则是将最后一页的第一个内容注入Page类

 

 

三、最后则跳转到页面  这里代码就不解释了

<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}">上一页</a>
        <a href="?start=${page.start+page.count}">下一页</a>
        <a href="?start=${page.last}">末  页</a>
    </div>
</div>

 

 

 

然后还可以通过PageHelper来简易完成分页

applicationContext添加  

<property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <!--使用下面的方式配置参数,一行配置一个 -->
                        <value>
                        </value>
                    </property>
                </bean>
            </array>
        </property>

控制器可以简化为  直接通过4、5、6行完成分页以及获取总条数

 1 public ModelAndView listCategory(Page page){
 2 
 3         ModelAndView mav = new ModelAndView();
 4         PageHelper.offsetPage(page.getStart(),5);
 5         List<Category> cs= categoryService.list();
 6         int total = (int) new PageInfo<>(cs).getTotal();
 7 
 8         page.caculateLast(total);
 9 
10         // 放入转发参数
11         mav.addObject("cs", cs);
12         // 放入jsp路径
13         mav.setViewName("listCategory");
14         return mav;
15     }

 

posted @ 2019-01-11 21:19  饼先生  阅读(245)  评论(0编辑  收藏  举报