springboot + mybatis分页插件pagehelper  

maven引入

<dependency>
            <groupId>com.github.pagehelper</groupId><!--mybatis分页插件-->
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version><!--注意版本-->
 </dependency>

引入maven后,实际操作代码如下:

controller

    /**demo   分页查询
     *
     * @param pageNum  当前页数
     * @param pageSize 当前页最多显示多少行
     * @return
     */
    @ResponseBody   //返回json数据
    @GetMapping("/demo/findByPaging")
    public  String findByPaging(Integer pageNum, Integer pageSize)throws ControllerException {
        int age = 27;
        JSONObject result = new JSONObject();
        try{
       //使用分页插件,核心代码就这一行 pageNum当前页数 pageSize 当前页最多显示多少行 PageHelper.startPage(pageNum,pageSize);
         Page
<EmployeesDemo> data = demoService.findByPaging( age);//age为查询条件 result.put("employees",data);//data为返回数据 //获取页面总数 result.put("pages",data.getPages()); //获取数据总数 result.put("total",data.getTotal()); }catch (Exception e){ e.printStackTrace(); throw new ControllerException("分页查询失败",e,userContext); } return result !=null?result.toString():null; }

service

    /**
     * 分页查询结果
     * @param age 查询条件  年龄
     * @return
     */
    public Page<EmployeesDemo> findByPaging(int age)throws ServiceException {
        Page<EmployeesDemo> pages = null;
        try{
            pages =demoDao.findByPaging(age);
        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException("分页查询结果出错 参数age="+age, e);
        }
        return pages;
    }

dao

    /**demo
     * 分页查询
     * @param age  查询条件  年龄
     * @return
     */
    public Page<EmployeesDemo> findByPaging( int age);

mapper

    <select id="findByPaging" resultType="HashMap"  parameterType="com.github.pagehelper.Page">
        select * from EmployeesDemo
        where age >= #{name}
    </select>

参考:https://blog.csdn.net/qq_28988969/article/details/78082116

另外如果为多表查询,不好建实体类情况;此时返回Hashmap即可,如下:

controller

import org.json.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

    /**
     * 获取用户信息
     * @param pageNum  当前页数
     * @param pageSize 当前页最多显示多少行
     * @return
     * @throws Exception
     */
    @RequestMapping("/user/getUserInfos")
    public String getUserInfos( Integer pageNum, Integer pageSize) throws Exception{

        JSONObject result = new JSONObject();
        try{
            PageHelper.startPage(pageNum,pageSize);
            Page<HashMap> data = sysUserService.getUserInfos( );

            result.put("data",data);
            //获取页面总数
            result.put("limit",data.getPages());
            //获取数据总数
            result.put("total",data.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            //   throw new ControllerException("分页查询失败",e,userContext);
        }

        return result !=null?result.toString():null;
    }

Services实现类

    /**
     * 获取用户数据
     * @return
     * @throws ServiceException
     */
    public Page getUserInfos() throws ServiceException{
        Page<HashMap> list = null;
        try {
            list = sysUserDao.getUserInfos();

        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("查询获取用户信息时出错", e);
        }
        return list;
    }

dao接口

import com.github.pagehelper.Page;
  
  /**
     * 获取用户数据
     * @return
     */
    public Page getUserInfos();

mapper

<select id="login" resultType="com.nsoft.gkzp.system.sysuser.entity.SysUser">
    select * from sys_user
    <where>
        <if test="loginName != null">
             loginname =#{loginName}
        </if>
        <if test="password != null">
            and Password=#{password}
        </if>
    </where>
</select>

 <select id="getUserInfos" resultType="java.util.HashMap">
        select k.id,k.loginName,t.name,t.Gender,RIGHT(IDCardNo,4) IDCardNo
        from   sys_user k
        left join HR_Recruit_entryInfo_base t  on t.IsNewest=1 and k.id = t.loginUserId
        where k.nstatusid =1
    </select>

这里有一篇非常好的文档,一定要看看

https://my.oschina.net/sunpr/blog/340951

我截图划了重点如下:

springboot + mybatis分页插件pagehelper