ssm框架实现分页功能

项目结构

bean里面是我们声明的类。UserHandler可以接受客户请求,然后调用业务逻辑组件,返回ModelAndViee,接着调用视图解析器找到ModelAndView对应的视图,将结果显示在客户端。dao是对数据层的操作,而service属于业务层。
ssm框架实现分页功能_第1张图片

定义一个分页信息类pageInfo

这个类里面包括页面上要显示的其它的信息,当前页数,总页数,总数据量,每页显示的数据量,如下图。使用泛型的原因是页面上可能会显示不同类的数据,这样显示不同类型数据时就不用定义一个新的分页类。

package com.zhongruan.bean;

import java.util.List;

public class PageInfo<T> {
    private List<T> list;
    private int totalPage;
    private int totalCount;
    private int size;
    private int currentPage;

    public PageInfo() {
    }

    public PageInfo(List<T> list, int totalPage, int totalCount, int size, int currentPage) {
        this.list = list;
        this.totalPage = totalPage;
        this.totalCount = totalCount;
        this.size = size;
        this.currentPage = currentPage;
    }

    public List<T> getList() {
        return list;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public int getSize() {
        return size;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    @Override
    public String toString() {
        return "PageInfo{" +
                "list=" + list +
                ", totalPage=" + totalPage +
                ", totalCount=" + totalCount +
                ", size=" + size +
                ", currentPage=" + currentPage +
                '}';
    }
}

用户类

package com.zhongruan.bean;

public class User {
    private int id;
    private String username;
    private String password;
    private int age;

    public User() {
    }

    public User(int id, String username, String password,int age) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.age=age;
    }



    public int getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public int getAge() {
        return age;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                '}';
    }
}

后台代码的实现

1.UserHandler.java
findAllByPage()的参数是从前端通过url传递的,是当前的页数。然后我们调用usrService里的findByPage方法,获取到当前页的分页信息


```java
@RequestMapping("/findAll03")
    public ModelAndView findAllByPage(@RequestParam(defaultValue = "1") int currentPage) {
        ModelAndView mv = new ModelAndView();
        //获取分页信息
        PageInfo pageInfo = userService.findByPage(currentPage);
        System.out.println(pageInfo);
        // 将数据返回到页面上
        mv.setViewName("pages/dataList");
        mv.addObject("pageInfo", pageInfo);
        return mv;
    }

2.UserService.java

package com.zhongruan.service;

import com.zhongruan.bean.PageInfo;
import com.zhongruan.bean.User;
import com.zhongruan.bean.Video;

import java.util.List;


public interface UserService {
    List<User> findAll();

    User login(User user);

    void deleteById(int id);

    User findById(int id);

    int update(User user);

    //获得用户列表页面的分页信息
    PageInfo<User> findByPage(int currentPage);

    void deleteAll(List list);

    PageInfo<User> findByPage(int currentPage,String username);

    
}

3.UserServiceImpl
实现接口UserService.java中的方法。

SELECT * FROM t_user LIMIT 0, 3
参数一  开始查询数据的索引  
参数二  表示查询的数量

页数     数据量     参数一
 1        5         05
 2		  5	        5, 5
					10, 5

我们每页的数据量是5个,那么第一页查出来的应该是数据库的前五条信息,两个参数分别为0和5,那么在第二页时,应该从第6个数据开始查询,也就是(2-1)*5=5,这时两个参数分别为5和5。在第n页时两个参数分别是(n-1)*5和5

public PageInfo<User> findByPage(int currentPage) {
        PageInfo<User> pageInfo = new PageInfo<>();
        //获取每页的数据量
        pageInfo.setSize(5);

        //获取总数据量
        int totalCount = userDao.getTotal();//中断
        pageInfo.setTotalCount(totalCount);
        //获取总页数
        int totalPage = (int)Math.ceil(totalCount/(double)pageInfo.getSize());
        pageInfo.setTotalPage(totalPage);

        //判断当前页是否合理
        if(currentPage<1){
            pageInfo.setCurrentPage(1);
        }else if(currentPage>totalPage){
            pageInfo.setCurrentPage(totalPage);
        }else {
            pageInfo.setCurrentPage(currentPage);
        }

        int start = (pageInfo.getCurrentPage()-1)*pageInfo.getSize();
        //查询当前页面下所有的用户信息
        List<User> list = userDao.findByPage(start,pageInfo.getSize();
        pageInfo.setList(list);
        return pageInfo;
    }

4.UserDao.java

package com.zhongruan.dao;
import com.zhongruan.bean.User;
import com.zhongruan.bean.Video;
import org.apache.ibatis.annotations.Param;

import java.util.List;


public interface UserDao {
    //查询全部用户的方法
    List<User> findAll();

    User checkUser(User user);

    void deleteById(int id);

    User findById(int id);

    int update(User user);
	//获取总数据量
    int getTotal();
    
	//获取当前页面下所有用户信息
    List<User> findByPage(@Param("start") int start,@Param("size") int size);

    void deleteAll(List list);

    int getTotal(String username);

	List<User> findByPage(@Param("start") int start,@Param("size") int size,@Param("username") String username);

  
}

5.UserDao.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.zhongruan.dao.UserDao">
    <select id="findAll" resultType="com.zhongruan.bean.User">
        select * from t_user
    </select>
    <select id="checkUser" parameterType="com.zhongruan.bean.User" resultType="com.zhongruan.bean.User">
        select * from t_user where username=#{username} and password=#{password}
    </select>

    <delete id="deleteById" parameterType="int">
        delete from t_user where id=#{id}
    </delete>

    <select id="findById" parameterType="int" resultType="com.zhongruan.bean.User">
        select * from t_user where id=#{id}
    </select>

    <update id="update" parameterType="com.zhongruan.bean.User">
        update t_user set username = #{username},password = #{password},age = #{age} where id = #{id}
    </update>
		<!--获取总数据量-->
	    <select id="getTotal" resultType="int">
	        select count(*) from t_user
	    </select>
		<!--获取当前页的信息-->
	    <select id="findByPage" parameterType="int"  resultType="com.zhongruan.bean.User">
        select * from t_user limit #{start},#{size}-->
       </select>

    <delete id="deleteAll" parameterType="List">
        delete from t_user where id in 
        <foreach collection="list" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>

    <select id="getTotal" resultType="int">
        select count(*) from t_user
        <if test="username != null and username != ''">
            where username like concat("%", #{username}, "%")
        </if>
    </select>


</mapper>

前端代码

<div class="box-footer">
                    <div class="pull-left">
                        <div class="form-group form-inline">
                            总共${pageInfo.totalPage} 页,共${pageInfo.totalCount} 条数据。 每页
                            <select class="form-control">
                                <option>1</option>
                                <option>2</option>
                                <option>3</option>
                                <option>4</option>
                                <option>5</option>
                            </select></div>
                    </div>

                    <div class="box-tools pull-right">
                        <ul class="pagination">
                            <li>
                                <a href="${pageContext.request.contextPath}/user/findAll03?currentPage=1" aria-label="Previous">首页</a>
                            </li>
                            <li><a href="${pageContext.request.contextPath}/user/findAll03?currentPage=${pageInfo.currentPage-1}">上一页</a></li>
                            <c:forEach begin="1" end="${pageInfo.totalPage}" var="pageNum">
                                <li><a href="${pageContext.request.contextPath}/user/findAll03?currentPage=${pageNum}">${pageNum}</a></li>
                            </c:forEach>


                            <li><a href="${pageContext.request.contextPath}/user/findAll03?currentPage=${pageInfo.currentPage+1}">下一页</a></li>
                            <li>
                                <a href="${pageContext.request.contextPath}/user/findAll03?currentPage=${pageInfo.totalPage}" aria-label="Next">尾页</a>
                            </li>
                        </ul>
                    </div>

                </div>

运行结果

ssm框架实现分页功能_第2张图片
ssm框架实现分页功能_第3张图片

你可能感兴趣的:(java,mybatis,spring)