ssm集合框架实现分页操作

实体类

package com.zs.oa.global;

import java.util.List;

public class PageBean<T> {
    private int currPage;//当前页数
    private int pageSize;//每页显示的记录数
    private int totalCount;//总记录数
    private int totalPage;//总页数
    private List<T> lists;//每页的显示的数据
    public PageBean() {
        super();
    }

    public int getCurrPage() {
        return currPage;
    }

    public void setCurrPage(int currPage) {
        this.currPage = currPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalCount() {
        return totalCount;
    }

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

    public int getTotalPage() {
        return totalPage;
    }

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

    public List<T> getLists() {
        return lists;
    }

    public void setLists(List<T> lists) {
        this.lists = lists;
    }

}

dao层

package com.zs.oa.dao;

import com.zs.oa.entity.Employee;
import org.apache.ibatis.annotations.Param;

import org.springframework.stereotype.Repository;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.HashMap;
import java.util.List;

@Repository("employeeDao")
public interface EmployeeDao extends JpaRepository{
    void insert(Employee employee);//插入
    void update(Employee employee);//修改
    void delete(String sn);//删除
    Employee select(String sn);//查询
    List<Employee> selectAll();//查询所有
    List<Employee> selectByDepartmentAndPost(@Param("dsn") String dsn,@Param("post")String post);
    int selectCount();//记录总数
    List<Employee> findByPage(HashMap<String,Object> map);//分页查找


}


配置文件

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zs.oa.dao.EmployeeDao">

    <resultMap id="employee" type="Employee">
        <id property="sn" column="sn" javaType="String"/>
        <result property="password" column="password" javaType="String"/>
        <result property="name" column="name" javaType="String"/>
        <result property="departmentSn" column="department_sn" javaType="String"/>
        <result property="post" column="post" javaType="String"/>
        <association property="department" column="department_sn" javaType="Department">
            <id property="sn" column="dsn" javaType="String"/>
            <result property="name" column="dname" javaType="String"/>
        </association>
    </resultMap>


    <insert id="insert" parameterType="Employee">
        insert into employee values(#{sn},#{password},#{name},#{departmentSn},#{post})
    </insert>

    <update id="update" parameterType="Employee">
        update employee set name=#{name},password=#{password},department_sn=#{departmentSn},post=#{post} where sn=#{sn}
    </update>

    <delete id="delete" parameterType="String">
        delete from employee where sn=#{sn}
    </delete>

    <select id="select" parameterType="String" resultMap="employee">
        select e.*,d.sn dsn,d.name dname from employee e left join department d on d.sn=e.department_sn
        where e.sn=#{sn}
    </select>

    <select id="selectAll" resultMap="employee">
       select e.*,d.sn dsn,d.name dname from employee e left join department d on d.sn=e.department_sn
    </select>


    <select id="selectByDepartmentAndPost" resultMap="employee">
        select e.*,d.sn dsn,d.name dname from employee e left join department d on d.sn=e.department_sn
        where e.sn is not NULL
        <if test="dsn!=null">
            and e.department_sn=#{dsn}
        </if>
        <if test="post!=null">
            and e.post=#{post}
        </if>
    </select>

    <select id="selectCount" resultType="int">
		select count(*) from employee
	</select>

    <select id="findByPage" parameterType="Map" resultMap="employee">
        select e.*,d.sn dsn,d.name dname
        from employee e left join department d on d.sn=e.department_sn
        <if test="start!=null and size!=null">
            limit #{start},#{size}
        </if>
    </select>
</mapper>

service层

package com.zs.oa.biz;


import com.zs.oa.entity.Employee;
import com.zs.oa.global.PageBean;

import java.util.List;

public interface EmployeeBiz{
    void add(Employee employee);//添加
    void edit(Employee employee);//编辑
    void remove(String sn);//删除
    Employee get(String sn);//查看员工信息
    List<Employee> getAll();//查看所有员工信息
    PageBean<Employee> findByPage(int currentPage);//分页查找


}

serviceImpl层

package com.zs.oa.biz.impl;

import com.zs.oa.biz.EmployeeBiz;
import com.zs.oa.dao.EmployeeDao;
import com.zs.oa.entity.Employee;
import com.zs.oa.global.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.List;

@Service("employeeBiz")
public class EmployeeBizImpl implements EmployeeBiz {
    @Qualifier("employeeDao")
    @Autowired
    private EmployeeDao employeeDao;
    @Override
    public void add(Employee employee) {
        employee.setPassword("000000");
        employeeDao.insert(employee);
    }

    @Override
    public void edit(Employee employee) {
        employeeDao.update(employee);
    }

    @Override
    public void remove(String sn) {
        employeeDao.delete(sn);
    }

    @Override
    public Employee get(String sn) {
        return employeeDao.select(sn);
    }

    @Override
    public List<Employee> getAll() {
        return employeeDao.selectAll();
    }

    @Override
    public PageBean<Employee> findByPage(int currentPage) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        PageBean<Employee> pageBean = new PageBean<Employee>();

        //封装当前页数
        pageBean.setCurrPage(currentPage);

        //每页显示的数据
        int pageSize=5;
        pageBean.setPageSize(pageSize);

        //封装总记录数
        int totalCount = employeeDao.selectCount();
        pageBean.setTotalCount(totalCount);

        //封装总页数
        double tc = totalCount;
        Double num =Math.ceil(tc/pageSize);//向上取整
        pageBean.setTotalPage(num.intValue());

        map.put("start",(currentPage-1)*pageSize);
        map.put("size", pageBean.getPageSize());
        //封装每页显示的数据
        List<Employee> lists = employeeDao.findByPage(map);
        pageBean.setLists(lists);

        return pageBean;
    }


}

Controller层

package com.zs.oa.controller;


import com.zs.oa.biz.DepartmentBiz;
import com.zs.oa.biz.EmployeeBiz;
import com.zs.oa.entity.Employee;
import com.zs.oa.global.Contant;
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.RequestParam;


import java.util.Map;

@Controller("employeeController")
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private DepartmentBiz departmentBiz;
    @Autowired
    private EmployeeBiz employeeBiz;

