SpringCloud+MyBatis分頁處理(前後端分離)
阿新 • • 發佈:2020-11-18
分頁處理,這是做JavaWeb專案中常見的場景。
背景:
1.系統架構:SpringCloud分散式
2.持久層:MyBatis
3.前端:前後分離vue.js/bootstrap等.
後臺提供restful api 介面,前端訪問後端介面展示資料。
2種方式提供分頁處理方案:
一、直接MyBatis資料庫進行分頁
controller介面
@ApiImplicitParams({ @ApiImplicitParam(name = "categoryId", value = "支付渠道大類Id", required = false, dataType = "Long", paramType = "query"), @ApiImplicitParam(name = "payChannelId", value = "支付渠道ID", required = false, dataType = "Long", paramType = "query"), @ApiImplicitParam(name = "bankCode", value = "銀行名稱code", required = false, dataType = "String", paramType = "query"), @ApiImplicitParam(name = "startTime", value = "建立時間起始(示例:2018-09-05)", required = false, dataType = "String", paramType = "query", length = 10), @ApiImplicitParam(name = "endTime", value = "建立時間截止(示例:2018-09-05)", required = false, dataType = "String", paramType = "query", length = 10), @ApiImplicitParam(name = "pageNum", value = "查詢開始頁", required = true, dataType = "int", paramType = "query"), @ApiImplicitParam(name = "pageSize", value = "查詢的頁面大小不需要分頁則把此值填大一點", required = true, dataType = "int", paramType = "query")}) public PageResult<ChannelBaseDataResp> queryPayChannel( @RequestParam(value = "categoryId", required = false) Long categoryId, @RequestParam(value = "payChannelId", required = false) Long payChannelId, @RequestParam(value = "bankCode", required = false) String bankCode, @RequestParam(value = "startTime", required = false) String startTime, @RequestParam(value = "endTime", required = false) String endTime, @RequestParam(value = "pageNum", required = true) Integer pageNum, @RequestParam(value = "pageSize", required = true) Integer pageSize) { PageResult<ChannelBaseDataResp> response = new PageResult<ChannelBaseDataResp>(); try { int endRowNo = pageNum * pageSize; int beginRowNo = (pageNum - 1) * pageSize + 1; return channelBaseDataService.queryBaseData(categoryId,payChannelId,bankCode,startTime,endTime,beginRowNo,endRowNo); } catch (Exception e) { response = new PageResult<ChannelBaseDataResp>(); if (e instanceof AppException) { response.setCode(((AppException) e).getErrorCode()); response.setResult(((AppException) e).getErrorMsg()); } else { response.setCode(CumReturnCode.SYSTEM_EXCEPTION.code); response.setResult(CumReturnCode.SYSTEM_EXCEPTION.message); } return response; } }
Swagger-ui介面介面如下:
pageNum 和 pageSize 用於前端分頁的引數,pageNum:表示頁碼第幾頁,pageSize:表示每頁展示資料數量。
所有查詢條件引數,在mapper-xml裡進行處理,再利用資料庫oracle本身的rownum行值進行分頁。
xml程式碼:
<select id="queryBaseDataList" resultMap="BaseResultMap"> select ID, PAY_CHANNEL_MAIN_ID, PAY_CHANNEL_ID, BANK_CODE, CREATE_TIME, UPDATE_TIME,UPDATOR from ( select A.*, rownum RN from ( select ID, PAY_CHANNEL_MAIN_ID, PAY_CHANNEL_ID, BANK_CODE, CREATE_TIME, UPDATE_TIME,UPDATOR from CUM_PAY_CHANNEL_BASE <where> <if test="beginCreateTime != null"> CREATE_TIME <![CDATA[ >= ]]> #{beginCreateTime,jdbcType=TIMESTAMP} </if> <if test="endCreateTime != null"> AND CREATE_TIME <![CDATA[ <= ]]> #{endCreateTime,jdbcType=TIMESTAMP} </if> <if test="categoryId != null and categoryId != '' "> AND PAY_CHANNEL_MAIN_ID = #{categoryId,jdbcType=DECIMAL} </if> <if test="payChannelId != null and payChannelId != ''"> AND PAY_CHANNEL_ID = #{payChannelId,jdbcType=DECIMAL} </if> <if test="bankCode != null and bankCode != ''"> AND BANK_CODE = #{bankCode,jdbcType=VARCHAR} </if> </where> order by CREATE_TIME desc ) A where rownum <= #{endRowNo,jdbcType=DECIMAL} ) where RN >= #{beginRowNo,jdbcType=DECIMAL} </select>
優點:直觀、方便、易排查問題。 缺點:訪問資料庫過於頻繁,未利用到mybatis本身的快取優勢。
二、Java+快取分頁
這種方法對於前端而言是沒變化,無感的。
只是在後端處理稍作變動,思路:
1.先把所有資料記錄查詢出來
2.資料庫實體再次封裝為查詢實體
3.純java程式碼進行分頁
controller介面不變
service業務處理——>資料庫crud操作變動
把所有資料庫記錄查詢處理
<select id="queryBaseDataList" resultMap="BaseResultMap"> select ID, PAY_CHANNEL_MAIN_ID, PAY_CHANNEL_ID, BANK_CODE, CREATE_TIME, UPDATE_TIME,UPDATOR from CUM_PAY_CHANNEL_BASE <where> <if test="beginCreateTime != null"> CREATE_TIME <![CDATA[ >= ]]> #{beginCreateTime,jdbcType=TIMESTAMP} </if> <if test="endCreateTime != null"> AND CREATE_TIME <![CDATA[ <= ]]> #{endCreateTime,jdbcType=TIMESTAMP} </if> <if test="categoryId != null and categoryId != '' "> AND PAY_CHANNEL_MAIN_ID = #{categoryId,jdbcType=DECIMAL} </if> <if test="payChannelId != null and payChannelId != ''"> AND PAY_CHANNEL_ID = #{payChannelId,jdbcType=DECIMAL} </if> <if test="bankCode != null and bankCode != ''"> AND BANK_CODE = #{bankCode,jdbcType=VARCHAR} </if> </where> order by CREATE_TIME desc </select>
再按照pageNum和pageSize進行分頁處理
//以分頁形式輸出給前端 List<ChannelRouteGroupResp> resultList = new ArrayList<>(); if(groupRespList.size() >= endRowNo){ for(int i=(beginRowNo-1); i< endRowNo; i++){ resultList.add(groupRespList.get(i)); } }else{ for(int i=(beginRowNo-1); i< groupRespList.size(); i++){ resultList.add(groupRespList.get(i)); } }
優點:利用了mybatis快取機制,分頁查詢快速,減少資料庫訪問次數。
缺點:當資料量大的時候,100W+;在沒有條件查詢資料庫的時候,耗時久
前端分頁後的介面展示如下:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援碼農教程。