springmvc中使用pageInfo工具来实现分页查询的简单操作

在查询较多数据的时候我们经常会用到分页查询,在浏览器页面上将查询的结果一页一页的分开展示给我们,下面是我写的一个使用pageInfo工具来完成简单的分页功能的步骤以及代码:

1.首先是在我们的service层对dao层查询出的数据进行分页操作:

1.1先在service接口中添加上 pageNum,pageSize 这两个参数,代表页数以及每页条数,该函数的返回结果改为PageInfo<>:

public PageInfo<Students> getStuAll(Integer timeteach,Integer pageNum,Integer pageSize);

1.2在service的实现类中加上 PageHelper.startPage(pageNum,pageSize);这句代码,然后把查询出来的集合放进 PageInfo 中并返回

@Override
	public PageInfo<Students> getStuAll(Integer timeteach,Integer pageNum,Integer pageSize) {
		PageHelper.startPage(pageNum,pageSize);
		List<Students> list = teachMainDao.selectStuAll(timeteach,cnamecid,stuname);
		return new PageInfo<Students>(list);
	}

2.接下来是在controller层接收返回结果,在这里也需要添加上 pageNum,pageSize 这两个参数,但是在这里可以通过@RequestParam() 注解设置这两个参数不是必须的并且修改默认值,在函数里面把从service层接收的 PageInfo 集合存到request作用域中:

@RequestMapping("tostushow")
	public String stushow(String timeteach,HttpServletRequest req,
			@RequestParam(required=false,defaultValue="1") Integer pageNum,
			@RequestParam(required=false,defaultValue="3") Integer pageSize){
		PageInfo<Students> pg = teachMainService.getStuAll(timeteach,pageNum,pageSize);
		req.setAttribute("pg", pg);
		return "stushow";
	}

3.在前端页面接收传过来的集合遍历输出,这里要注意的一点是,如果是遍历pageInfo类型的集合,要使用它里面的自带的 .list 这个属性来代表集合然后遍历输出

<#list pg.list as tsl>
	<tr>
		<td>${tsl_index+1}</td>
		<td>${tsl.stuname}</td>
		<td>${tsl.stunum}</td>
		<td>${tsl.stusex}</td>		
		<td>${tsl.stuage}</td>
		<td>${tsl.clz.cname}</td>
		<td>${tsl.stuhome}</td>
		<td>${tsl.sturemarks}</td>
		<td>头像缩略图,点击放大</td>
	</tr>
</#list>

4.上一页与下一页功能的实现,设置上页下页的超链接按钮,点击的时候跳转到相应请求并且带上 pageNum 这个参数,并且上页下页要对 pageNum 这个参数的值进行+1或者-1的操作,这样就实现了分页查询的功能:

<#if pg.hasPreviousPage>
	<a href="/tostushow?pageNum=${pg.pageNum-1}">上一页</a>
</#if>
<#if pg.hasNextPage>
	<a href="/tostushow?pageNum=${pg.pageNum+1}">下一页</a>
</#if>
好的,针对您的问题,我可以给出以下的解答: Spring MVC框架分页查询通常可以借助于PageHelper插件来实现PageHelper是MyBatis分页插件,它可以拦截MyBatis的查询操作,并在查询语句执行前自动添加分页相关的语句。使用PageHelper可以帮助我们轻松地实现分页查询功能。 以下是实现Spring MVC分页查询的步骤: 1. 引入PageHelper插件依赖。 在pom.xml文件添加以下依赖: ``` <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.2.0</version> </dependency> ``` 2. 在Spring MVC配置文件配置PageHelper插件。 在Spring MVC配置文件添加以下配置: ``` <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value> dialect=mysql </value> </property> </bean> </array> </property> </bean> ``` 其,dialect属性指定数据库类型,这里以MySQL为例。 3. 在Controller编写分页查询方法。 在Controller编写分页查询方法,例如: ``` @RequestMapping("/list") public ModelAndView list(@RequestParam(defaultValue="1") int pageNum, @RequestParam(defaultValue="10") int pageSize) { ModelAndView mv = new ModelAndView("list"); // 使用PageHelper进行分页查询 PageHelper.startPage(pageNum, pageSize); List<User> userList = userService.getUserList(); PageInfo<User> pageInfo = new PageInfo<User>(userList); mv.addObject("userList", userList); mv.addObject("pageInfo", pageInfo); return mv; } ``` 在这个方法,我们使用PageHelper的startPage方法开始分页查询,然后调用userService的getUserList方法获取用户列表,最后将查询结果和分页信息添加到ModelAndView返回。 4. 在JSP页面显示分页查询结果。 在JSP页面显示分页查询结果,例如: ``` <table> <thead> <tr> <th>ID</th> <<th>Name</th> <th>Age</th> </tr> </thead> <tbody> <c:forEach items="${userList}" var="user"> <tr> <td>${user.id}</td> <td>${user.name}</td> <td>${user.age}</td> </tr> </c:forEach> </tbody> </table> <div> <c:if test="${pageInfo.pages > 1}"> <c:url value="/user/list" var="url"> <c:param name="pageNum" value="${pageInfo.pageNum-1}"/> </c:url> <a href="${url}">上一页</a> <c:forEach begin="1" end="${pageInfo.pages}" var="i"> <c:url value="/user/list" var="url"> <c:param name="pageNum" value="${i}"/> </c:url> <c:if test="${pageInfo.pageNum == i}"> <strong>${i}</strong> </c:if> <c:if test="${pageInfo.pageNum != i}"> <a href="${url}">${i}</a> </c:if> </c:forEach> <c:url value="/user/list" var="url"> <c:param name="pageNum" value="${pageInfo.pageNum+1}"/> </c:url> <a href="${url}">下一页</a> </c:if> </div> ``` 在这个JSP页面,我们使用c:forEach标签遍历查询结果,使用pageInfo对象显示分页信息,使用c:url标签生成分页链接。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值