1. 程式人生 > >Mybatis使用入門,這一篇就夠了

Mybatis使用入門,這一篇就夠了

mybatis中,封裝了一個sqlsession 物件(裡面封裝有connection物件),由此物件來對資料庫進行CRUD操作。

執行流程

mybatis有一個配置的xml,用於配置資料來源、對映Mapping,xml的檔名可以任取,為了方便,我們還是起mybatis-config.xml

我們讀取此配置的xml,獲得一個sqlsession,之後由此物件類進行資料庫的CRUD操作

Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = factory.openSession();

入門使用

1. 建立實體類和Dao類

2. 配置mybatis-config.xml檔案,配置資料來源

<?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>
    <!-- 引入外部資原始檔-->
    <properties resource="jdbc.properties"/>

    <!-- 配置資料來源環境 -->
    <environments default="development">
        <environment id="development">
            <!-- 資料庫事務管理型別 -->
            <transactionManager type="JDBC"/>
            <!-- 資料來源,type=pooled 說明是使用連線池方式,可以節省資源 -->
            <dataSource type="POOLED">
                <!-- 呼叫資原始檔裡的使用者資訊-->
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
</configuration>

3. 定義連線資料庫工具,可以獲得sqlsession物件

Dao類中每次進行CRUD操作,都要執行一次openSession方法來獲得SqlSession物件,造成資源的浪費和程式碼的重複

所以,和之前的JdbcUtil工具類一樣,我們也定義定義一個工具類MyBatisUtil,用來返回SQLSession物件

static SqlSessionFactory sqlSessionFactory = null;

static {
    try {
        // 載入mybatis配置檔案,並建立SqlSessionFactory例項
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //這個build方法可以接受幾種不同的引數,如Reader/InputSteam等
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    } catch (IOException e) {

    }
}

public static SqlSession getSqlSession() {
    return sqlSessionFactory.openSession();
}

public static void closeSqlSession(SqlSession sqlSession){
    if (sqlSession != null) {
        sqlSession.close();
    }
}

4. sql語句寫在mapper中

mapper檔案放在了resources下面

Mybatis中,sql語句則是寫在了xml檔案中,這些xml檔案也稱為mapper對映檔案

mapper標籤如果帶有xmln屬性,IDEA會報解析xml錯誤,得把xmln屬性刪除

<?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">
<!--
namespace: 名稱空間,用於標識每一個Mapper XML檔案中的語句,預防在不同的Mapper XML檔案中存在相同的語句ID
-->
<mapper namespace="employeeMapper">
    <!--
        resultType: 也稱為自動對映,只有在表的列名與POJO類的屬性完全一致時使用,會比較方便,全類名
    -->
    <select id="selectAll" resultType="com.wan.bean.Employee">
        select * from employee
    </select>
</mapper>

5. 在mybatis-config.xml檔案中註冊mapper

<?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>
    <!-- 省略資料來源配置-->
    <mappers>
        <mapper resource="com/wan/mapping/employeeMapper.xml"/>
        <!--如果還有mapper,則繼續新增 -->
    </mappers>
</configuration>

6. dao類通過sqlsession進行查詢

SqlSession sqlSession = MybatisUtil.getSqlSession();
// 呼叫語句,如果有引數,傳入引數 
//引數為名稱空間namespace+id,執行xml中的sql語句
List<Employee> list = sqlSession.selectList("employeeMapper.selectAll");

PS:如果是插入、更新和刪除操作,還需要提交操作,預設是不會自動提交的

sqlSession.commit();

補充

1.typeAliases標籤

<select id="selectAll" resultType="com.wan.bean.Employee">
    select * from employee
</select>

resultType屬性需要全類名,我們可以使用typeAliases標籤來簡化輸入

typeAliases標籤需要在mybatis-config.xml檔案中進行設定

<?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>
    <properties resource="jdbc.properties"/>
    <!--指定一個bean包 -->
    <typeAliases>
        <package name="com.wan.bean"/>
    </typeAliases>
    <!--省略配置資料來源等-->
