设为首页 加入收藏

TOP

mybatis分页插件PageHelper使用(二)
2019-09-03 02:36:56 】 浏览:28
Tags:mybatis 插件 PageHelper 使用
} } View Code
  • UserMapper      注意:mapper中就按不分页的那种写法就好
package com.szfore.dao;

import com.szfore.model.User;
import com.szfore.model.UserExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface UserMapper {
    /**
     * 多条件分页查询
     * @param userParam
     * @return
     */
    public List<User> queryByPage(User userParam);
}
  • UserMapper.xml    注意:sql中就不要写limit了,pageHelp会自己处理,sql就按不分页的那种写法就好
<!--多条件分页查询用户-->
  <select id="queryByPage" resultType="com.szfore.model.User">
    SELECT
          *
    FROM
        `user` 
    WHERE
         <if test="id != null and id != ''">
          AND id = #{id}
        </if>
        <if test="uname != null and uname != ''">
          AND uname = #{uname}
        </if>
        <if test="name != null and name != ''">
          AND name like '%${name}%'
        </if>
        <if test="phone != null and phone != ''">
          AND phone like '%${phone}%'
        </if>
        <if test="company != null and company != ''">
          AND company like '%${company}%'
        </if>
        <if test="jobtitle != null and jobtitle != ''">
          AND jobTitle like '%${jobtitle}%'
        </if>
        <if test="birth != null and birth != ''">
          AND birth like '%${birth}%'
        </if>
  </select>
  • UserServiceImpl
package com.szfore.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szfore.dao.MenuMapper;
import com.szfore.dao.UserMapper;
import com.szfore.dao.UserRoleMapper;
import com.szfore.model.*;
import com.szfore.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService{

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
 
    /**
     * 多条件分页查询用户
     * @param userParam
     * @param pageNum
     * @param pageSize
     * @return
     */
    public Json queryByPage(User userParam,Integer pageNum,Integer pageSize) {
        //利用PageHelper分页查询 注意:这个一定要放查询语句的前一行,否则无法进行分页,因为它对紧随其后第一个sql语句有效
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.queryByPage(userParam);
        PageInfo<User> pageInfo = new PageInfo<User>(userList);
        Json json = new Json();
        json.setMsg("成功!");
        json.setObj(pageInfo);
        json.setSuccess(true);
        return json;
    }
}

说明:PageInfo是PageHelper自带的分页对象类,详情如下:

当前页
private int pageNum;
每页的数量
private int pageSize;
当前页的数量
private int size;
//由于startRow和endRow不常用,这里说个具体的用法  
//可以在页面中"显示startRow到endRow 共size条数据"  

当前页面第一个元素在数据库中的行号
private int startRow;
当前页面最后一个元素在数据库中的行号
private int endRow;
总记录数
private long total;
总页数
private int pages;
结果集
private List<T> list;

第一页
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇JVM工作原理浅析 下一篇lock与synchronized的区别

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目