Mybatis的分页插件Pagehelper的基本使用

第一步导包

  
            junit
            junit
            4.11
            test
        


        
        
            org.mybatis
            mybatis
            3.4.6
        
        
        
            mysql
            mysql-connector-java
            5.1.47
        
        
        
            log4j
            log4j
            1.2.17
        

        
            org.mybatis.generator
            mybatis-generator-core
            1.3.5
        
        
        
            com.github.pagehelper
            pagehelper
            5.1.8
        

        
            com.alibaba
            fastjson
            1.2.47
        

第二步,配置配置文件

db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8
user=root
upwd=root

Mybatis的配置文件






    
    
    
        
        
        
        
    
    
    
        
        
        
        
    

    
    
        
            
            
            
        
    


    
    
        
            
            
            
            
                
                
                
                
                
                
                
                
            
        
    
    
        
        
        
        
        
        
        
        
    

实体类

package com.hw.entity;

import java.io.Serializable;

/**
 * @program: Maven
 * @description:
 * @author: hw
 * @create: 2019-01-01 00:42
 **/
public class Student implements Serializable {
    private Integer sid;
    private String sname;
    private Integer tid;




    public Student() {
        super();
    }

    public Student(String sname, Integer tid) {
        this.sname = sname;
        this.tid = tid;
    }

    public Student(Integer sid, String sname, Integer tid) {
        this.sid = sid;
        this.sname = sname;
        this.tid = tid;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", sname='" + sname + '\'' +
                ", tid=" + tid +
                '}';
    }

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public Integer getTid() {
        return tid;
    }

    public void setTid(Integer tid) {
        this.tid = tid;
    }
}

Mapper接口

package com.hw.mapper;

import com.hw.entity.Student;

import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @program: Maven
 * @description:
 * @author: hw
 * @create: 2019-01-01 02:45
 **/
public interface StudentMapper {

    @Select("select * from student where sname like '%${sname}%' and tid=#{tid}")
    public List findStudentByMap(Map map);

}

测试类

package com.hw.test;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.entity.Student;
import com.hw.mapper.StudentMapper;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: Maven
 * @description:
 * @author: hw
 * @create: 2019-01-03 09:14
 **/
public class demo1 {
    SqlSession sqlSession;
    SqlSessionFactory build;

    @Before
    public void before() {
        try {
            //读取配置文件
            InputStream inputStream = Resources.getResourceAsStream("Mybatis.xml");
            //通过sqlsessionFactoryBuilder创建sqlsessionFactory会话工厂
            build = new SqlSessionFactoryBuilder().build(inputStream);
            //通过sqlsessionFactory创建sqlsession
            sqlSession = build.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @After
    public void after() {
        //关闭sqlsession
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void test1() throws IOException, InterruptedException {
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //查询返回值为 lsit
        Map map = new HashMap<>();
        map.put("tid", 1);
        map.put("sname", "1");

        //分页开始数和分页的条数
    Page objects = PageHelper.startPage(2, 2);
        List studentMap = mapper.findStudentByMap(map);
        System.err.println(studentMap);
        PageInfo pageInfo=new PageInfo(studentMap);
        String str = JSON.toJSONString(pageInfo);
        System.err.println(str);
        System.err.println(pageInfo.getList()+"_____________");
    }
}

输出的信息

然后看下官方文档对于这些参数的解释:

下面几个参数都是针对默认 dialect 情况下的参数。使用自定义 dialect 实现时,下面的参数没有任何作用。

  1. helperDialect:分页插件会自动检测当前的数据库链接,自动选择合适的分页方式。 你可以配置helperDialect属性来指定分页插件使用哪种方言。配置时,可以使用下面的缩写值:
    oracle,mysql,mariadb,sqlite,hsqldb,postgresql,db2,sqlserver,informix,h2,sqlserver2012,derby
    特别注意:使用 SqlServer2012 数据库时,需要手动指定为 sqlserver2012,否则会使用 SqlServer2005 的方式进行分页。
    你也可以实现 AbstractHelperDialect,然后配置该属性为实现类的全限定名称即可使用自定义的实现方法。

  2. offsetAsPageNum:默认值为 false,该参数对使用 RowBounds 作为分页参数时有效。 当该参数设置为 true 时,会将 RowBounds 中的 offset 参数当成 pageNum 使用,可以用页码和页面大小两个参数进行分页。

  3. rowBoundsWithCount:默认值为false,该参数对使用 RowBounds 作为分页参数时有效。 当该参数设置为true时,使用 RowBounds 分页会进行 count 查询。

  4. pageSizeZero:默认值为 false,当该参数设置为 true 时,如果 pageSize=0 或者 RowBounds.limit = 0 就会查询出全部的结果(相当于没有执行分页查询,但是返回结果仍然是 Page 类型)。

  5. reasonable:分页合理化参数,默认值为false。当该参数设置为 true 时,pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页。默认false 时,直接根据参数进行查询。

  6. params:为了支持startPage(Object params)方法,增加了该参数来配置参数映射,用于从对象中根据属性名取值, 可以配置 pageNum,pageSize,count,pageSizeZero,reasonable,不配置映射的用默认值, 默认值为pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero

  7. supportMethodsArguments:支持通过 Mapper 接口参数来传递分页参数,默认值false,分页插件会从查询方法的参数值中,自动根据上面 params 配置的字段中取值,查找到合适的值时就会自动分页。 使用方法可以参考测试代码中的 com.github.pagehelper.test.basic 包下的 ArgumentsMapTest 和 ArgumentsObjTest

  8. autoRuntimeDialect:默认值为 false。设置为 true 时,允许在运行时根据多数据源自动识别对应方言的分页 (不支持自动选择sqlserver2012,只能使用sqlserver),用法和注意事项参考下面的场景五

  9. closeConn:默认值为 true。当使用运行时动态数据源或没有设置 helperDialect 属性自动获取数据库类型时,会自动获取一个数据库连接, 通过该属性来设置是否关闭获取的这个连接,默认true关闭,设置为 false 后,不会关闭获取的连接,这个参数的设置要根据自己选择的数据源来决定。

重要提示:

当 offsetAsPageNum=false 的时候,由于 PageNum 问题,RowBounds查询的时候 reasonable 会强制为 false。使用 PageHelper.startPage 方法不受影响。

 

 

官网教程:https://pagehelper.github.io/docs/howtouse/

 

点赞或者评论是我最大的动力,有问题欢迎留言或者联系q:1559810637

你可能感兴趣的:(后端)