SpringBoot中Mybaties PageHelper插件使用

首先引入pom.xml文件配置html

    <!-- mybatis -->
    <dependency>
     <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.1</version>
    </dependency>
        
     <!--mybatis分頁插件-->
        <dependency>
             <groupId>com.github.pagehelper</groupId>
             <artifactId>pagehelper-spring-boot-starter</artifactId>
             <version>1.1.0</version>
        </dependency>

在application.properties中進行配置mysql

#mybatis
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.CarManage.dao
 
#pagehelper
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql

Controller中git

@Controller
@RequestMapping(value="/admin")
public class MessageController {
 
    @Autowired
    private IMessages iMessages;
    
    @GetMapping(value="myMessage")
    public String messageList(@RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
            @RequestParam(value = "pageSize", defaultValue = "5") int pageSize, HttpServletRequest request){
        PageInfo<CarMessage> messageList =     iMessages.findItemByPage(currentPage, pageSize);
        request.setAttribute("messageList", messageList);
        return "myMessage";
    }
}

傳三個參數,currentPage 當前頁碼,pageSize每頁顯示多少數據,HttpServletRequest用來封裝數據
使用@RequestParam進行參數設置,value和defaultValue意指當URL直接請求「/admin/myMessage」時,默認傳值參數名稱是currentPage,默認值是1;默認傳值參數名稱是pageSize,默認值是5。此次請求是在別的頁面跳轉此頁面時發生。當進行分頁請求時,就會真實傳入這2個參數。能夠發現request中封裝進去了PageInfo對象。這是PageHelper中的東西。裏面封裝了github

n多成員變量,在前臺直接調用就能夠。spring

public class PageInfo<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    //當前頁
    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 prePage;
    //下一頁
    private int nextPage;
 
    //是否爲第一頁
    private boolean isFirstPage = false;
    //是否爲最後一頁
    private boolean isLastPage = false;
    //是否有前一頁
    private boolean hasPreviousPage = false;
    //是否有下一頁
    private boolean hasNextPage = false;
    //導航頁碼數
    private int navigatePages;
    //全部導航頁號
    private int[] navigatepageNums;
    //導航條上的第一頁
    private int navigateFirstPage;
    //導航條上的最後一頁
    private int navigateLastPage;

等等東西。很方便使用。下面會介紹怎麼使用。sql

而後再來看Service數據庫

@Service
@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
public class MessagesServiceImpl implements IMessages{
 
        @Autowired
        private CarMessageMapper carMessageMapper;
 
        @Override
        public PageInfo<CarMessage> findItemByPage(int currentPage, int pageSize) {
            PageHelper.startPage(currentPage, pageSize);
            List<CarMessage> allMessages = carMessageMapper.findAll();   
            PageInfo<CarMessage> pageInfo = new PageInfo<>(allMessages);
            return pageInfo;
        }
    
}
//設置分頁信息保存到threadlocal中
PageHelper.startPage(currentPage, pageSize); //必定要放在查詢以前
//緊跟着的第一個select方法會被分頁,contryMapper會被PageInterceptor截攔,截攔器會從threadlocal中取出分頁信息,把分頁信息加到sql語句中,實現了分頁查旬
 List<CarMessage> allMessages = carMessageMapper.findAll();   

將list結果集進行pageInfo包裹,返回包裹對象到Controller中。bootstrap

前天,我使用的是Thymeleaf模板引擎mybatis

 <table id="myMessageTable"  data-toggle="table" >
                <thead>
                            <tr>
                                <th>消息日期</th>
                                <th>消息內容</th>
                                <th>消息狀態</th>
                                <th>操做</th>
                            </tr>
                         </thead>
                            <tbody>
                            <th:block th:each="messageList : ${messageList.list}">
                                <tr th:attr="id=${messageList.id}">
                                    <td  th:text="${#dates.format(messageList.createtime,'yyyy-MM-dd HH:mm:ss')}"  ></td>
                                    <td th:text="${messageList.content}"></td>
                                    <td>
                                       <th:block th:if="${messageList.status == 0}">
                                            <span>未讀</span>
                                        </th:block>
                                        <th:block th:if="${messageList.status == 1}">
                                            <span>已讀</span>
                                        </th:block>
                                    </td>
                                    <td>-</td>
                                </tr>
                            </th:block>
                            </tbody>
                </table>
<div th:replace="common/pagination :: pageNav(${messageList})"></div>

pageInfo中有一個成員變量list,封裝了分頁查詢的結果集,因此在前臺直接循環pageInfo中的list便可。
使用th:include或者th:replace引入th:fragment定義的分頁按鈕片斷,必定要在循環外,由於傳入的要是pageInfo整個對象,而不是listapp

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>
 
<div th:fragment="pageNav(pageInfo)">
 
<div class="fixed-table-pagination" style="display: block;" >
    <div class="pull-left pagination-detail">
    <span class="pagination-info">顯示第 <span th:text="${pageInfo.startRow}"></span> 到第 <span th:text="${pageInfo.endRow}"></span> 條記錄,總共 <span th:text="${pageInfo.total}"></span>條記錄</span>
        <span class="page-list">每頁顯示 
        <span class="btn-group dropup">
            <button type="button" class="btn btn-default  dropdown-toggle" data-toggle="dropdown">
                <span class="page-size"  th:text="${pageInfo.pageSize}" ></span>
                 <span class="caret"></span>
             </button>
                <ul class="dropdown-menu" role="menu">
                <li  th:class="${pageInfo.pageSize== 5 }?'active':''"><a th:href="@{?pageSize=5}" >5</a></li>
                <li  th:class="${pageInfo.pageSize== 8 }?'active':''"><a th:href="@{?pageSize=8}" >8</a></li>
             </ul>
         </span> 
         條記錄
     </span>
     </div>
</div>
 
 
<div   class="dataTables_paginate paging_bootstrap pagination" >
        <ul >
            <li th:if="${pageInfo.hasPreviousPage}">
                <a th:href="@{'?currentPage='+${pageInfo.prePage}}" >
                    ← Previous
                </a>
            </li>
 
            <th:block th:each="nav : ${pageInfo.navigatepageNums}">
                <li th:class="${nav==pageInfo.pageNum}?'active':''"><a th:href="@{'?currentPage='+${nav}}" th:text="${nav}"></a>
                </li>
            </th:block>
 
            <th:block th:if="${pageInfo.hasNextPage}">
                <li>
                    <a th:href="@{'?currentPage='+${pageInfo.nextPage}}" >
                        Next → 
                    </a>
                </li>
            </th:block>
 
        </ul>
    </div>
 
</div>
 
</body>
</html>

這個能夠做爲通用。在這裏直接調用pageInfo全部你須要的成員變量便可。很方便。效果如圖。

這是別的頁面跳轉進來的時候,URL訪問路徑,並沒有參數。因此以默認方式分頁,默認定位在第一頁,每頁顯示5條數據。

點擊第二頁的時候,觀察URL有參數傳入。以傳入方式顯示。

相關文章
相關標籤/搜索