1. pagehelper分页的使用
Controller层
/**
* 跳转到应用列表页面
* @param pageNo 要显示第几页内容
* @param pageSize 一页显示多少条
* @param model
* @return
*/
@RequestMapping("/toApplicationList.html")
public String toApplicationList(int pageNo, int pageSize, Model model){
-------------------- 主要内容 ----------------------------
PageInfo<Applications> page = applicationsService.getApplicationList(pageNo,pageSize);
---------------------------------------------------------
model.addAttribute("pageInfo", page);
return "backend/applications/application_list";
}
service层
/**
* 查询所有的应用
* @param pageNo
* @param pageSize
* @return
*/
@Override
//注意这里,这里设置了泛型为<Applications>,有时候有也可以不加,一般情况尽量加上,规范一点
public PageInfo<Applications> getApplicationList(int pageNo, int pageSize) {
1. --------------------- 主要内容 --------------------------
作用:开启分页查询,写在查询语句上方
注意:只有紧跟在 PageHelper.startPage 方法后的第一个 MyBatis 的查询(select)方法会被分页
方法: startPage(int pageNum, int pageSize, String orderBy)
pageNo:要显示第几页内容
pageSize:一页显示多少条
orderBy :排序(设置根据什么字段什么规则排序)
PageHelper.startPage(pageNo, pageSize, "oaao.create_time desc");//这里的oaao是表名的缩写
pageNo:要显示第几页内容
pageSize:一页显示多少条
--------------------------------------------------------
List<Applications> list = applicationsMapper.getApplicationPage();
2. ----------------------- 主要内容 ------------------------
//用PageInfo对结果进行包装
PageInfo<Applications> page = new PageInfo<Applications>(list);
--------------------------------------------------------
return page;
-------- 这里a和b是两个值相同但是对象类型不同的对象(BeanUtils.copyProperties(a, b);) -----
PageInfo pageInfo = new PageInfo(a);//因为查询的时候是用a对象查询的,所以我们分页之类的数据都在这里面
pageInfo.setList(b);//把我们真正需要的数据(我们前端需要这样格式的数据)赋值进去
return pageInfo;
}
------------------------------------------------------------------------
例:分页(Controller)
@GetMapping("/listPage")
public ModelAndView infoListPage(HttpSession session, Map<String, Object> map,
@RequestParam(value="pageNo", required = false, defaultValue = "0") Integer pageNo,
@RequestParam(value="pageSize", required = false, defaultValue = "10") Integer pageSize){
String contactPhone = (String) session.getAttribute("contactPhone");
PageInfo<ActivityTeamLoginOutputDTO> activityTeamLoginOutputDTOList = iActivityTeamService.infoListPage(contactPhone, pageNo, pageSize);
if (activityTeamLoginOutputDTOList.getSize() > 0 ){
log.error("【没有查询到信息】");
}else{
log.info("【成功显示所有信息】");
}
map.put("activityTeamLoginOutputDTOList", activityTeamLoginOutputDTOList);
return new ModelAndView("/ActivityTeam/listPage",map);
}
重点
public class PageInfo<T> extends PageSerializable<T> {
//当前页(默认从第一页开始,所以我们设置默认值的时候要设置为1)
private int pageNum;
//每页的数量
private int pageSize;
//当前页的数量
private int size;
//当前页面第一个元素在数据库中的行号
private int startRow;
//当前页面最后一个元素在数据库中的行号
private int endRow;
//总记录数
private long total;
//总页数
private int pages;
//结果集
private List<T> list;
//第一页
private int firstPage;
//前一页
private int prePage;
//是否为第一页
private boolean isFirstPage;
//是否为最后一页
private boolean isLastPage;
//是否有前一页
private boolean hasPreviousPage;
//是否有下一页
private boolean hasNextPage;
//导航页码数
private int navigatePages;
//所有导航页号
private int[] navigatepageNums;
}
2. thymeleaf模板获取分页信息(行数比较多,配合上面的方法一起看)
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<script src="../js/jquery-3.3.1.min.js" ></script><!-- jq -->
<link rel="stylesheet" th:href="@{../css/bootstrap.min.css}" /><!-- bootstrap.css -->
<script src="../js/bootstrap.min.js"></script><!-- bootstrap.js -->
<script src="../js/sortTable.js"></script><!-- 自定义js -->
<meta charset="UTF-8">
<title>孩子信息列表</title>
<script th:inline="javascript">
/*2.点击变大变小图片 */
var b = 0;
function imgSize(btn){
if(b==0){
$(btn).animate({width:"100%"});
b=1;
}else if(b==1){
$(btn).animate({width:"50%"});
b=0;
}
}
</script>
</head>
<body>
<br><br><br>
<!-- 背景特效 -->
<script src="../js/canvas-nest.min.js" >
</script>
<div class="container-fluid" >
<div class="">
<div class="col-md-2 column">
</div>
<div class="col-md-8 column">
<h1 style="text-align: center;">参赛信息</h1>
<br/>
<ul class="nav nav-tabs">
<li class="active">
<a href="#">首页</a>
</li>
<!-- 左边内容 -->
<li>
<a href="">#</a>
</li>
<li >
<a href="">#</a>
</li>
<!-- 右边内容 -->
<li class="dropdown pull-right">
<a href="/ActivityTeam/logout" >注销</a>
</li>
</ul>
<br/>
<table class="table table-hover ">
<thead>
<tr >
<th >团队名</th>
<th style="cursor: pointer;">推荐机构</th><!-- <span class="glyphicon glyphicon-search" aria-hidden="true"> </span> -->
<th style="cursor: pointer;">家长姓名<span> </span></th>
<th style="cursor: pointer;">家长性别<span> </span></th>
<th style="cursor: pointer;">宝贝姓名<span> </span></th>
<th style="cursor: pointer;">宝贝性别<span> </span></th>
<th style="cursor: pointer;">宝贝年龄<span> </span></th>
<th style="cursor: pointer;">所在城市<span> </span></th>
<th >宝贝参赛图片</th>
</tr>
</thead>
<tbody>
<tr th:each="activityTeamLoginOutputDTO,activityTeamLoginOutputDTOStat : ${activityTeamLoginOutputDTOList.list}" >
<td th:text="${activityTeamLoginOutputDTO.activityTeamName}"></td>
<td th:text="${activityTeamLoginOutputDTO.officialActivityOrderDetail}"></td>
<td th:text="${activityTeamLoginOutputDTO.customerName}"></td>
<td th:text="${activityTeamLoginOutputDTO.customerGender}==0?'女':'男'"></td>
<td th:text="${activityTeamLoginOutputDTO.customerChildName}"></td>
<td th:text="${activityTeamLoginOutputDTO.customerChildGender}==0?'女':'男'"></td>
<td th:text="${activityTeamLoginOutputDTO.customerChildBirthday}"></td>
<td th:text="${activityTeamLoginOutputDTO.customerCity}"></td>
<td >
<!-- 略缩图 -->
<a href="" class="a-img" data-toggle="modal" th:attr= "data-target='#myModal' + ${activityTeamLoginOutputDTOStat.index}" >
<img th:src="@{ ${activityTeamLoginOutputDTO.onePicUrlArr[0]}}" alt="" style="width: 30px;height: 30px">
</a>
<!-- 模态框 -->
<div class="modal fade" th:attr= "id='myModal' + ${activityTeamLoginOutputDTOStat.index}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" style="width: 50%;">
<div class="modal-content">
<!-- 最顶层:关闭按钮(x)、标题、下载 -->
<div class="modal-header" style="padding: 18px;border-bottom: 0px;">
<!-- 关闭按钮 -->
<button type="button"style="float: right;" class="btn btn-default" data-dismiss="modal">关闭</button>
<!-- 标题 -->
<div class="modal-title" id="myModalLabel">
<span th:text="${activityTeamLoginOutputDTO.customerChildName}"></span>
</div>
</div>
<!-- 中间:主要内容 -->
<div class="modal-body" style="padding: 0px;text-align: center;">
<button class="img-Size" style="border:0px;background-color:white;width:50%;" onclick='imgSize(this)'>
<!-- 遍历循环urlArray -->
<div th:each="onePicUrlArr, onePicUrlArrStat : ${activityTeamLoginOutputDTO.onePicUrlArr}" >
<img th:src="@{${onePicUrlArr}}" alt="" style="width:100%;">
<div class="carousel-caption">
</div>
</div>
</button>
</div>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
<div style="align-content: center;text-align: center;">
<ul class="pagination pagination-lg" >
<li th:class="${activityTeamLoginOutputDTOList.pageNum==1}?'disabled' : ''"><a th:href="@{'../ActivityTeam/listPage?pageNo=' + ${activityTeamLoginOutputDTOList.pageNum-1}}">上一页</a></li>
<li th:if="${activityTeamLoginOutputDTOList.pageNum-3 >=1}" ><a th:href="@{'../ActivityTeam/listPage?pageNo=' + ${activityTeamLoginOutputDTOList.pageNum-3}}" th:text="${activityTeamLoginOutputDTOList.pageNum -3}" >1</a></li>
<li th:if="${activityTeamLoginOutputDTOList.pageNum-2 >=1}" ><a th:href="@{'../ActivityTeam/listPage?pageNo=' + ${activityTeamLoginOutputDTOList.pageNum-2}}" th:text="${activityTeamLoginOutputDTOList.pageNum -2}" >1</a></li>
<li th:if="${activityTeamLoginOutputDTOList.pageNum-1 >=1}" ><a th:href="@{'../ActivityTeam/listPage?pageNo=' + ${activityTeamLoginOutputDTOList.pageNum-1}}" th:text="${activityTeamLoginOutputDTOList.pageNum -1}" >1</a></li>
<li class="active"><a href="#" th:text="${activityTeamLoginOutputDTOList.pageNum}" >1</a></li>
<li th:if="${activityTeamLoginOutputDTOList.pageNum+1 <=activityTeamLoginOutputDTOList.pages}" ><a th:href="@{'../ActivityTeam/listPage?pageNo=' + ${activityTeamLoginOutputDTOList.pageNum+1}}" th:text="${activityTeamLoginOutputDTOList.pageNum +1}" >1</a></li>
<li th:if="${activityTeamLoginOutputDTOList.pageNum+2 <=activityTeamLoginOutputDTOList.pages}" ><a th:href="@{'../ActivityTeam/listPage?pageNo=' + ${activityTeamLoginOutputDTOList.pageNum+2}}" th:text="${activityTeamLoginOutputDTOList.pageNum +2}" >1</a></li>
<li th:if="${activityTeamLoginOutputDTOList.pageNum+3 <=activityTeamLoginOutputDTOList.pages}" ><a th:href="@{'../ActivityTeam/listPage?pageNo=' + ${activityTeamLoginOutputDTOList.pageNum+3}}" th:text="${activityTeamLoginOutputDTOList.pageNum +3}" >1</a></li>
<li th:class="${activityTeamLoginOutputDTOList.pageNum==activityTeamLoginOutputDTOList.pages}?'disabled' : ''" ><a th:href="@{'../ActivityTeam/listPage?pageNo=' + ${activityTeamLoginOutputDTOList.pageNum+1}}">下一页</a></li>
</ul><br>
</div>
<br/><br/><br/><br/><br/>
</div>
<div class="col-md-2 column">
</div>
</div>
</div>
</body>
</html>
补充
3. 什么时候用DTO做为返回值?什么时候用pojo做为返回值?BO和DTO的关系
- 返回值不仅仅只有一个表的数据的时候,就用DTO。
- 返回值只有单个表的信息的时候,就用POJO。
- 注意:DTO类创建的时候,我们是根据自己的需求来创建的的,所以用于接收返回值效果很好,方便。
- 我们在数据库查询的时候返回值含有多个表的数据的时候,我们可以用BO对象来接受参数,然后再service层返回出去的时候我们应该再把它改成DTO对象,一般情况下,DTO对象和BO对象中存储的数据都是一样的