您好,登錄后才能下訂單哦!
這篇文章給大家介紹怎么在SpringCloud中使用MyBatis實現分頁處理,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
一、直接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+;在沒有條件查詢數據庫的時候,耗時久
前端分頁后的界面展示如下:
關于怎么在SpringCloud中使用MyBatis實現分頁處理就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。