分页插件PageHelper
参看了pagehelper-spring-boot,使用起来非常放方便,关于更多PageHelper可以点击https://github.com/pagehelper/Mybatis-PageHelper。
添加maven依赖
根据上面地址中的说明,只需要添加如下依赖,并且可以不再mybatis的依赖
- <!--分页插件-->
- <dependency>
- <groupId>com.github.pagehelper</groupId>
- <artifactId>pagehelper-spring-boot-starter</artifactId>
- <version>1.0.0</version>
- </dependency>
pagehelper的使用
我增加了如下测试代码。
- @RequestMapping("/find/mybatis/page")
- public String findUserPageFromMybatis(HttpServletRequest request, Integer pageNum, Integer pageSize) {
- pageNum = pageNum == null ? 1 : pageNum;
- pageSize = pageSize == null ? 10 : pageSize;
- PageHelper.startPage(pageNum, pageSize);
- List<UserMo> list = userMapper.selectUserList();
- PageInfo pageInfo = new PageInfo(list);
- Page page = (Page) list;
- return "PageInfo: " + JSON.toJSONString(pageInfo) + ", Page: " + JSON.toJSONString(page);
- }
运行程序后在浏览器输入 http://127.0.0.1:8080/find/mybatis/page,会看到如下输出。
- PageInfo: {
- "endRow": 1,
- "firstPage": 1,
- "hasNextPage": true,
- "hasPreviousPage": false,
- "isFirstPage": true,
- "isLastPage": false,
- "lastPage": 8,
- "list": [
- {
- "createDate": 1483586613000,
- "id": 1,
- "isDeleted": 0,
- "modifyDate": 1483586613000,
- "money": 1.20,
- "name": "sss"
- }
- ],
- "navigateFirstPage": 1,
- "navigateLastPage": 8,
- "navigatePages": 8,
- "navigatepageNums": [1,2,3,4,5,6,7,8],
- "nextPage": 2,
- "pageNum": 1,
- "pageSize": 1,
- "pages": 11,
- "prePage": 0,
- "size": 1,
- "startRow": 1,
- "total": 11
- },
- Page: [
- {
- "createDate": 1483586613000,
- "id": 1,
- "isDeleted": 0,
- "modifyDate": 1483586613000,
- "money": 1.20,
- "name": "sss"
- }
- ]
重要提示
- 只有紧跟在 PageHelper.startPage 方法后的第一个 MyBatis 的查询(select)方法会被分页。
- 请不要在系统中配置多个分页插件(使用 spring 时,mybatis-config.xml 和 Spring<bean> 配置方式,请选择其中一种,不要同时配置多个分页插件)。
- 对于带有 for update 的 sql,会抛出运行时异常,对于这样的 sql 建议手动分页,毕竟这样的 sql 需要重视。
- 由于嵌套结果方式会导致结果集被折叠,因此分页查询的结果在折叠后总数会减少,所以无法保证分页结果数量正确。
当spring boot集成好mybatis时候需要进行分页,我们首先添加maven支持
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | < dependency > < groupId >com.github.pagehelper</ groupId > < artifactId >pagehelper</ artifactId > < version >5.1.2</ version > </ dependency > < dependency > < groupId >com.github.pagehelper</ groupId > < artifactId >pagehelper-spring-boot-autoconfigure</ artifactId > < version >1.2.3</ version > </ dependency > < dependency > < groupId >com.github.pagehelper</ groupId > < artifactId >pagehelper-spring-boot-starter</ artifactId > < version >1.2.3</ version > </ dependency > |
方式一:我们在application.yml(spring 需要读取的yml)中加入
1 2 3 4 5 | pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql |
然后重启即可。
配置文件最终会被java所读取,最终注入到spring bean中,所以我们方法二是配置其bean类,热加载方便修改当然方式一更简单,
方式二:在注解涵盖package下面新建PageHeleperConfig
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import com.github.pagehelper.PageHelper; import java.util.Properties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author zhuxiaomeng * @date 2018/1/2. * @email [email protected] */ @Configuration public class PageHelperConfig { @Bean public PageHelper getPageHelper(){ PageHelper pageHelper= new PageHelper(); Properties properties= new Properties(); properties.setProperty( "helperDialect" , "mysql" ); properties.setProperty( "reasonable" , "true" ); properties.setProperty( "supportMethodsArguments" , "true" ); properties.setProperty( "params" , "count=countSql" ); pageHelper.setProperties(properties); return pageHelper; } } |
pageHelper 基础知识为:
1 2 | import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; |
1 | Page<T> tPage= PageHelper.startPage(page,limit); |
下一句的查询语句来进行分页。你只需要用List<T>接收
下面是通用Mapper和PageHelper在Spring Boot下的编程式配置示例,其中包名及其他配置参数请自行替换成符合你项目的配置。
@Configuration
public class MybatisConfigurer {
@Resource
private DataSource dataSource;
@Bean
public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setTypeAliasesPackage("cn.potato.orm.model");
//分页插件
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("reasonable", "true");
properties.setProperty("supportMethodsArguments", "true");
properties.setProperty("returnPageInfo", "check");
properties.setProperty("params", "count=countSql");
pageHelper.setProperties(properties);
//添加插件
bean.setPlugins(new Interceptor[]{pageHelper});
//添加XML目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
return bean.getObject();
}
@Configuration
@AutoConfigureAfter(MybatisConfigurer.class)
public static class MyBatisMapperScannerConfigurer {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
mapperScannerConfigurer.setBasePackage("cn.potato.orm.mapper");
//配置通用mappers
Properties properties = new Properties();
properties.setProperty("mappers", "cn.potato.orm.core.Mapper");
properties.setProperty("notEmpty", "false");
properties.setProperty("IDENTITY", "MYSQL");
mapperScannerConfigurer.setProperties(properties);
return mapperScannerConfigurer;
}
}
}