1.mybatis没有自带PageHelper需要在maven中引入jar 包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>最新版本</version>
</dependency>
2.再跟xml的配置拦截器 (注意防止的位置)
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!--配置的是可以使用方法传参数-->
<property name="supportMethodsArguments" value="true"/>
</plugin>
</plugins>
3.怎么在代码中使用(常用的方法)
//第二种,Mapper接口方式的调用,推荐这种使用方式。
PageHelper.startPage(1, 10);
List<Country> list = countryMapper.selectIf(1);
//第三种,Mapper接口方式的调用,推荐这种使用方式。
PageHelper.offsetPage(1, 10);
List<Country> list = countryMapper.selectIf(1);
//第四种,参数方法调用
//存在以下 Mapper 接口方法,你不需要在 xml 处理后两个参数
public interface CountryMapper {
List<Country> selectByPageNumSize(
@Param("user") User user,
@Param("pageNum") int pageNum,
@Param("pageSize") int pageSize);
}
//配置supportMethodsArguments=true
//在代码中直接调用:
List<Country> list = countryMapper.selectByPageNumSize(user, 1, 10);
3.1 怎么获取数据总条数
public void text() {
ActionSQL mapper = getSession().getMapper(ActionSQL.class);
PageHelper.startPage(1,10);
List<StuBean> query = mapper.query();
PageInfo p=new PageInfo(query);//将查出的List放入
System.out.println(p.getTotal());//获取总条数
}
怎么在JAVAEE中使用pagehelper
首先第一步导包是肯定要的
那么第二步,在配置文件中配置拦截器拦截SQL
如下
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ds"> </property>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!--使用下面的方式配置参数,一行配置一个 -->
<value>
supportMethodsArguments=true
</value>
</property>
</bean>
</array>
</property>
</bean>
具体方法在https://pagehelper.github.io/docs/ 中