天天看点

guns框架分页实现

首先注意分页的maven依赖一般有俩种,不能混用,否者出错,guns中默认使用如下Page依赖

<!-- mybatis的orm插件 -->
 <dependency> 
  <groupId>com.baomidou</groupId>
   <artifactId>mybatisplus-spring- boot-starter</artifactId>
   <version>1.0.4</version> </dependency> 
<dependency>
   <groupId>com.baomidou</groupId> 
   <artifactId>mybatis-plus</artifactId> 
   <version>2.0.7</version>
 </dependency>      

导入是:

import com.baomidou.mybatisplus.plugins.Page;      

示例:

/**
 * 前台快讯列表
 */
@RequestMapping(value = "/newsList")
public Object newsList(HttpSession session, @RequestParam(required=true,defaultValue="1") Integer page, Model model) {
    User user= (User)session.getAttribute(Constants.USER_SESSION);
    if(user!=null&& !"".equals(user)){
        model.addAttribute("user",user);
    }
    Page<News> newsPage=new Page<>(page,15);
    EntityWrapper<News> ew = new EntityWrapper<News>();
    ew.orderDesc(Collections.singleton("createTime"));
    Page<News> newsList=newsService.selectPage(newsPage,ew);
    model.addAttribute("newsList",newsList);
    return new ModelAndView(PREFIX_2+"newsList.html","homeModel",model);
}      

这是前台列表页面的控制分页显示的代码,其中Page<> newsPage=new Page<>(page,15);是要使用的分页语句,page代指起始页,他由前台用户点击的页码传递到后台实现分页内容切换,15是一页显示的数量

如何实现 动态分页呢?比如有对分页的结果有按照某个字段排序的需求.....?

通过 import com.baomidou.mybatisplus.mapper.EntityWrapper;实现,如上面代码中

EntityWrapper<News> ew = new EntityWrapper<News>();

ew.orderDesc(Collections.singleton("createTime"));

创建EntityWrapper示例, orderDesc代指按照什么来降序排列,ew的方法还有很多,我这里是按照createTime排序即可,当然更加复杂的查询需求参考官网的使用介绍

最后将ew 作为selectPage的第二个参数即可实现动态排序啦!

import com.baomidou.mybatisplus.service.IService;      
package com.stylefeng.guns.modular.system.tools;


public class Constants {
 public final static String  USER_SESSION="userSession";
 public final static String  FILEUPLOAD_ERROR_1="* APK信息不完整";
 public final static String  FILEUPLOAD_ERROR_2="* 上传失败";
 public final static String  FILEUPLOAD_ERROR_3="* 上传文件格式不正确";
 public final static String  FILEUPLOAD_ERROR_4="* 上传文件过大!";
 public final static String  SYS_MESSAGE="massage";
 /*public final static String  USER_SESSION="userSession";*/
 public final static int  honorPageSize=6;
 public final static int  deptSize=4;
 public final static int  newsSize=6;
 public final static int  reportSize=12;
 public final static int  pageSize=5;
 public final static String SESSION_BASE_MODEL="baseModel";

}      

继续阅读