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. TensorFlow——TensorBoard可视化

    TensorFlow提供了一个可视化工具TensorBoard,它能够将训练过程中的各种绘制数据进行展示出来,包括标量,图片,音频,计算图,数据分布,直方图等,通过网页来观察模型的结构和训练过程中各个 ...

  2. MapGIS文件如何压缩存盘

    经过多次编辑修改的MapGIS数据,含有大量逻辑上已删除的节点或图元,数据冗余复杂, 在转换过程前应注意一定要采用压缩存盘方式处理,目的是确保编辑状态已删除的数据真正从物理存储层删除,以确保数据的精简 ...

  3. 【C&数据结构】---关于链表结构的前序插入和后序插入

    刷LeetCode题目,需要用到链表的知识,忽然发现自己对于链表的插入已经忘得差不多了,以前总觉得理解了记住了,但是发现真的好记性不如烂笔头,每一次得学习没有总结输出,基本等于没有学习.连复盘得机会都 ...

  4. Nginx模块讲解

    Nginx模块分为:nginx官方模块.第三方模块 通过nginx -V查看编译参数,可以看到官方编译的模块 --with-compat --with-file-aio --with-threads ...

  5. Flask登录认证

    login函数 @app.route('/login/', methods=['GET', 'POST']) def login(): if request.method == 'GET': retu ...

  6. 缓存读写策略 - Cache Aside.md

    场景描述 比如一条数据同时存在数据库.缓存,现在你要更新此数据,你会怎么更新? 先更新数据库?还是先更新缓存? 其实这两种方式都有问题. (1)先更新数据库,后更新缓存 这样会造成数据不一致. A 先 ...

  7. 「 优质资源20190409 」Java最新精选优质资源!

    资源导读 经过小编精心整理,java最新优质资源出炉 不想看书,可以看视频,比较生动有趣,好的视频教程是一个好老师! 资源来自于网络,请勿用于商业用途 资源目录 1.Java Spring 技术栈构建 ...

  8. 剑指Offer对答如流系列 - 实现Singleton模式

    目录 面试题2:实现Singleton模式 一.懒汉式写法 二.饿汉式写法 三.枚举 面试题2:实现Singleton模式 题目:设计一个类,我们只能生成该类的一个实例. 由于设计模式在面向对象程序设 ...

  9. scanf 函数笔记

    函数声明 int scanf(const char *format, ...); 说明 <返回值> scanf ("<格式化字符串>", <参数表&g ...

  10. Dubbo入门到实战

    前沿:在当下流行的分布式架构中Dubbo是非常流行的一门技术,借着这几天有空学习学习,并在后面的项目中进行实战,为后面的分布式项目做铺垫. Dubbox简介 Dubbox 是一个分布式服务框架,其前身 ...