Mybatis的分頁插件PageHelper使用及注意事項

(一)PageHelper分頁插件介紹

*筆者採用spring+springMVC+mybatis框架做java後臺開發,開發軟件是IntellijIDEA(用過之後已卸掉eclipse),項目爲maven工程。

使用方法

(在已有ssm框架基礎上)

1.mybatis的配置文件中添加代碼:

  <plugins>
    <!-- com.github.pagehelper爲PageHelper類所在包名 -->
    <plugin interceptor="com.github.pagehelper.PageHelper">
        <property name="dialect" value="mysql"/>
        <!-- 該參數默認爲false -->
        <!-- 設置爲true時,會將RowBounds第一個參數offset當成pageNum頁碼使用 -->
        <!-- 和startPage中的pageNum效果一樣-->
        <property name="offsetAsPageNum" value="true"/>
        <!-- 該參數默認爲false -->
        <!-- 設置爲true時,使用RowBounds分頁會進行count查詢 -->
        <property name="rowBoundsWithCount" value="true"/>
    </plugin>
</plugins>

2.maven工程添加dependency自動下載jar包,將以下代碼添加到pom.xml

    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>4.0.0</version>
    </dependency>

3.配置完成,等下完jar包就可以用了。。哈哈哈哈哈哈

3.1 controller
    @ResponseBody
    @RequestMapping(value = "selectPositionWithCondition.do")
    public AjaxResultPo deleteDepart(PositionManagerPO positionManagerPO){
        PageInfo<PositionManagerVO> pageInfo = positionService.getMessagesByCondition(positionManagerPO);
        return new AjaxResultPo(true,pageInfo);
    }

*需要引 import com.github.pagehelper.PageInfo;
這裏的重點就是controller調用service實現類的getMessagesByCondition方法,返回一個以PageInfo包裝的一個list,(實際從數據庫中查出的是list<>)

3.2 Service實現類
 @Autowired
    private PositionManagerMapper mapper;

    public PageInfo<PositionManagerVO> getMessagesByCondition(PositionManagerPO positionManagerPO) {
        PageHelper.startPage(1,10);//這句是重點,利用分頁插件將接下來的一條查詢的sql限定在第一頁,查10條數據
        List<PositionManagerVO> list = mapper.selectPosition1(positionManagerPO);//調用mapper層執行查詢
        PageInfo<PositionManagerVO> page = new PageInfo<PositionManagerVO>(list);//包裝到PageInfo,傳給controller
        //測試PageInfo全部屬性,PageInfo的強大之處,它攜帶以下信息
        System.out.println("PageNum: "+page.getPageNum()); //2  第幾頁
        System.out.println("PageSize: "+page.getPageSize()); //3  每頁包含的條數
        System.out.println("StartRow: "+page.getStartRow()); //4  顯示的頁面的第一條的
        System.out.println("EndRow: "+page.getEndRow());  //6   顯示的頁面的最後一條的
        System.out.println("Total: "+page.getTotal());  //8   總記錄數
        System.out.println("Pages: "+page.getPages());  //3   總頁數
        System.out.println("FirstPage: "+page.getFirstPage());  //1  第一頁
        System.out.println("LastPage: "+page.getLastPage());   //3  最後一頁
        System.out.println("isHasPreviousPage: "+page.isHasPreviousPage());
        System.out.println("isHasNextPage: "+page.isHasNextPage());
        return page;
    }
3.3 mapper
@Component
public interface PositionManagerMapper {
    public List<PositionManagerVO> selectPosition1(PositionManagerPO positionManagerPO);
}

mapper中什麼都沒有。。。

3.4 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.channelsoft.dao.PositionManagerMapper">

    <select id="selectPosition1" resultType="java.util.ArrayList" parameterType="com.channelsoft.model.PositionManagerPO" resultMap="PositionResultMap">
        select * from t_position_manager

    </select>
</mapper>

同樣簡單粗暴的查詢

接下來是本文的重點。。

(二)注意事項

1. pageHelper只支持使用後的下一條查詢語句,而且是用limit ?,? 實現的
2.pageHelper與註解形式的@SelectProvider不兼容,需使用xml文件寫動態sql語句
3.maven工程默認不編譯.xml文件,在工程的target文件夾下看不到對應的.xml,需要在pom.xml的build標籤裏添加以下內容:

    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
        <filtering>true</filtering>
      </resource>
    </resources>

歡迎:轉載,評論,提問

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章