MyBatis-Flex實現(xiàn)分頁查詢的示例代碼
實現(xiàn)flex的分頁查詢需要去維護一個對應(yīng)的獲取數(shù)據(jù)庫總數(shù)的方法,下面會對有無該方法進行一個比較
實現(xiàn)文件主要以下幾個類,注意UserMapper.xml的位置,默認(rèn)是掃描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)鏈?zhǔn)讲僮鞯氖纠a
- 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)文章
Java實現(xiàn)時間與字符串互相轉(zhuǎn)換詳解
這篇文章主要為大家詳細介紹了Java中實現(xiàn)時間與字符串互相轉(zhuǎn)換的相關(guān)方法,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04
使用maven創(chuàng)建web項目的方法步驟(圖文)
本篇文章主要介紹了使用maven創(chuàng)建web項目的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
JetBrains的IDEA編輯器自動配置的JDK版本下載目錄過程
文章說明Java安裝路徑為~/Library/Java/JavaVirtualMachines/ms-17.0.16,并分享配置經(jīng)驗,建議用戶根據(jù)實際需求調(diào)整環(huán)境變量,以確保Java版本正確運行2025-08-08
微服務(wù)通過Feign調(diào)用進行密碼安全認(rèn)證操作
這篇文章主要介紹了微服務(wù)通過Feign調(diào)用進行密碼安全認(rèn)證操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06

