SpringBoot框架学习记录第四篇之分页查询数据

SpringBoot框架学习记录第一篇之学生信息增删改修:https://blog.csdn.net/qq_18620233/article/details/104662420

SpringBoot框架学习记录第二篇,自定义查询和动态查询:https://blog.csdn.net/qq_18620233/article/details/104679507

SpringBoot框架学习记录第三篇之表单验证和事务处理:https://blog.csdn.net/qq_18620233/article/details/104686340

 

本节学习Spring Boot分页查询数据

程序预览

代码实现

Controller代码:


/**
 * 分页实现,显示分页遍历出来的数据
 * @return
 */
//@ResponseBody
@RequestMapping("/list3")
public ModelAndView studentList3(){
	ModelAndView mav = new ModelAndView();
	Student s_student = new Student();
	Long total=studentService.getCount();
	System.out.println("输出了"+PageUtil.genPagination("/student/list4", total, 1, 2,""));
	//分页数据
	mav.addObject("pageCode",PageUtil.genPagination("/student/list4", total, 1, 5,""));
	
	//遍历数据库中的信息
	//mav.addObject("studentList",studentService.pageList(s_student, 1, 5,Sort.Direction.DESC,"age"));
	mav.addObject("studentList", studentService.pageList(s_student, 1, 5,Sort.Direction.DESC,"age"));
	
	mav.addObject("title", "分页学生列表");
	mav.setViewName("studentList3");
	return mav;
}
/** 
 * 分页查询信息
 * @param page
 * @return
 * @throws Exception
 */
@RequestMapping("/list4/{id}")
public ModelAndView list(@PathVariable(value="id",required=false) Integer page,HttpServletRequest request)throws Exception{

	ModelAndView mav=new ModelAndView();
	Student s_student=new Student();

	mav.addObject("studentList", studentService.pageList(s_student, page, 5,Sort.Direction.DESC,"age"));
	Long total=studentService.getCount();
	StringBuffer param=new StringBuffer();
	mav.addObject("pageCode",PageUtil.genPagination("/student/list4", total, page, 5,""));
	mav.addObject("title", "分页学生列表");
	mav.setViewName("studentList3");
	return mav;
}

 

Service代码:

/**
 * 统计学生数量
 * @return
 */
public Long getCount(){
	return studentRepository.count();
}

/**
 * 分页显示数据
 * @param s_student
 * @param page
 * @param pageSize
 * @param direction
 * @param properties
 * @return
 */
public List<Student> pageList(Student s_student, Integer page, Integer pageSize,Direction direction,String... properties){
	Pageable pageable=new PageRequest(page-1, pageSize, direction,properties);
	Page<Student> pageArticle=studentRepository.findAll(new Specification<Student>() {
		
		@Override
		public Predicate toPredicate(Root<Student> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
			Predicate predicate=cb.conjunction();
			if(s_student!=null){
				if(s_student.getAge()!=null){
					predicate.getExpressions().add(cb.equal(root.get("state"), s_student.getAge()));
				}
			}
			return predicate;
		}
	}, pageable);
	return pageArticle.getContent();
}

 

分页工具代码:

package top.hiai.util;

/**
 * 分页工具类
 * @author Administrator
 *
 */
public class PageUtil {

	/**
	 * 生成分页代码
	 * @param targetUrl 目标地址
	 * @param totalNum 总记录数
	 * @param currentPage 当前页
	 * @param pageSize 每页大小
	 * @param param 请求参数
	 * @return
	 */
	public static String genPagination(String targetUrl,long totalNum,int currentPage,int pageSize,String param){
		long totalPage=totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1;
		if(totalPage==0){
			return "未查询到数据";
		}else{
			StringBuffer pageCode=new StringBuffer();
			pageCode.append("<div class='layui-box layui-laypage layui-laypage-default' id='layui-laypage-1' >");
			pageCode.append("<a href='"+targetUrl+"/1"+param+"'>首页</a>");
			if(currentPage>1){
				pageCode.append("<a href='"+targetUrl+"/"+(currentPage-1)+param+"' class='layui-laypage-prev'>上一页</a>");
			}else{
				pageCode.append("<a href='javascript:;' class='layui-laypage-prev layui-disabled'>上一页</a>");
			}
			for(int i=currentPage-2;i<=currentPage+2;i++){
				if(i<1||i>totalPage){
					continue;
				}
				if(i==currentPage){
					pageCode.append("<span class='layui-laypage-curr'><em class='layui-laypage-em'></em><em>"+i+"</em></span>");	
				}else{
					pageCode.append("<a href='"+targetUrl+"/"+i+param+"'>"+i+"</a>");	
				}
			}
			if(currentPage<totalPage){
				pageCode.append("<a href='"+targetUrl+"/"+(currentPage+1)+param+"' class='layui-laypage-next'>下一页</a>");
			}else{
				pageCode.append("<a href='javascript:;' class='layui-laypage-next layui-disabled'>下一页</a>");
			}
			pageCode.append("<a href='"+targetUrl+"/"+totalPage+param+"'>尾页</a>");
			pageCode.append("</div>");
			return pageCode.toString();
		}
	}
}

 

HTML页面代码:

<body>
	<p>
		<a href="/student/addview">添加学生</a>&nbsp;&nbsp;
		<a href="/student/addview2">AJAX添加学生</a>&nbsp;&nbsp;
		<a href="/student/trans">转账事务处理</a>&nbsp;&nbsp;
	</p>
	<form method="post" action="/student/list2">
		学生姓名:<input type="text" name="username"/>&nbsp;&nbsp;&nbsp;&nbsp;
		学生性别:<input type="text" name="sex"/>&nbsp;&nbsp;&nbsp;&nbsp;
		<input type="submit" value="搜索"/><br/><br/>
	</form>
	<form action="" method="get">
		Lucene搜索:<input type="text" name="q"/>
		<input type="submit" value="搜索"/><br/><br/>
	</form>
	
	<table>
		<tr>
			<th>编号</th>
			<th>姓名</th>
			<th>性别</th>
			<th>年龄</th>
			<th>金钱</th>
			<th>操作</th>
		</tr>
		<tr th:each="student:${studentList}">
			<td th:text="${student.id}"></td>
			<td th:text="${student.username}"></td>
			<td th:text="${student.sex}"></td>
			<td th:text="${student.age}"></td>
			<td th:text="${student.money}"></td>
			<td>
				<a th:href="@{'/student/stuedit/'+${student.id}}">修改</a>
				<a th:href="@{'/student/studel/'+${student.id}}">删除</a>
			</td>
		</tr>
	</table>
	<div th:utext="${pageCode}" style="padding-top: 20px;"></div>
</body>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值