首先导入maven依赖
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.10</version> </dependency>
然后在spring-config.xml中配置
<!--分页插件--> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value> dialect=mysql </value> </property> </bean> </array> </property>
最后在service中只需要两行代码就可以轻松实现分页了
@Override public JsonData findAllShopType(int page, int limit) { PageHelper.startPage(page, limit); List<ShopType> allShopType = shopTypeMapper.findAllShopType(); PageInfo<ShopType> shopTypePageInfo = new PageInfo<>(allShopType); return JsonData.buildSuccess(shopTypePageInfo); }
以上代码纯属个人理解,若有错误,欢迎指正