SpringBoot+PageHelper实现分页
不整那些花里胡哨的,简单粗暴有效!小白文章,不喜勿喷.有问题可以加我qq:2548841623/vx:N2548841623
关键代码:
//PageHelper配置
@Configuration // 针对于PageHelper的配置
public class web_xml {
// PageHelper分页配置
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
pageHelper.setProperties(p);
return pageHelper;
}
}
//开始分页
PageHelper.startPage(pageNum, 15,true);// 开始分页括号内的三个值分别是:当前页页码 每页多少行 是否排序
List<DataTable> list = tableDAO.findAll();//从TableDAO类中获取查询到的所有结果(List集合)
PageInfo<DataTable> pageInfo = new PageInfo<>(list);//将list集合封装到PageInfo中,按住Ctrl点击PageInfo可以查看所有属性值
关键(容易忽视的)注解:
@Mapper
@Configuration
@Bean
第0步:配置pom.xml(都到分页了,其他的组件自己搞定就ok)
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
第1步:配置类的编写,也是重点,我在这个上面被坑了好久,其中@Confinguration和@Bean注解必不可少!!!一下内容直接copy即可(数据库自己连接)
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.github.pagehelper.PageHelper;
@Configuration // 针对于PageHelper的配置
public class web_xml {
// PageHelper分页配置
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
pageHelper.setProperties(p);
return pageHelper;
}
}
第2步:去数据库中查到的数据用List集合返回,在Controller拿到返回给前台,要求在前台页面能够看到,之后开始分页,具体步骤如下:
/////////////////////////////////////////////分割线:以下是Controller///////////////////////////////////////
@Controller
public class DataTableController {
@Autowired
private TableDAO tableDAO;
@RequestMapping(value = "/table")
public ModelAndView tables(@RequestParam(name = "xiye",required=false,defaultValue="1")int pageNum) {
ModelAndView modelAndView = new ModelAndView();
PageHelper.startPage(pageNum, 15,true);// 开始分页括号内的三个值分别是:当前页页码 每页多少行 是否排序
List<DataTable> list = tableDAO.findAll();//从TableDAO类中获取查询到的所有结果(List集合)
PageInfo<DataTable> pageInfo = new PageInfo<>(list);//将list集合封装到PageInfo中,按住Ctrl点击PageInfo可以查看所有属性值
modelAndView.addObject("list", pageInfo.getList());//将成功分页后的集合放到modelAndViewz中
modelAndView.addObject("count", pageInfo.getPages());// 总页数
modelAndView.setViewName("HomePage");//将所有的数据返回给HomePage界面
System.out.println("当前页---------" + pageInfo.getPageNum());//------------1
System.out.println("每页显示的条数----" + pageInfo.getPageSize());//-------------15
System.out.println("总条数----------" + pageInfo.getTotal());
System.out.println("总页数-----------" + pageInfo.getPages());
return modelAndView;
}
}
/////////////////////////////////////////////分割线:以下是DAO///////////////////////////////////////
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.mingde.webapps.model.DataTable;
@Mapper//此注解必不可少!!!!!
public interface TableDAO {
// @Select("select row_number()over(order by NO desc)rn,a.* from DATATABLE a")//倒序
//select row_number()over(order by 列名 asc)rn,a.* from 表名 a
@Select("select row_number()over(order by NO asc)rn,a.* from DATATABLE a")//顺序
List<DataTable> findAll();
}
/////////////////////////////////////////////分割线:以下是实体类///////////////////////////////////////
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "DATATABLE")//DATATABLE是表明
public class DataTable {
@Id//数据库里面的主键
@Column(name = "NO")//NO数据库里面的值
private int no;
@Column(name = "NAME")
private String name;
@Column(name = "SEX")
private String sex;
@Column(name = "AGE")
private int age;
@Column(name = "TEL")
private long tel;
@Column(name = "ID")
private long id;
@Column(name = "HOME")
private String home;
@Column(name = "STUDENTID")
private String studentid;
@Column(name = "QQ")
private long qq;
@Column(name = "WECHAT")
private String wechat;
@Column(name = "PARTY")
private String party;
/*省略get,set方法*/
/*eclipse右键或者Alt+Shift+S ===> 点击Source ===> Generate Getters and Setters... ==>全选生成get,set方法*/
//////////////////////////////////分割线:以下是Service(感觉没啥好解释的)///////////////////////////////////////
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mingde.webapps.dao.TableDAO;
import com.mingde.webapps.model.DataTable;
@Service(value = "tableService")
public class TableService {
@Autowired
private TableDAO tableDAO;
public List<DataTable> getTableDao() {
return tableDAO.findAll();
}
}