Springboot +pagehelper 分页插件
2018-03-08 19:53
507 查看
一.前言
上篇博客中介绍了spring boot集成mybatis的方法,基于上篇文章这里主要介绍如何使用分页插件PageHelper。在MyBatis中提供了拦截器接口,我们可以使用PageHelp最为一个插件装入到SqlSessionFactory,实现拦截器功能。(新手的话按照我这个就可以实现分页,如果项目中有设置SqlSessionFactoryBean,请注意下面的东西用该放到SqlSessionFactoryBean 配置里面)二.实现
pom.xml文件中添加依赖包[html] view plain copy print?<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.0</version> </dependency>
创建MybatisConf类
[html] view plain copy print?package com.woniu.mybatisconf;
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.github.pagehelper.PageHelper;
/*
* 注册MyBatis分页插件PageHelper
*/
@Configuration
public class MybatisConf {&nbs
4000
p;
@Bean
public PageHelper pageHelper() {
System.out.println(“MyBatisConfiguration.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;
}
}
package com.woniu.mybatisconf; import java.util.Properties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.github.pagehelper.PageHelper; /* * 注册MyBatis分页插件PageHelper */ @Configuration public class MybatisConf { @Bean public PageHelper pageHelper() { System.out.println("MyBatisConfiguration.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; } }
这时就可以使用PageHelp插件了,在controller中直接使用。
[html] view plain copy print?package com.woniu.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageHelper;
import com.woniu.bean.User;
import com.woniu.mapper.UserMaper;
@RestController
@RequestMapping(“/web”)
public class WebController {
@Autowired
private UserMaper mapper;
@RequestMapping(“/index”)
public List<User> selectAge(int age){
/*
* 第一个参数是第几页;第二个参数是每页显示条数。
*/
PageHelper.startPage(1,2);
return mapper.Select(age);
}
}
package com.woniu.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.github.pagehelper.PageHelper; import com.woniu.bean.User; import com.woniu.mapper.UserMaper; @RestController @RequestMapping("/web") public class WebController { @Autowired private UserMaper mapper; @RequestMapping("/index") public List<User> selectAge(int age){ /* * 第一个参数是第几页;第二个参数是每页显示条数。 */ PageHelper.startPage(1,2); return mapper.Select(age); } }
相关文章推荐
- SpringBoot集成MyBatis的分页插件PageHelper(回头草)
- Spring Boot系列教程十一: Mybatis使用分页插件PageHelper
- springboot中分页插件pageHelper的使用
- Spring Boot系列六 Spring boot集成mybatis、分页插件pagehelper
- spring boot集成开源分页插件pagehelper
- spring-boot 集成mybatis的分页插件PageHelper和Generator (番外)
- 记录问题:springboot中使用Mybatis-pagehelper分页插件遇到的版本兼容问题
- Mybatis分页插件 - PageHelper与springBoot的整合
- SpringBoot Mybatis PageHelper分页插件的两种用法(一)
- spring-boot | 整合通用Mabatis 分页插件PageHelper
- springboot如何集成mybatis的pagehelper分页插件
- SpringBoot集成分页插件PageHelper
- SpringBoot集成MyBatis的分页插件PageHelper(回头草)
- SpringBoot分页插件PageHelper
- spring-boot 集成mybatis的分页插件PageHelper和Generator (番外)
- spring-boot 集成mybatis的分页插件PageHelper和Generator
- SpringBoot Mybatis PageHelper分页插件的两种用法(二)
- SpringBoot之分页插件PageHelper的使用
- [置顶] MyBatis基于Spring-boot集成通用Mapper以及pagehelper分页插件(含源码下载)
- Spring 3.x使用PageHelper插件分页查询