分页插件的使用
程序员文章站
2022-06-18 23:19:25
...
1.applicationContext.xml文件里添加bean
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--别名扫描的包路径-->
<property name="typeAliasesPackage" value="com.heima.domian"></property>
<!--拦截器插件-->
<property name="plugins" >
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<!--方言的配置-->
<prop key="helperDialect">mysql</prop>
<!--分页合理化参数 默认false-->
<prop key="reasonable">true</prop>
<!--默认false,如果使用pagesize=0时全查-->
<prop key="pageSizeZero">true</prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
2.在service层使用pagehelper
public PageInfo<Order> findAllOrder(Integer pageNum, Integer pageSize) {
//1.starPage方法从第几页查询多少条数据
PageHelper.startPage(pageNum, pageSize);
List<Order> allOrder = orderDao.findAllOrder();
//2.PageInfo将查询的数据添加入pageInfo中
return new PageInfo<Order>(allOrder);
}
3.controller层接收pageInfo 将pageInfo添加入Attribute里
@RequestMapping("/findAllOrder")
public String findAllOrder(Model model, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize) {
SecurityContext context = SecurityContextHolder.getContext();
User user = (User) context.getAuthentication().getPrincipal()
//返回pageInfo;
PageInfo<Order> pageInfo = orderService.findAllOrder(pageNum, pageSize);
//添加pageInfo
model.addAttribute("oList", pageInfo);
return "order/orderList";
}