框架:springMVC+spring+mybatis+maven+mysql
工具:Spring Tool Suite
问题:分页
maven中加入:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.0</version>
</dependency>
mybatis-config中配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
<property name="supportMethodsArguments" value="true"/>
<property name="params" value="pageNum=pageNumKey;pageSize=pageSizeKey;"/>
</plugin>
</plugins>
</configuration>
spring-mybatis中配置:
<property name="configLocation" value="classpath:mybatis-config.xml" />
mapper的方法:
List<TbSystemBasisCourseComment> selectCourseComments(
@Param("courseCourseCode") String courseCourseCode,
@Param("pageNumKey") int pageNum,
@Param("pageSizeKey") int pageSize);
controller层与service层的方法的形参都是pageNumKey和pageSizeKey,
在mapper中对应为pageNum和pageSize
sql语句不用写limit,插件会将pageNum和pageSize自动加入;
分页功能实现;
PS:
尝试在spring-mybatis中按照github上的文档不用mybatis-config.xml文件直接配置:
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
helperDialect=mysql
supportMethodsArguments=true
</value>
</property>
</bean>
</array>
</property>
会出错,未找到原因,遂放弃直接在spring-mybatis配置
网友评论