17-SSM中通过pagehelper分页的实现
SSM中通过pagehelper分页的实现
1. 在SSM框架的基础上实现,导包
<!-- 分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
2. application.xml的配置,在sqlSessionFactory的Bean标签中加入下面的属性
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
<!-- helperDialect:有别于3.0+版本,现在必须是helperDialect,否则spring启动加载时会报错 -->
helperDialect=mysql
</value>
</property>
</bean>
</array>
</property>
2.1 如果你是纯javaconfig配置的SSM框架,你应该在SpringConfig下面这样配置plugins,代替第二步
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
//分页 以下
PageInterceptor pageInterceptor = new PageInterceptor();
//创建插件需要的参数集合
Properties properties = new Properties();
//配置数据库方言 为oracle
properties.setProperty("helperDialect", "mysql");
//配置分页的合理化数据
properties.setProperty("reasonable", "true");
pageInterceptor.setProperties(properties);
//将拦截器设置到sqlSessionFactroy中
sqlSessionFactoryBean.setPlugins(new Interceptor[] {pageInterceptor});
//以上
return sqlSessionFactoryBean;
}
3. 编写service层,CustomService代码如下:
public interface CustomService {
List<Custom> findAllCustom(int page,int rows);
}
4. CustomServiceImpl代码如下:
@Service
public class CustomServiceImpl implements CustomService {
@Autowired
CustomMapper customMapper;
/**
* page 开始页数
* rows 每页显示的数据条数
*/
@Override
public List<Custom> findAllCustom(int page,int rows) {
//将参数传给方法实现分页
PageHelper.startPage(page, rows);
CustomExample example = new CustomExample();
List<Custom> customs = customMapper.selectByExample(example);
return customs;
}
}
5. 编写web层,CustomController代码如下:
@Controller
@RequestMapping("custom")
public class CustomController {
@Autowired
CustomService customService;
@RequestMapping("list")
@ResponseBody
public PageBean<Custom> list(int page,int rows){
List<Custom> customs = customService.findAllCustom( page,rows);
//查询到的数据给到PageInfo ,只需要把结果集给到该对象,获取分页信息
// 就可以通过该对象get方法拿到总页数,总记录数,等等你想要的数据
PageInfo<Custom> pageInfo=new PageInfo<>(customs);
//根据前台需要在自定义一个分页对象
//我的本次项目只需要传入页面需要的list集合,和total,同时json形式返回
PageBean<Custom> pageBean=new PageBean<>(customs,pageInfo.getTotal());
//把该对象json返回
return pageBean;
}
6. PageBean<T>创建
public class PageBean<T> {
private List<T> rows;
private long total;
public PageBean(List<T> rows, long total) {
this.rows = rows;
this.total = total;
}
public PageBean() {
}
public List<T> getRows() {
return rows;
}
public void setRows(List<T> rows) {
this.rows = rows;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
}
7. 测试
http://localhost/custom/list?rows=30&page=1
17-SSM中通过pagehelper分页的实现的更多相关文章
- 如何在实际项目中使用PageHelper分页插件
PageHelper是一个分页插件,能够简单快速的帮助开发人员完成常见的分页功能,你只需要简单的使用两行代码就可以完成一个分页效果- 最近做一个科创项目,使用Maven+SSM的环境,有分页的功能,于 ...
- ssm中逆向工程与分页的应用
昨天对springboot中的mybatis逆向工程与分页应用进行了整理,今天对ssm项目中的逆向工程与分页进行整理. 项目运行环境:eclipse+jdk1.8+maven+tomcat 一.搭建s ...
- spring_boot 中通过PageHelper分页
1. 第一步 导入pom.xml依赖 <!--PageHelper模版--> <!--PageHelper模版--> <dependency> <groupI ...
- PageHelper分页实战(SSM整合)
步骤一:引入SSM相关的jar包,包列表如下: 步骤二:创建或修改配置文件,配置文件清单如下: applicationContext.xml <?xml version="1.0&qu ...
- SSM框架手动实现分页逻辑(非PageHelper)
第一种方法:查询出所有数据再分页 分析: 分页时,需要获得前台传来的两个参数,分别为pageNo(第几页数据),pageSize(每页的条数); 根据这两个参数来计算出前端需要的数据是查出数据list ...
- ssm工程集成mybatis分页插件pagehelper
1 首先需要在mybatis的配置文件SqlMapConfig.xml文件中配置pagehelper插件 <plugins> <plugin interceptor=" ...
- Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件
前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...
- PageHelper分页插件的使用
大家好!今天写ssm项目实现分页的时候用到pageHelper分页插件,在使用过程中出现了一些错误,因此写篇随笔记录下整个过程 1.背景:在项目的开发的过程中,为了实现所有的功能. 2.目标:实现分页 ...
- SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件
原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...
随机推荐
- vue中动态设置echarts画布大小
document.getElementById('news-shopPagechart').style.height = this.heightpx2+'px'; //heightpx2定义在data ...
- Springboot2.1.1下的自定义拦截器而静态资源不能访问的问题
1.项目结构 2.自定义拦截器 public class LoginHandlerlnterceptor implements HandlerInterceptor { //目标方法执行之前 @Ove ...
- 暑假提高组集训Day1 T2
那么这一道题我在考试的时候写挂了(0分 呜呜~) 我原来的思路是广搜来骗取部分分(哈哈~) 但是我忘记了一个非常重要的问题 我广搜开的数组没有考虑负的下标 下一次考试如果再写暴力 就可以把坐标都加上一 ...
- bzoj 2683 CDQ分治
题目描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y<=N,A是正整数 将格子x,y里的数 ...
- background-position和position
1.background-position:表示背景定位的属性.描述属性值时,有两种方式:一是像素描述:而是单位描述. (1)像素描述: 格式如下: background-position:向右偏移量 ...
- 菜鸟学习Fabric源码学习 — kafka共识机制
Fabric 1.4源码分析 kafka共识机制 本文档主要介绍kafka共识机制流程.在查看文档之前可以先阅览raft共识流程以及orderer服务启动流程. 1. kafka 简介 Kafka是最 ...
- 华为,小米部分机型微信浏览器rem不适配的解决方案
针对近日华为,小米的部分机型,在升级系统或升级微信之后,微信内置浏览器产生的rem不能正确填充满的问题,有如下解决方案 目前来看,产生这个情况的原因是因为给html附font-size时,附上的fon ...
- java 赋值运算
注意:在赋值运算的时候,会自动发生数据类型转变 例子 public class test{ public static void main(String[] args){ byte num = 5; ...
- HashMap在JDK7和JDK8中的区别
在[深入浅出集合Map]中,已讲述了HashMap在jdk7中实现,在此就不再细说了 JDK7中的HashMap 基于链表+数组实现,底层维护一个Entry数组 Entry<K,V>[] ...
- svn subvesion Branch Merge