mybatis之PageHelper

1:需要的jar

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>4.0.1</version>
</dependency>
<dependency>
    <groupId>com.github.jsqlparser</groupId>
    <artifactId>jsqlparser</artifactId>
    <version>0.9.1</version>
</dependency>

2:mybatis-config.xml

<?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.PageHelper" >
            <property name ="dialect" value ="mysql"/>
            <!-- 该参数默认为false -->
            <!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 -->
            <!-- 和startPage中的pageNum效果一样-->
            <property name ="offsetAsPageNum" value="true" />
            <!-- 该参数默认为false -->
            <!-- 设置为true时,使用RowBounds分页会进行count查询 -->
            <property name="rowBoundsWithCount" value="true"/>
        </plugin > 
    </plugins>
</configuration>

3:使用

@GET
@Produces("application/json;charset=utf-8")
public Response getModelList(@QueryParam("number") int number,
        @QueryParam("size") int size,
        @QueryParam("keyWords") String keyWords) throws Throwable {
    Result result = new Result();
    result.setErrorCode(0);
    PageHelper.startPage(number, size);
    Blog model = new Blog();
    model.setTitle(keyWords);
    Page<Blog> page = (Page<Blog>) blogService.getList(model);
    result = getResult(page);
    return Response.status(200).entity(result).build();
}




public Result getResult(Page page){
    Result result = new Result();
    Pager pager = new Pager();
    pager.setNumber(page.getPageNum());
    pager.setSize(page.getPageSize());
    pager.setTotal(page.getTotal());
    result.setPage(pager);
    result.setData(page.getResult());
    return result;

}

你可能感兴趣的:(mybatis,分页)