maven引入pageHelper
<!-- https://github.com/pagehelper/Mybatis-PageHelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
mybatis配置分页信息
@Configuration
@EnableTransactionManagement
public class MybatisConfig implements TransactionManagementConfigurer {
@Autowired
DataSource dataSource;
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactoryBean() {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setTypeAliasesPackage("cn.vobile.hss.model");
// 支持下划线到驼峰
org.apache.ibatis.session.Configuration conf = new org.apache.ibatis.session.Configuration();
conf.setMapUnderscoreToCamelCase(true);
bean.setConfiguration(conf);
//分页插件
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("offsetAsPageNum", "true");
properties.setProperty("rowBoundsWithCount", "true");
properties.setProperty("reasonable", "true");
properties.setProperty("supportMethodsArguments", "true");
properties.setProperty("returnPageInfo", "check");
properties.setProperty("params", "pageNum=page;pageSize=rows;orderBy=orderBy");
pageHelper.setProperties(properties);
//添加插件
bean.setPlugins(new Interceptor[]{pageHelper});
//添加XML目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
//读取多个文件下的xml文件
List<Resource> resources = new ArrayList<>();
Resource[] resources1 = resolver.getResources("classpath*:mapper/*.xml");
Resource[] resources2 = resolver.getResources("classpath*:sqlmapperext/*.xml");
resources.addAll(Arrays.asList(resources1));
resources.addAll(Arrays.asList(resources2));
bean.setMapperLocations(resources.toArray(new Resource[]{}));
return bean.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
主要是对properties中进行的一些配置化信息。
- reasonable表示分页合理化,如果pageNum<=0时候会查询第一页的数据。如果pageNum>总页数,会查询最后一页的数据。
-
params中可以定义分页属性名称,默认是pageNum和pageSize。你也可以定义其他的名称。
例如 properties.setProperty(“params”, “pageNum=page;pageSize=rows;orderBy=orderBy”);
- pageSizeZero默认false,为true时候,如果pageSize=0时候查询所有数据
使用PageHelper
//自定义分页bo类。主要设置三个属性。与params中设置的对应
public class BaseEntityWithPage {
@Transient
private Integer page = ;
@Transient
private Integer rows = ;
@Transient
private String orderBy;
@JsonIgnore
public String getOrderBy() {
return orderBy;
}
public void setOrderBy(String orderBy) {
this.orderBy = orderBy;
}
@JsonIgnore
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
@JsonIgnore
public Integer getRows() {
return rows;
}
public void setRows(Integer rows) {
this.rows = rows;
}
}
//service调用处
public List<Customer> getAllCustomers(Customer page){
page.setOrderBy("id");
PageHelper.startPage(page);
return customerMapper.getAllCustomers();
}
直接调用PageHelper的startPage方法。
public static <E> Page<E> startPage(Object params) {}
结果
@Test
public void getCustomersTest(){
Customer page = new Customer();
page.setRows();
page.setPage();
List<Customer> list = customerService.getAllCustomers(page);
System.out.println("list = " + list);
}
//打印的结果
SELECT * FROM customer WHERE id <> 'common0000' order by id limit ?,?
DEBUG [main] - ==> Parameters: (Integer), (Integer)
可能有些不太详细,具体可以参考插件作者的文档哦。
https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md