  /*  @RequestMapping("/list")
    public  String list(Map<String,Object> map){
        map.put("list",employeeBiz.getAll());
        return "employee_list";
    }*/
    @RequestMapping("/to_add")
    public  String toAdd(Map<String,Object> map){
        map.put("employee",new Employee());
        map.put("dlist",departmentBiz.getAll());
        map.put("plist", Contant.getPosts());
        return "employee_add";
    }
    @RequestMapping("/add")
    public  String add(Employee employee){
        employeeBiz.add(employee);
        return "redirect:list";
    }
    @RequestMapping(value ="/to_update",params = "sn")
    public  String toUpdate( String sn,Map<String,Object> map){
        map.put("employee",employeeBiz.get(sn));
        map.put("dlist",departmentBiz.getAll());
        map.put("plist", Contant.getPosts());
        return "employee_update";
    }
    @RequestMapping("/update")
    public  String update(Employee employee){
        employeeBiz.edit(employee);
        return "redirect:list";
    }
    @RequestMapping(value ="/remove",params = "sn")
    public  String remove( String sn){
        employeeBiz.remove(sn);
        return "redirect:list";
    }

    /*@RequestMapping("/list")
    public String  main(@RequestParam(value="currentPage",defaultValue="1",required=false)int currentPage, Map<String,Object> map){
        map.put("pagemsg",employeeBiz.findByPage(currentPage));
        return "employee_list";
    }*/

    /**
     * 分页查询
     * @param currentPage
     * @param map
     * @return
     */
    @RequestMapping("/list")
    public String  main(@RequestParam(value="currentPage",defaultValue="1",required=false)int currentPage, Map<String,Object> map){
        map.put("pagemsg",employeeBiz.findByPage(currentPage));
        return "layui";
    }
}

前端页面

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<jsp:include page="top.jsp"/>

<section id="content" class="table-layout animated fadeIn">
    <div class="tray tray-center">
        <div class="content-header">
            <h2> 员工列表 </h2>
            <p class="lead"></p>
        </div>
        <div class="admin-form theme-primary mw1000 center-block" style="padding-bottom: 175px;">
            <div class="panel  heading-border">
                <div class="panel-menu">
                    <div class="row">
                        <div class="hidden-xs hidden-sm col-md-3">
                            <div class="btn-group">
                                <button type="button" class="btn btn-default light">
                                    <i class="fa fa-refresh"></i>
                                </button>
                                <button type="button" class="btn btn-default light">
                                    <i class="fa fa-trash"></i>
                                </button>
                                <button type="button" class="btn btn-default light">
                                    <i class="fa fa-plus" onclick="javascript:window.location.href='/employee/to_add';"></i>
                                </button>
                            </div>
                        </div>
                        <div class="col-xs-12 col-md-9 text-right">
                            <div class="btn-group">
                                   <span>第${requestScope.pagemsg.currPage }/ ${requestScope.pagemsg.totalPage}页</span>
                                   <span>总记录数:${requestScope.pagemsg.totalCount }  每页显示:${requestScope.pagemsg.pageSize}</span>
                                   <c:if test="${requestScope.pagemsg.currPage != 1}">
                                       <a href="${pageContext.request.contextPath }/employee/list?currentPage=1">[首页]</a>  
                                       <a href="${pageContext.request.contextPath }/employee/list?currentPage=${requestScope.pagemsg.currPage-1}">[上一页]</a>  
                                   </c:if>

                                   <c:if test="${requestScope.pagemsg.currPage != requestScope.pagemsg.totalPage}">
                                       <a href="${pageContext.request.contextPath }/employee/list?currentPage=${requestScope.pagemsg.currPage+1}">[下一页]</a>  
                                       <a href="${pageContext.request.contextPath }/employee/list?currentPage=${requestScope.pagemsg.totalPage}">[尾页]</a>  
                                   </c:if>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="panel-body pn">
                    <table id="message-table" class="table admin-form theme-warning tc-checkbox-1">
                        <thead>
                        <tr class="">
                            <th class="text-center hidden-xs">Select</th>
                            <th class="hidden-xs">工号</th>
                            <th class="hidden-xs">姓名</th>
                            <th class="hidden-xs">所属部门</th>
                            <th class="hidden-xs">职务</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <c:forEach items="${requestScope.pagemsg.lists}" var="emp">
                        <tr class="message-unread">
                            <td class="hidden-xs">
                                <label class="option block mn">
                                    <input type="checkbox" name="mobileos" value="FR">
                                    <span class="checkbox mn"></span>
                                </label>
                            </td>
                            <td>${emp.sn}</td>
                            <td>${emp.name}</td>
                            <td class="text-center fw600">${emp.department.name}</td>
                            <td class="hidden-xs">
                                <span class="badge badge-warning mr10 fs11">${emp.post}</span>
                            </td>
                            <td>
                                <a href="/employee/to_update?sn=${emp.sn}">编辑</a>
                                <a href="/employee/remove?sn=${emp.sn}">删除</a>
                            </td>
                        </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</section>

<jsp:include page="bottom.jsp"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值