</configuration>

之後我們的mapper檔案中就可以這樣寫

<!--resultType就可以不用寫全包名 -->
<select id="selectAll" resultType="Employee">
    select * from employee
</select>

我這裡只介紹用法,詳解請看下面的參考連結

參考 MyBatis 配置 typeAliases 詳解

2.引入mapper的四種方法

1. 檔案路徑註冊

<mappers>
    <mapper resource="com/wan/mapper/EmployeeMapper.xml" />
</mappers>

2. 包名掃描註冊

<mappers>
    <package name="com.wan.mapper" />
</mappers>

使用這種,必須保證xxxMapper.java和xxxMapper.xml兩者名字一模一樣!而且是要在同一包裡

3. 類名註冊

<mappers>
    <mapper class="com.shizongger.chapter2.mapper.UserMapper" />
</mappers>

4. url註冊

<mappers>
    <mapper url="file:/home/shizongger/workspace/Chapter3/src/com/shizongger/chapter2/mapper/RoleMapper.xml" />
</mappers>

參考:配置MyBatis時報錯資訊的解決方案

SQLSession方法說明

方法名 說明
insert 插入
delete 刪除
update 更新
selectOne 查詢單行結果,返回一個Object
selectList 查詢多行結果,返回一個List

使用和之前一樣,第一個引數傳入一個namespce+id,就可以找到指定的mapper檔案裡面的sql語句,並執行

CRUD

查詢

Employee中,屬性名和表的列名對應

<select id="selectAll" resultType="Employee">
    select * from employee
</select>

如果屬性和表的列名不一致,可以使用列名對映resultMap標籤,(也就是自動轉為別名)

<!--type也是需要全包名,由於之前定義的別名,所以就可以不寫-->
<resultMap id="baseResultMap" type="Employee">
    <!--使用對映,把對應的列名對映為對應的屬性 -->
    <id property="empno" column="EMPNO" />
    <result property="ename" column="ENAME"/>
    <result property="job" column="JOB"/>
    <result property="mgr" column="MGR"/>
    <result property="hiredate" column="HIREDATE"/>
    <result property="sal" column="SAL"/>
    <result property="comm" column="COMM"/>
    <result property="deptno" column="DEPTNO"/>
</resultMap>
<!--引用上面定義的resultMap-->
<select id="selectAll" resultMap="baseResultMap">
    select * from employee
</select>

帶條件查詢

<select id="selectById" parameterType="int" resultMap="baseResultMap">
    <!-- 如果引數型別是簡單的基本或者包裝型別,#{} 裡面的可以任取,都是可以獲得引數 -->
    select * from EMPLOYEE where EMPNO=#{id}
</select>

//使用
Employee e = sqlsession.selectOne("employeeMapper.selectById",7369);

上面的select語句相當於一個預編譯語句

String s = "SELECT * FROM employee WHERE empno=?";
PreparedStatement ps = conn.prepareStatement(s);
ps.setInt(1,empno);

多條件查詢

可以使用where標籤,當然,之前的單條件也可以使用where標籤,where標籤好處是會自動刪除多餘的and

<select id="selectSelective" parameterType="Employee" resultMap="baseResultMap">
    select * from EMPLOYEE
    <where>
        <!--自動刪除多餘的and -->
        <!--#相當於從傳入的bean物件(Employee)中通過getDeptno方法獲得屬性值 -->
        and deptno=#{deptno}
        and sal>=2000
    </where>
</select>

大小比較條件

條件中有大小比較,<號得通過CDATA存放條件

