注!!! PageHelper.startPage () 必须加在执行查询sql语句方法之前 因为需要拼接limit 不然分页不了
mybatis配置文件(方式一)
<?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>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
<setting name="cacheEnabled" value="true"/>
</settings>
<typeAliases>
<package name="com.wei.pojo"/>
</typeAliases>
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
<property name="offsetAsPageNum" value="true"/>
<property name="rowBoundsWithCount" value="true"/>
<property name="pageSizeZero" value="true"/>
<property name="reasonable" value="true"/>
</plugin>
</plugins>
</configuration>
java类配置 (方式二)
package com.sky.exam1.config;
import com.github.pagehelper.PageHelper;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration
public class MyConfig {
@Bean
PageHelper pageHelper(){
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("reasonable", "true");
properties.setProperty("supportMethodsArguments", "true");
properties.setProperty("returnPageInfo", "check");
properties.setProperty("params", "count=countSql");
pageHelper.setProperties(properties);
new SqlSessionFactoryBean().setPlugins(pageHelper);
return pageHelper;
}
}
yml文件配置(方式三)
pagehelper:
helperDialect: mysql
reasonable: false #开启优化,如果开启优化,在分页页码结果没有数据的时候,会显示有数据的页码数据
supportMethodsArguments: true #是否支持接口参数来传递分页参数,默认false
pageSizeZero: false #pageSize=0 返回所有
params: count=countSql
依赖包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.1</version>
</dependency>
然后就可以直接使用mybatis分页的方法和对象了!