mybatis pagehelper分页插件

1.pom.xml添加maven依赖

<!-- mybatis pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.6.4</version>

</dependency>

2.在mybatis-config.xml配置pagehelper 插件

<?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" />
<!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 (相当于没有执行分页查询,但是返回结果仍然是Page类型) -->
<property name="pageSizeZero" value="true" />
<!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 
禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->
<property name="reasonable" value="false" />
</plugin>
</plugins>

</configuration>

3.LinkMApper.xml,sql不需要任何处理如limit分页相关的东西

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="dao.LinkMapper" >
  <resultMap id="BaseResultMap" type="model.Link" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="department" property="department" jdbcType="VARCHAR" />
    <result column="position" property="position" jdbcType="VARCHAR" />
    <result column="phone" property="phone" jdbcType="VARCHAR" />
    <result column="email" property="email" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, name, department, position, phone, email
  </sql>
  <select id="getAllLink" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from t_link
  </select>

</mapper>

4.接口LinkMapper.java

package dao;
import java.util.List;
import model.Link;
public interface LinkMapper {
    List<Link> getAllLink();

}

5.service接口,用PageInfo接收List<Link>

package service;
import model.Link;
import com.github.pagehelper.PageInfo;
/**   
 * Description:  
 * @author guoshanglei  
 * @date 2018-5-2  
 */
public interface LinkService {
PageInfo<Link> getAllLink(int pageNum,int pageSize);

}

6.serviceImpl

package service.impl;
import java.util.List;
import model.Link;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.LinkService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import dao.LinkMapper;
/**   
 * Description:  
 * @author guoshanglei  
 * @date 2018-5-2  
 */
@Service
    public class LinkServiceImpl implements LinkService{
@Autowired

public LinkMapper linkMapper;

@Override
public PageInfo<Link> getAllLink(int pageNum, int pageSize) {

               //PageHelper是核心有许多方法,如startPage(int pageNum, int pageSize, boolean count):

                pageNum第几页,pageSize每页的个数,是否进行count查询

PageHelper.startPage(pageNum, pageSize,true);
//正常查询List<Link>
List<Link> list = (List<Link>) linkMapper.getAllLink();
//List<Link>转化为PageInfo<Link>
PageInfo<Link> pageInfo = new PageInfo<Link>(list);
return pageInfo;
}

}

7.controller

        //分页
@RequestMapping(value="/getAllLink",method = RequestMethod.GET)
public @ResponseBody PageInfo<Link> getAllLink(){
ReturnModel returnModel = new ReturnModel();
PageInfo<Link> pageInfo = linkService.getAllLink(1,1);
return pageInfo;

}

最终浏览器返回:

{"pageNum":1,"pageSize":1,"size":1,"startRow":1,"endRow":1,"total":26,"pages":26,"list":[{"id":2,"name":"j654j","department":"jh4jh1","position":"654jh4","phone":"4h4","email":"herthrt"}],"firstPage":1,"prePage":0,"nextPage":2,"lastPage":8,"isFirstPage":true,"isLastPage":false,"hasPreviousPage":false,"hasNextPage":true,"navigatePages":8,"navigatepageNums":[1,2,3,4,5,6,7,8]}
阅读更多
想对作者说点什么? 我来说一句

Mybatis分页插件PageHelper的JAR包

2018年01月08日 340KB 下载

最新Mybatis-PageHelper分页插件Jar

2017年03月16日 340KB 下载

MybatisPageHelper分页插件

2016年10月15日 1.86MB 下载

最新mybatis分页插件PageHelper 5.0.2

2017年05月29日 582KB 下载

没有更多推荐了,返回首页

不良信息举报

mybatis pagehelper分页插件

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