<select id="selectSelective" parameterType="Employee" resultMap="baseResultMap">
    select * from EMPLOYEE
    <where>
        <!--loSal為Employee的一個屬性,#{loSal}相當於是通過Employee物件的get方法來獲得loSal的屬性值 -->
        and SAL>=#{loSal}
        <!--CDATA中的資料不會被解析器解析 -->
        <![CDATA[ and SAL<=#{hiSal} ]]>
    </where>
</select>

#與$區別:

${}用在我們能夠確定值的地方,也就是我們程式設計師自己賦值的地方。
#{}一般用在使用者輸入值的地方!!

參考:
MyBatis中#{}和${}的不同和${}的妙用

模糊查詢:

模糊查詢中需要使用%等萬用字元,我們可以在xml中定義好,自動拼接萬用字元

<select id="selectSelective" parameterType="Employee" resultMap="baseResultMap">
    select * from EMPLOYEE
    <where>
        <if test="ename != null">
            <!--使用bind標籤,設定格式,自動拼接萬用字元 -->
            <bind name="pattern" value="'%' + ename + '%'"/>
            and ENAME like #{pattern}
        </if>
    </where>
</select>

動態查詢

Mybatis中提供了if標籤用來實現動態查詢,和JSTL標籤庫使用類似

<select id="selectSelective" parameterType="Employee" resultMap="baseResultMap">
    select * from EMPLOYEE
    <where>
        <!--#{ename}其實是通過Employee類中的get方法來獲得物件的ename屬性值 -->
        <if test="ename != null">
            and ename=#{ename}
        </if>
        <if test="job != null and job.trim().length>0">
            and JOB=#{job}
        </if>
        <if test="deptno != null">
            and DEPTNO=#{deptno}
        </if>
    </where>
</select>

插入

主鍵為序列

某個主鍵是由oracle中的序列生成的

<insert id="insert_1" parameterType="Employee">
    <!--
        keyProperty: 表示將從序列獲得的值賦予實體的哪個屬性
        order: 表示主鍵值生成的方式,可取值:BEFORE | AFTER
             由於不同的資料庫對插入的資料時主鍵生成方式是不同,例如:
             mysql and ms server: 主鍵生成方式為後生成方式。
             oracle: 主鍵生成方式預生成.
    -->
    <!--呼叫資料庫中的序列,並賦值給傳入的Employee物件的empno屬性 -->
    <selectKey keyProperty="empno" resultType="integer" order="BEFORE">
        select EMP_SEQ.nextval from dual
    </selectKey>
    <!--
        如果使用這種整表插入的方式,那當資料庫表的某些列可以為空值時,我將要告訴底層的JDBC驅動如何處理空值的情況,這不是mybatis所需要的,
        而是底層有些JDBC驅動所需的特性,實際上就是讓JDBC驅動去呼叫PrepareStatement.setNull()來設定空值
     -->
    <!--如果是常用的資料型別int,date等,jdbcType可以省略不寫 -->
    insert into EMPLOYEE
    values (#{empno},#{ename},#{job},#{mgr,jdbcType=INTEGER},#{hiredate,jdbcType=DATE},#{sal,jdbcType=DOUBLE},#{comm,jdbcType=DOUBLE},#{deptno,jdbcType=INTEGER})

</insert>

複用sql語句

把insert要插入的列名和數值寫在sql標籤裡,之後方便重用,之後重用的時候需要使用include子標籤拼接sql語句

<!--insert into employee(ENAME,JOB..) values(xx,xx) -->
<!--(ENAME,JOB..) -->
<sql id="insert_set_column">
    <!-- suffixOverrides屬性,會自動把多餘的“,”刪除 -->
    <trim prefix="(" suffix=")" suffixOverrides=",">
        empno,
        <if test="ename != null">ENAME,</if>
        <if test="job != null">JOB,</if>
        <if test="mgr != null">MGR,</if>
        <if test="hiredate != null">HIREDATE,</if>
        <if test="sal != null">SAL,</if>
        <if test="comm != null">COMM,</if>
        <if test="deptno != null">DEPTNO,</if>
    </trim>
</sql>
<!--(xx,xx,xx) -->
<sql id="insert_values">
    <trim prefix="values(" suffix=")" suffixOverrides=",">
        #{empno},
        <if test="ename != null">#{ename},</if>
        <if test="job != null">#{job},</if>
        <if test="mgr != null">#{mgr},</if>
        <if test="hiredate != null">#{hiredate},</if>
        <if test="sal != null">#{sal},</if>
        <if test="comm != null">#{comm},</if>
        <if test="deptno != null">#{deptno},</if>
    </trim>
</sql>

<insert id="insert_2" parameterType="Employee">
    <selectKey keyProperty="empno" resultType="integer" order="BEFORE">
        select EMP_SEQ.nextval from dual
    </selectKey>
    insert into EMPLOYEE 
    <!--拼接sql -->
    <include refid="insert_set_column"/> 
    <include refid="insert_values"/>
</insert>

更新

<update id="update_1" parameterType="Employee">
    update EMPLOYEE
    <set>
        <if test="ename != null and ename.trim().length>0">ENAME=#{ename},</if>
        <if test="job != null and job.trim().length>0">JOB=#{job},</if>
        <if test="mgr != null">MGR=#{mgr},</if>
        <if test="hiredate != null">HIREDATE=#{hiredate},</if>
        <if test="sal != null">SAL=#{sal},</if>
        <if test="comm != null">COMM=#{comm},</if>
        <if test="deptno != null">DEPTNO=#{deptno},</if>
    </set>
   <!-- <where>如果帶多條件的更依然可以使<where>元素動態生成where子句</where> -->
   where EMPNO=#{empno}
</update>
<update id="update_2" parameterType="Employee">
    update EMPLOYEE
    <trim prefix="set" suffixOverrides=",">
        <if test="ename != null and ename.trim().length>0">ENAME=#{ename},</if>
        <if test="job != null and job.trim().length>0">JOB=#{job},</if>
        <if test="mgr != null">MGR=#{mgr},</if>
        <if test="hiredate != null">HIREDATE=#{hiredate},</if>
        <if test="sal != null">SAL=#{sal},</if>
        <if test="comm != null">COMM=#{comm},</if>
        <if test="deptno != null">DEPTNO=#{deptno},</if>
    </trim>
   <!-- <where>如果帶多條件的更依然可以使<where>元素動態生成where子句</where> -->
   where EMPNO=#{empno}
</update>

刪除

<delete id="delete" parameterType="Employee">
    delete EMPLOYEE EMPNO=#{empno}
    <!--條件多的話也可以使用<where>...</where> -->
</delete>

w3c select標籤、delete標籤等詳解

高階使用

1.動態代理

我們之前,上面都是在Dao類中寫上一段sqlsession.selectOne/selectList,還是比較麻煩

所以mybatis提供了一種簡單的方法,使用動態代理(介面類)可以簡化步驟

Mybatis中有這樣的約定:

  1. 介面方法名與mapper中的id相同
  2. 介面方法引數與parameterType型別相同
  3. 介面方法的返回值型別與resultType型別相同

滿足上面的條件,Mybatis就會將介面類中的方法和mapper中的sql語句一一對應起來,而不需要再次新建一個Dao,在Dao類裡面編寫方法

具體步驟:

1. 實體類編寫

2. 新建介面類

如果方法的返回值為void,則mapper中就不需要定義resultType屬性

如果方法返回值是List,mapper中的resultType為泛型T

package com.wan.mapping;

import com.wan.bean.Employee;

import java.util.List;

/**
 * @author StarsOne
 * @date Create in  2019/9/16 0016 20:38
 * @description
 */
public interface EmployeeMapper {
    List<Employee> selectAll();
}

2. 編寫mapper.xml

<?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="com.wan.mapping.EmployeeMapper">
    <!--特例:返回值為list,resultType=bean類-->
    <select id="selectAll" resultType="Employee" >
        select * from employee
    </select>
</mapper>

3. 註冊mapper

這裡我們由於使用了package註冊mapper,一定保證xxmapper.java和xxmapper.xml兩個名字相同,大小寫都要一樣

保證Mapper.xml和介面的那個Mapper在相同的包路徑,在mybatis配置xml檔案指定

<?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>
    <!--省略資料來源配置 -->...
    <!-- 註冊SQL對映檔案,在這些檔案中寫SQL語句 -->
    <mappers>
        <!--指定整個包中的全部Mapper -->
        <package name="com.wan.mapper"/>
    </mappers>
</configuration>

4. 使用

使用還是和之前一樣,獲得SqlSession物件,此物件有個getMapper方法,把介面類傳入,就可以回撥介面的方法了

Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = factory.openSession();

EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
List<Employee> employees = mapper.selectAll();

介面類中的方法名與EmployeeMapper.xml中的對應

使用:

EmployeeMapper mapper = sqlsession.getMapper(EmployeeMapper.class);
mapper.selectById(7369);

2.遍歷列表

Mybatis中提供了foreach標籤,用於遍歷

如果方法引數傳入了一個List,可以使用此標籤遍歷,例子如下:

<!--相當於select * from employee where job in (...)) -->
<select id="selectByJobs" parameterType="list" resultMap="baseResultMap">
    select * from EMPLOYEE
    <where>
        <foreach item="job" collection="list" open="JOB IN(" close=")" separator=",">
            #{job}
        </foreach>
    </where>
</select>

foreach標籤的屬性主要有 item,index,collection,open,separator,close,使用和JSTL標籤裡面的foreach標籤差不多

屬性名 說明
item 表示集合中每一個元素進行迭代時的別名
index 指定一個名字,用於表示在迭代過程中,每次迭代到的位置,
open 表示該語句以什麼開始,
separator 表示在每次進行迭代之間以什麼符號作為分隔 符,
close 表示以什麼結束。

關鍵屬性:collection

  1. 如果傳入的是單引數且引數型別是一個List的時候,collection屬性值為list
  2. 如果傳入的是單引數且引數型別是一個array陣列的時候,collection的屬性值為array
  3. 如果傳入的引數是多個的時候,我們就需要把它們封裝成一個Map了,當然單引數也可以封裝成map,map的key就是引數名,所以這個時候collection屬性值就是傳入的List或array物件在自己封裝的map裡面的key

參考:mybatis 中 foreach collection的三種用法

3.考慮執行緒安全

使用ThreadLocal物件,保證每個執行緒取出的SqlSession是同一個物件

方法 說明
void set(Object value) 設定當前執行緒的執行緒區域性變數的值。
public Object get() 該方法返回當前執行緒所對應的執行緒區域性變數。
public void remove() 將當前執行緒區域性變數的值刪除,目的是為了減少記憶體的佔用,該方法是JDK 5.0新增的方法。
protected Object initialValue() 返回該執行緒區域性變數的初始值,該方法是一個protected的方法,顯然是為了讓子類覆蓋而設計的。
static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();
//設定
threadLocal.set(sqlsession);
//取出
SqlSession s = threadLocal.get();

巢狀查詢

<!-- 結果集對映: 列《》屬性名 -->
<resultMap id="baseResultMap" type="Employee">
    <!-- 專門對映主鍵列 -->
    <id property="empno" column="EMPNO" />
    <result property="ename" column="ENAME"/>
    <result property="job" column="JOB"/>
    <result property="mgr" column="MGR"/>
    <result property="hiredate" column="HIREDATE"/>
    <result property="sal" column="SAL"/>
    <result property="comm" column="COMM"/>
    <result property="deptno" column="DEPTNO"/>
</resultMap>

<!-- 擴充套件另一個結果對映 -->
<resultMap id="extendBaseResultMap" type="Employee" extends="baseResultMap">
    <association property="department" javaType="Department">
        <!-- 關聯的巢狀結果 -->
        <id property="deptno" column="DEPTNO"/>
        <result property="dname" column="DNAME"/>
        <result property="location" column="LOC"/>
    </association>
</resultMap>


<!--
    1.巢狀結果(推薦使用)
    優點:效能好,一條語句把所有實體的資料完全查詢出來。
    缺點:對SQL編寫的要求高了,因為涉及多表連線查詢
-->
<select id="selectById" resultMap="extendBaseResultMap" parameterType="int">
    select e.EMPNO,
          e.ENAME,
          e.JOB,
          e.MGR,
          e.HIREDATE,
          e.SAL,
          e.COMM,
          d.DEPTNO,
          d.DNAME,
          d.LOC
    from EMPLOYEE E
      inner join DEPARTMENT D
          on E.DEPTNO = D.DEPTNO
     where E.EMPNO=#{id}
</select>

<!--
    2. 巢狀查詢
    優點:編寫SQL簡單,無需做多表的連線查詢;關聯的實體通過單獨的SQL語句查詢並單獨封裝。
    缺點:執行了N+1條件語句。效能差
-->
<resultMap id="extendBaseResultMap_2" type="Employee" extends="baseResultMap">
    <association property="department" column="DEPTNO" select="selectDepartmentById" />
</resultMap>

<select id="selectDepartmentById" parameterType="int" resultType="Department">
    select deptno,
           dname,
           loc as location
    from DEPARTMENT where DEPTNO=#{id}
</select>

<select id="selectById_2" resultMap="extendBaseResultMap_2" parameterType="int">
    select e.EMPNO,
          e.ENAME,
          e.JOB,
          e.MGR,
          e.HIREDATE,
          e.SAL,
          e.COMM,
          e.DEPTNO
    from EMPLOYEE E
     where E.EMPNO=#{id}
      <!-- or e.empno=7902 or e.empno=7844 -->
</select>

分頁查詢

分頁的話,像以前那樣使用三層巢狀查詢也可以實現。

不過,有開發者為Mybatis開了個一個外掛PageHelper,可以用來更為簡單地使用分頁查詢

1.新增jar包

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <!--自動下載最新版本 -->
    <version>REALSE</version>
</dependency>

2.配置攔截器外掛

<!-- 
    plugins在配置檔案中的位置必須符合要求,否則會報錯,順序如下:
    properties?, settings?, 
    typeAliases?, typeHandlers?, 
    objectFactory?,objectWrapperFactory?, 
    plugins?, 
    environments?, databaseIdProvider?, mappers?
-->
<plugins>
    <!-- com.github.pagehelper為PageHelper類所在包名 -->
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
        <!-- 使用下面的方式配置引數,後面會有所有的引數介紹 -->
        <property name="param1" value="value1"/>
    </plugin>
</plugins>

3.程式碼使用

只有在查詢之前呼叫過startPage或者是offsetPage方法,後面的查詢出來的List結果就會進行分頁查詢

下面的兩個都是查詢第一頁,每一頁有10條資料

//第二種,Mapper介面方式的呼叫,推薦這種使用方式。

PageHelper.startPage(1, 10);
List<Employee> employees = employeeMapper.selectAll();

//第三種,Mapper介面方式的呼叫,推薦這種使用方式。
PageHelper.offsetPage(1, 10);
List<Employee> employees = employeeMapper.selectAll();

這裡提一下,這個外掛還帶有一個PageInfo類,裡面有可以記錄各種資訊

剛開始,我以為是和我之前自己封裝的Page一樣,詳情請看Jsp學習筆記(4)——分頁查詢

但是,其實不一樣的,這個PageInfo就是一個封裝而已,只是用來存放資料而已,裡面有各種資訊

屬性 說明
pageNum 當前頁號(第幾頁)
pageSize 每頁的顯示的資料個數
size 當前頁的顯示的資料個數
startRow 當前頁面第一個元素在資料庫中的行號
endRow 當前頁面最後一個元素在資料庫中的行號
pages 總頁數
prePage 上一頁的頁號
nextPage 下一頁頁號
isFirstPage 是否為第一頁
isLastPage 是否為最後一頁
hasPreviousPage 是否有前一頁
hasNextPage 是否有下一頁
navigatePages 導航頁碼數
navigatepageNums 所有導航頁號
navigatePages 導航條上的第一頁
navigateFirstPage 導航條上的第一頁
navigateLastPage 導航條上的最後一頁

有個getTotal方法,可以獲得查詢結果的總記錄數

PageHelper.startPage(1, 10);
List<Employee> employees = mapper.selectAll();
PageInfo<Employee> pageInfo = new PageInfo<>(employees);