1.引入依赖
<!-- mybatis分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.2.1</version> </dependency>
注意:PageHelp版本要依据 JDK版本 和 Mybatis版本来选择,不然会报错
2.修改spring-mybatis.xml
<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:com/wode/mapper/*.xml" /> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageHelper"> <property name="properties"> <value> dialect=mysql </value> </property> </bean> </array> </property> </bean>
3.在service层中调用
@Service public class MsgService { @Resource private MsgMapper msgMapper; public PageInfo<Msg> queryMsgByUserId(String userId, int currentPage){ //参数一是起始页(1.2.3.4...),参数二是每页显示的条数 PageHelper.startPage(currentPage, IConstant.MSG_PAGE_SIZE); List<Msg> msgList = msgMapper.queryMsgByUserId(userId); PageInfo<Msg> pageJson = new PageInfo<Msg>(msgList); return pageJson; } }
参考链接:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md
原文地址:https://www.cnblogs.com/vettel0329/p/8404564.html
时间: 2024-10-03 04:44:04