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,这个连接池 ...
随机推荐
- C# 使用nuget.exe发布类库及更新类库
前景:在开发学习阶段希望一些重复使用代码或者算法代码积累.能够在VS中下载安装方便使用. 准备工作: 1.Nuget登录账号(可 Microsoft 账号).Nuget官网 2.Nuget.exe程序 ...
- IPython的介绍与使用
1.IPython简介 ipython是一个python的交互式shell,比默认的python shell好用得多,支持变量自动补全,自动缩进,支持bash shell命令,内置了许多很有用的功能和 ...
- 开始使用Manjaro
Manjaro是什么? 一个基于Arch系列,开源的linux发行版 Mnajrao官网了解更多,这里不做更多阐述内容 为什么使用Manjaro 第一点,为了方便自己隔离腾讯网游 第二点,更方便的学习 ...
- 02--java--环境搭建
第一步,下载JDK 去ORACLE官网http://www.oracle.com下载 有安装版和绿色版,安装版一路下一步,绿色版解压缩压缩包就行了 安装版直接自动配置环境变量,绿色版需要自己配置环境变 ...
- CSS-03-组选择器
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 小白学Java:RandomAccessFile
目录 小白学Java:RandomAccessFile 概述 继承与实现 构造器 模式设置 文件指针 操作数据 读取数据 read(byte b[])与read() 追加数据 插入数据 小白学Java ...
- Shell考题中级篇
写脚本实现,可以用shell.perl等.把文件b中有的,但是文件a中没有的所有行,保存为文件c,并统计c的行数. grep -v -x bbb -f aaa > ccc && ...
- ts和nts的区别 (redis中碰到)
[TS指Thread Safet y线程安全 NTS即None-Thread Safe 非线程安全] 区别:[TS NTS] TS指Thread Safety,即线程安全,一般在IIS以ISAPI ...
- 事务管理(ACID)
目录 一.事务管理(ACID) 原子性(Atomicity) 一致性(Consistency) 持久性(Durability) 隔离性(Isolation) 二.事务隔离级别 脏读 不可复读 虚读(幻 ...
- Linux防火墙之iptables基本匹配条件和隐式扩展匹配条件
一.iptables的基本匹配条件 上一篇博文我们说到了iptables的基本工作原理.数据报文在内核的走向和管理链.管理规则.以及查看规则.导入和导出规则:回顾请参考https://www.cnbl ...