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分页的实现的更多相关文章

  1. 如何在实际项目中使用PageHelper分页插件

    PageHelper是一个分页插件,能够简单快速的帮助开发人员完成常见的分页功能,你只需要简单的使用两行代码就可以完成一个分页效果- 最近做一个科创项目,使用Maven+SSM的环境,有分页的功能,于 ...

  2. ssm中逆向工程与分页的应用

    昨天对springboot中的mybatis逆向工程与分页应用进行了整理,今天对ssm项目中的逆向工程与分页进行整理. 项目运行环境:eclipse+jdk1.8+maven+tomcat 一.搭建s ...

  3. spring_boot 中通过PageHelper分页

    1. 第一步 导入pom.xml依赖 <!--PageHelper模版--> <!--PageHelper模版--> <dependency> <groupI ...

  4. PageHelper分页实战(SSM整合)

    步骤一:引入SSM相关的jar包,包列表如下: 步骤二:创建或修改配置文件,配置文件清单如下: applicationContext.xml <?xml version="1.0&qu ...

  5. SSM框架手动实现分页逻辑(非PageHelper)

    第一种方法:查询出所有数据再分页 分析: 分页时,需要获得前台传来的两个参数,分别为pageNo(第几页数据),pageSize(每页的条数); 根据这两个参数来计算出前端需要的数据是查出数据list ...

  6. ssm工程集成mybatis分页插件pagehelper

    1    首先需要在mybatis的配置文件SqlMapConfig.xml文件中配置pagehelper插件 <plugins> <plugin interceptor=" ...

  7. Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件

    前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...

  8. PageHelper分页插件的使用

    大家好!今天写ssm项目实现分页的时候用到pageHelper分页插件,在使用过程中出现了一些错误,因此写篇随笔记录下整个过程 1.背景:在项目的开发的过程中,为了实现所有的功能. 2.目标:实现分页 ...

  9. SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件

    原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...

随机推荐

  1. 低功耗蓝牙(BLE)——概念

    1. 种类 单模蓝牙:仅支持传统蓝牙和BLE(低功耗蓝牙)中的一种: 双模蓝牙:同时支持传统蓝牙和BLE(低功耗蓝牙). 2. 部署方案 3. 节点类型 根据蓝牙协议不同的协议层有不同的角色 1. S ...

  2. 悄摸直播(三)—— 搭建rtmp服务器(smart_rtmpd - rtmp服务器搭建)

    悄摸直播 -- javaCV实现本机摄像头画面远程直播 搭建rtmp服务器 一.素材 rtmp服务器:smart_rtmpd ffmpeg工具:ffmpeg.exe 二.搭建 1.下载smart_rt ...

  3. 数字任意组合 - gcd

    链接:https://www.nowcoder.com/acm/contest/160/A来源:牛客网 题目描述有一个计数器,计数器的初始值为0,每次操作你可以把计数器的值加上a1,a2,...,an ...

  4. 第二阶段冲刺个人任务——seven

    今日任务: 整体运行测试上传到公网上的程序. 昨日成果: 搭建网络服务器,上传数据库及程序.

  5. 隐隐约约 听 RazorEngine 在 那里 据说 生成代码 很 美。

    这只是 一个开始 ....

  6. 生成链接中的全限定URL(Generating Fully Qualified URLs in Links) | 在视图中生成输出URL | 高级路由特性

    结果:<a class="myCSSClass"href="https://myserver.mydomain.com/Home/Index/MyId#myFrag ...

  7. vuex 基本语法

    VUEX 的核心概念 1 .State (常用):2.Getters :3.Mutations(常用):4.Actions :5.Modules: 1.State是唯一的数据源,单一的状态树 cons ...

  8. hadoop中两种上传文件方式

    记录如何将本地文件上传至HDFS中 前提是已经启动了hadoop成功(nodedate都成功启动) ①先切换到HDFS用户 ②创建一个user件夹 bin/hdfs dfs -mkdir /user ...

  9. JPA基本注解的使用

    一:JPA基本注解 使用: 使用: 使用: 查看表: 二:用table来生成主键 使用: allocationSize:每次增加多少 tablel:指定使用那张表 执行两次main方法后查看表: jp ...

  10. linux--->redis php扩展安装

    阿里云centos6.9下 redis php扩展安装 下载phpredis wget http://pecl.php.net/get/redis-3.1.0.tgz 或 wget https://g ...