zoukankan      html  css  js  c++  java
  • mybatis分页插件PageHelper使用

    一、环境

    开发工具:idea

    jdk:1.7

    mysql:5

    框架:spring+springmvc+mybatis

    使用maven来管理项目

    二、与ssm整合

    • 第一步:在pom.xml引入PageHelper的依赖
    <!-- 引入mybatis的 pagehelper 分页插件 -->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>5.1.2</version>
            </dependency>
    • 第二步:在mybatis的全局配置文件中配置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>
    	<!-- 引入 pageHelper插件 -->
    	<!--注意这里要写成PageInterceptor, 5.0之前的版本都是写PageHelper, 5.0之后要换成PageInterceptor-->
    	<plugins>
    		<plugin interceptor="com.github.pagehelper.PageInterceptor">
    			<!--reasonable:分页合理化参数,默认值为false,直接根据参数进行查询。
    			  当该参数设置为 true 时,pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页。-->
    			<!--<property name="reasonable" value="true"/>-->
    		</plugin>
    	</plugins>
    </configuration>

    三、使用

    例如:实现对用户的多条件查询

    • User实体类
    package com.szfore.model;
    
    import java.util.Date;
    import java.util.List;
    
    public class User {
        private Integer id;
    
        private String uname;
    
        private String pwd;
    
        private String name;
    
        private Integer sex;
    
        private String phone;
    
        private String company;
    
        private String jobtitle;
    
        private String birth;
    
        private Date createdate;
    
        private Date lastlogintime;
    
        private List<Role> roleList;
    
        public List<Role> getRoleList() {
            return roleList;
        }
    
        public void setRoleList(List<Role> roleList) {
            this.roleList = roleList;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getUname() {
            return uname;
        }
    
        public void setUname(String uname) {
            this.uname = uname == null ? null : uname.trim();
        }
    
        public String getPwd() {
            return pwd;
        }
    
        public void setPwd(String pwd) {
            this.pwd = pwd == null ? null : pwd.trim();
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name == null ? null : name.trim();
        }
    
        public Integer getSex() {
            return sex;
        }
    
        public void setSex(Integer sex) {
            this.sex = sex;
        }
    
        public String getPhone() {
            return phone;
        }
    
        public void setPhone(String phone) {
            this.phone = phone == null ? null : phone.trim();
        }
    
        public String getCompany() {
            return company;
        }
    
        public void setCompany(String company) {
            this.company = company == null ? null : company.trim();
        }
    
        public String getJobtitle() {
            return jobtitle;
        }
    
        public void setJobtitle(String jobtitle) {
            this.jobtitle = jobtitle == null ? null : jobtitle.trim();
        }
    
        public String getBirth() {
            return birth;
        }
    
        public void setBirth(String birth) {
            this.birth = birth == null ? null : birth.trim();
        }
    
        public Date getCreatedate() {
            return createdate;
        }
    
        public void setCreatedate(Date createdate) {
            this.createdate = createdate;
        }
    
        public Date getLastlogintime() {
            return lastlogintime;
        }
    
        public void setLastlogintime(Date lastlogintime) {
            this.lastlogintime = lastlogintime;
        }
    }
    
    
    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>
      </WHERE>
    </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;
    
    第一页
    private int firstPage;
    前一页
    private int prePage;
    
    是否为第一页
    private boolean isFirstPage = false;
    是否为最后一页
    private boolean isLastPage = false;
    是否有前一页
    private boolean hasPreviousPage = false;
    是否有下一页
    private boolean hasNextPage = false;
    导航页码数
    private int navigatePages;
    所有导航页号
    private int[] navigatepageNums;
    View Code
  • 相关阅读:
    ctf web 百度杯”CTF比赛 九月场Upload i春秋
    ctf web 西普实验吧 登陆一下好吗 MySQL隐式转化 MySQL表达式的计算顺序
    Firefox 47.0.1
    给数组原型添加方法
    JS中几种常见的数组算法(前端面试必看)
    进制转换技巧解析
    redis通过6379端口无法连接服务器
    阿里云图片或文件上传 启动时报Error creating bean with name 'ossClient'问题
    20170628-三七互娱-测试工程师(提前批)
    20170514-vivo-软件工程师Java(提前批)
  • 原文地址:https://www.cnblogs.com/helf/p/11098105.html
Copyright © 2011-2022 走看看