MyBatis-Flex實現(xiàn)分頁查詢的示例代碼
實現(xiàn)flex的分頁查詢需要去維護一個對應(yīng)的獲取數(shù)據(jù)庫總數(shù)的方法,下面會對有無該方法進行一個比較
實現(xiàn)文件主要以下幾個類,注意UserMapper.xml的位置,默認是掃描resources下的mapper包
首先實現(xiàn)UserService和對應(yīng)的實現(xiàn)類,并在內(nèi)部進行對應(yīng)邏輯代碼實現(xiàn)
UserService
public interface IUserService { List<User> getAll(); /** * * @param page 當(dāng)前頁數(shù) * @param pageSize 每頁的總條數(shù) * @return */ Page<User> getPage(int page, int pageSize); }
UserServiceImpl 對應(yīng)的實現(xiàn)類
@Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService { @Resource private UserMapper userMapper; @Override public List<User> getAll() { return this.getMapper().selectAll(); } @Override public Page<User> getPage(int page, int pageSize) { // 查詢數(shù)據(jù)庫的條件 這邊可以根據(jù)自身需求進行對應(yīng)條件添加 // 可以自行查看源碼,這邊不加以闡述 QueryWrapper queryWrapper = QueryWrapper.create(); // selectPage 對應(yīng)的指定mapper的方法 // page.of()內(nèi)的page 和 pageSize就對應(yīng)我們的參數(shù) 即頁數(shù)和行數(shù) // queryWrapper 查詢條件 Page<User> pageInfo = userMapper.xmlPaginate("selectPage", Page.of(page, pageSize) , queryWrapper); return pageInfo; } }
Mapper
@Mapper public interface UserMapper extends BaseMapper<User> { /** * flex的分頁配置 * @return */ long selectPage_COUNT(); /** * 分頁 * @return */ List<User> selectPage(); }
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" > <!--namespace根據(jù)自己需要創(chuàng)建的的mapper的路徑和名稱填寫--> <mapper namespace="org.wyq.studyone.mapper.UserMapper"> <!-- 分頁查詢--> <!-- resultType對應(yīng)的是你的實體類 User的路徑--> <select id="selectPage" resultType="org.wyq.studyone.entity.User"> select * from `user` limit ${pageOffset}, ${pageSize} </select> <!-- flex分頁配置--> <select id="selectPage_COUNT" resultType="long"> select count(*) from `user` </select> </mapper>
selectPage_COUNT該方法沒有的話在執(zhí)行分頁方法時會報錯,具體如下
java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for org.wyq.studyone.mapper.UserMapper.selectPage_COUNT
可以看到,在沒有selectPage_COUNT的情況下,會報找不到這個方法,可我們明明沒有調(diào)用過該函數(shù)啊,其實這個是分頁的內(nèi)部會去進行的一個調(diào)用,對此我們可以看一下xmlPaginate相關(guān)的源碼
Page<User> pageInfo = userMapper.xmlPaginate("selectPage", Page.of(page, pageSize) , queryWrapper);
default <E> Page<E> xmlPaginate(String dataSelectId, Page<E> page, QueryWrapper queryWrapper) { return this.xmlPaginate(dataSelectId, dataSelectId + "_COUNT", page, queryWrapper, (Map)null); }
可以看到,其內(nèi)部的組裝了 dataSelectId + "_COUNT" 這嗎一個變量,這個變量其實就是selectPage + _COUNT 也就是 selectPage_COUNT,所以我們以后要寫分頁代碼的話就需要加個對應(yīng)的 dataSelectId + "_COUNT" 用來實現(xiàn)其分頁內(nèi)部的變量
這個雖然麻煩但一定意義上實現(xiàn)了代碼的更加靈活性
好了,開始代碼測試
controller層:
/* * Copyright 2013-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.wyq.studyone.controller; import com.mybatisflex.core.paginate.Page; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.wyq.studyone.entity.User; import org.wyq.studyone.service.IUserService; import javax.annotation.Resource; import java.util.List; /** * @author <a href="mailto:chenxilzx1@gmail.com" rel="external nofollow" >theonefx</a> */ @Controller public class UserController { @Resource private IUserService userService; @RequestMapping("/hello") @ResponseBody public String hello() { List<User> all = userService.getAll(); return all.toString(); } @RequestMapping("/page") @ResponseBody public String page(int page, int pageSize) { Page<User> pageInfo = userService.getPage(page, pageSize); return pageInfo.toString(); } }
可以看到確實是獲取前面五條數(shù)據(jù)
到此這篇關(guān)于MyBatis-Flex實現(xiàn)分頁查詢的示例代碼的文章就介紹到這了,更多相關(guān)MyBatis-Flex 分頁查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Mybatis-flex整合達夢數(shù)據(jù)庫的實現(xiàn)示例
- Spring Boot整合MyBatis-Flex全過程
- SpringBoot使用MyBatis-Flex實現(xiàn)靈活的數(shù)據(jù)庫訪問
- mybatis-flex實現(xiàn)鏈式操作的示例代碼
- mybatis-flex實現(xiàn)多數(shù)據(jù)源操作
- MyBatis-Flex實現(xiàn)多表聯(lián)查(自動映射)
- Springboot集成Mybatis-Flex的示例詳解
- mybatis-flex與springBoot整合的實現(xiàn)示例
- MyBatis-Flex 邏輯刪除的用法小結(jié)
相關(guān)文章
SpringBoot整合spring-data-jpa的方法
這篇文章主要介紹了SpringBoot整合spring-data-jpa的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06微服務(wù)實戰(zhàn)之怎樣提升springboot服務(wù)吞吐量
這篇文章主要介紹了微服務(wù)實戰(zhàn)之怎樣提升springboot服務(wù)吞吐量方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08SpringBoot配置 Druid 三種方式(包括純配置文件配置)
本文給大家分享在項目中用純 YML(application.yml 或者 application.properties)文件、Java 代碼配置 Bean 和注解三種方式配置 Alibaba Druid 用于監(jiān)控或者查看 SQL 狀況的相關(guān)知識,感興趣的朋友一起看看吧2021-10-10