StudentScoreService添加方法
/**
* 分页查询 无查询条件
*
* @param page
* @param size
* @return
*/
public Page<StudentDTO> findStudent(Integer page, Integer size) {
@SuppressWarnings("deprecation")
Pageable pageable = new PageRequest(page, size, Sort.Direction.ASC, "id");
return studentScoreRepository.findAll(pageable);
}/**
* 分页查询 有查询条件
*
* @param page
* @param size
* @param studentDTO
* @return
*/
public Page<StudentDTO> findStudentByMap(Integer page, Integer size, final StudentDTO studentDTO) {
@SuppressWarnings("deprecation")
Pageable pageable = new PageRequest(page, size, Sort.Direction.ASC, "id");
Page<StudentDTO> bookPage = studentScoreRepository.findAll(new Specification<StudentDTO>() {@Override
public Predicate toPredicate(Root<StudentDTO> root, CriteriaQuery<?> query,
CriteriaBuilder criteriaBuilder) {
List<Predicate> list = new ArrayList<Predicate>();
if (null != studentDTO.getStudentName() && !"".equals(studentDTO.getStudentName())) {
list.add(criteriaBuilder.equal(root.get("studentName").as(String.class),
studentDTO.getStudentName()));
}
Predicate[] p = new Predicate[list.size()];
return criteriaBuilder.and(list.toArray(p));/*其他方法
* Predicate p1 = criteriaBuilder.equal(root.get("studentName").as(String.class),
* studentDTO.getStudentName()); query.where(criteriaBuilder.and(p1)); return
* query.getRestriction();
*/
}}, pageable);
return bookPage;
}
StuentScoreController
/**
* 分页查询,没有条件
*
* @param modelMap
* @param page 页数
* @param size 分页每页的行数
* @return
*/
@RequestMapping("findStudentScore")
// 必须制定page和size的默认值,否则会报错,page=0,默认为第一页,size表示分页的一页的行数
public String findStudentScore(ModelMap modelMap, @RequestParam(value = "page", defaultValue = "0") Integer page,
@RequestParam(value = "size", defaultValue = "3") Integer size) {
Page<StudentDTO> students = studentScoreService.findStudent(page, size);
modelMap.addAttribute("datas", students);
return "studetnScore";
}/**
* 分页查询 按条件查询
*
* @param modelMap
* @param page
* @param size
* @param studentDTO
* @return
*/
@RequestMapping(value = "findStudentByMap", method = { RequestMethod.GET, RequestMethod.POST })
public String findStudentByMap(ModelMap modelMap, @RequestParam(value = "page", defaultValue = "0") Integer page,
@RequestParam(value = "size", defaultValue = "3") Integer size, StudentDTO studentDTO) {
Page<StudentDTO> datas = studentScoreService.findStudentByMap(page, size, studentDTO);
modelMap.addAttribute("datas", datas);
return "studentScore1";
}
page.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="page">
<body>
<div th:fragment="pager">
<div class="text-right" th:with="baseUrl=${#httpServletRequest.getRequestURL().toString()},pars=${#httpServletRequest.getQueryString() eq null ? '' : new String(#httpServletRequest.getQueryString().getBytes('iso8859-1'), 'UTF-8')}">
<ul style="margin:0px;" class="pagination" th:with="newPar=${new java.lang.String(pars eq null ? '' : pars).replace('page='+(datas.number), '')},
curTmpUrl=${baseUrl+'?'+newPar},
curUrl=${curTmpUrl.endsWith('&') ? curTmpUrl.substring(0, curTmpUrl.length()-1):curTmpUrl}" >
<!--<li th:text="${pars}"></li>--><li><a href="#" th:href="@{${curUrl}(page=0)}">首页</a></li>
<li th:if="${datas.hasPrevious()}"><a href="#" th:href="@{${curUrl}(page=${datas.number-1})}">上一页</a></li><!--总页数小于等于10-->
<div th:if="${(datas.totalPages le 10) and (datas.totalPages gt 0)}" th:remove="tag">
<div th:each="pg : ${#numbers.sequence(0, datas.totalPages - 1)}" th:remove="tag">
<span th:if="${pg eq datas.getNumber()}" th:remove="tag">
<li class="active"><span class="current_page line_height" th:text="${pg+1}">${pageNumber}</span></li>
</span>
<span th:unless="${pg eq datas.getNumber()}" th:remove="tag">
<li><a href="#" th:href="@{${curUrl}(page=${pg})}" th:text="${pg+1}"></a></li>
</span>
</div>
</div><!-- 总数数大于10时 -->
<div th:if="${datas.totalPages gt 10}" th:remove="tag">
<li th:if="${datas.number-2 ge 0}"><a href="#" th:href="@{${curUrl}(page=${datas.number}-2)}" th:text="${datas.number-1}"></a></li>
<li th:if="${datas.number-1 ge 0}"><a href="#" th:href="@{${curUrl}(page=${datas.number}-1)}" th:text="${datas.number}"></a></li>
<li class="active"><span class="current_page line_height" th:text="${datas.number+1}"></span></li>
<li th:if="${datas.number+1 lt datas.totalPages}"><a href="#" th:href="@{${curUrl}(page=${datas.number}+1)}" th:text="${datas.number+2}"></a></li>
<li th:if="${datas.number+2 lt datas.totalPages}"><a href="#" th:href="@{${curUrl}(page=${datas.number}+2)}" th:text="${datas.number+3}"></a></li>
</div>
<li th:if="${datas.hasNext()}"><a href="#" th:href="@{${curUrl}(page=${datas.number+1})}">下一页</a></li>
<!--<li><a href="#" th:href="@{${curUrl}(page=${datas.totalPages-1})}">尾页</a></li>-->
<li><a href="#" th:href="${datas.totalPages le 0 ? curUrl+'page=0':curUrl+'&page='+(datas.totalPages-1)}">尾页</a></li>
<li><span th:utext="'共'+${datas.totalPages}+'页 / '+${datas.totalElements}+' 条'"></span></li>
</ul>
</div>
</div>
</body>
</html>
studetnScore.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<script type="text/javascript" th:src="@{/jquery/jquery-3.3.1.min.js}"></script>
<script type="text/javascript" th:src="@{/bootstrap/js/bootstrap.min.js}"></script>
<link type="text/css" rel="stylesheet" th:href="@{/bootstrap/css/bootstrap-theme.min.css}"/>
<link type="text/css" rel="stylesheet" th:href="@{/bootstrap/css/bootstrap.css}"/>
</head>
<body>
<table class="table table-hover">
<tr>
<th>学生id</th>
<th>姓名</th>
<th>英语成绩</th>
<th>数学成绩</th>
</tr>
<tr th:each="obj:${datas}">
<td th:text="${obj.id}">${obj.id}</td>
<td th:text="${obj.studentName}">${obj.studentName}</td>
<td th:text="${obj.score.eng_score}">${obj.score.eng_score}</td>
<td th:text="${obj.score.math_score}">${obj.score.math_score}</td>
</tr>
</table>
<div th:include="page :: pager" th:remove="tag"></div>
</body>
</body>
</html>
studentScore1.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
<script type="text/javascript" th:src="@{/jquery/jquery-1.12.3.min.js}"></script>
<script type="text/javascript" th:src="@{/bootstrap/js/bootstrap.min.js}"></script>
<link type="text/css" rel="stylesheet" th:href="@{/bootstrap/css/bootstrap-theme.min.css}"/>
<link type="text/css" rel="stylesheet" th:href="@{/bootstrap/css/bootstrap.css}"/>
</head>
<body>
<form th:action="@{/findStudentByMap}" th:object="${studentDTO}" th:method="get">
<div class="form-group">
<label class="col-sm-2 control-label" >name</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="studentName" placeholder="请输入名称" th:field="*{studentName}"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
<button class="btn btn-default" type="submit" placeholder="查询">查询</button>
</div>
</div>
</form>
<table class="table table-hover">
<tr>
<th>学生id</th>
<th>姓名</th>
<th>英语成绩</th>
<th>数学成绩</th>
</tr>
<tr th:each="obj:${datas}">
<td th:text="${obj.id}">${obj.id}</td>
<td th:text="${obj.studentName}">${obj.studentName}</td>
<td th:text="${obj.score.eng_score}">${obj.score.eng_score}</td>
<td th:text="${obj.score.math_score}">${obj.score.math_score}</td>
</tr>
</table>
<div th:include="page :: pager" th:remove="tag"></div>
</body>
</html>