IDEA_工具-Maven_项目:SSM 框架整合(Spring、SpringMVC、MyBatis)【2】及页面分页显示数据


注:【网页版】右上方的悬浮框( 有目录索引 )


一、基础配置

1-1】pom.xml

<!-- 分页工具 -->
	<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
	<dependency>
	    <groupId>com.github.pagehelper</groupId>
	    <artifactId>pagehelper</artifactId>
	    <version>5.1.2</version>
	</dependency>

1-2】mybatis-config.xml

<!--分页操作 [译:插件、拦截]-->
	<plugins>
	    <plugin interceptor="com.github.pagehelper.PageInterceptor" />
	</plugins>

1-3】service.EmployeeService

@Service
public class EmployeeService {

    @Autowired
    private EmployeeMapper employeemapper;

    /**
     * 查询所有数据的内容
     */
    public List<Employee> getAllInfo(){
        return employeemapper.selectByExampleWithDepartment(null);
    }

}

1-4】controller.EmployeeController

@Controller
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 查询所有数据的方法
     */
    @RequestMapping(value = "/emps",method = RequestMethod.GET)
    public String getALl(
            @RequestParam(value = "pn",defaultValue="1") Integer pn,
            Model model){
        // 当前页,和页数显示数量
        PageHelper.startPage(pn,5);
        List<Employee> allInfo = employeeService.getAllInfo();
        // 显示底部页码个数,五个
        PageInfo<Employee> objectPageInfo = new PageInfo<>(allInfo,5);
        model.addAttribute("pageInfo",objectPageInfo);
        return "list";
    }
}

二、jsp 页面(位于 WEB-INF/views 中)

2-1】外部资源

  • 下载 BootStrap 用于生产环境的 Bootstrap 并放在 webapp / static 包下
  • 下载 jQuery 放在 webapp / static / js 包下
  • jQuery-GitHub

2-2】H5 页面

<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <title>列表显示页</title>

    <link href="../../static/bootstrap-3.3.7-dist/css/bootstrap.min.css" type="text/css" rel="stylesheet" />

    <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 -->
    <!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/html5shiv.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dest/respond.min.js"></script>
    <![endif]-->

    <style type="text/css">
        div{
            /*border: 1px solid red;*/
        }
        .tableTheadTr_ThCenter th{
            text-align:center;
        }
    </style>
</head>
<body>
<div class="container">
    <!--标题行-->
    <div class="row">
        <div class="text-center col-xs-offset-3 col-xs-6">
            <h1>SSM-CRUD</h1>
            <!--<button class="btn btn-success">HelloWrold</button>
            <button type="button" class="btn btn-link">链接按钮</button>-->
        </div>
    </div>

    <!--按钮行-->
    <div class="row">
        <div class="col-xs-offset-8 col-xs-4 text-center">
            <button type="button" class="btn btn-primary">查询</button>
            <button type="button" class="btn btn-info">添加</button>
        </div>
    </div>
    <hr/>

    <!--数据显示行-->
    <div class="row">
        <table class="table table-hover text-center">
            <caption>查询表格</caption>
            <thead>
            <tr class="tableTheadTr_ThCenter">
                <th>#</th><th>empName</th><th>gender</th><th>email</th><th>departName</th><th colspan="2">操作</th>
            </tr>
            </thead>
            <tbody>
                <c:forEach items="${pageInfo.list}" varStatus="status" var="emp">
                    <tr class="${status.count%2==0?"success":"info"} tableTbodyTr_textLineHeight">
                        <td>${emp.empId}</td>
                        <td>${emp.empName}</td>
                        <td>${emp.gender=="M"?"男":"女"}</td>
                        <td>${emp.email}</td>
                        <td>${emp.department.deptName}</td>
                        <td><button type="button" class="btn btn-primary"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>&nbsp;编辑</button></td>
                        <td><button type="button" class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span>&nbsp;删除</button><td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>
    </div>

    <!--分页行-->
    <div class="row">
        <!--分页文字-->
        <div class="col-xs-5">
            当前第${pageInfo.pageNum}页,共${pageInfo.pages}页,共${pageInfo.total}记录
        </div>
        <!--分页条-->
        <div class="col-xs-7  text-center">
            <ul class="pagination">
                <li><a href="emps?pn=1">首页</a></li>
                <c:if test="${pageInfo.pageNum!=1}">
                    <li><a href="emps?pn=${pageInfo.pageNum-1}">&laquo;</a></li>
                </c:if>
                <c:forEach items="${pageInfo.navigatepageNums}" var="var">
                    <li><a href="emps?pn=${var}">${var}</a></li>
                </c:forEach>
                <c:if test="${pageInfo.pageNum!=pageInfo.pages}">
                    <li><a href="emps?pn=${pageInfo.pageNum+1}">&raquo;</a></li>
                </c:if>
                <li><a href="emps?pn=${pageInfo.pages}">末页</a></li>
            </ul>
        </div>
    </div>

</div>
</body>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
<script src="../../static/js/jquery-1.12.4.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="../../static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
</html>

猜你喜欢

转载自blog.csdn.net/ice_debj/article/details/104812259