1、增加pagehelper
<!-- mybatis pager --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.0</version> </dependency>
2、增加配置
<!--Spring和MyBatis整合--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!--自动扫描mapping.xml文件--> <property name="mapperLocations" value="classpath:mappers/*.xml"></property> <!-- 分页插件 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageHelper"> <property name="properties"> <value> dialect=oracle </value> </property> </bean> </array> </property> </bean>
3、Service中实现
public PageInfo<Log> queryList(int pageNum, int pageSize){ PageHelper.startPage(pageNum,pageSize); List<Log> list = xxMapperDao.queryList(); PageInfo<Log> pageInfo = new PageInfo<>(list); return pageInfo; }
4、Controller中调用
@RequestMapping(value = "queryLogs.htm") public void queryLogs( HttpServletResponse response, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10")int pageSize){ PageInfo<Log> list = iLogService.queryList(pageNum, pageSize); ... }