最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MyBatis-Flex實(shí)現(xiàn)分頁查詢的示例代碼

 更新時(shí)間:2024年10月14日 11:10:16   作者:玉米淀粉  
在MyBatis-Flex中實(shí)現(xiàn)分頁查詢時(shí),需要注意維護(hù)一個(gè)獲取數(shù)據(jù)庫總數(shù)的方法,詳細(xì)介紹了UserService、UserServiceImpl類以及Mapper.xml配置,感興趣的可以了解一下

實(shí)現(xiàn)flex的分頁查詢需要去維護(hù)一個(gè)對應(yīng)的獲取數(shù)據(jù)庫總數(shù)的方法,下面會對有無該方法進(jìn)行一個(gè)比較

實(shí)現(xiàn)文件主要以下幾個(gè)類,注意UserMapper.xml的位置,默認(rèn)是掃描resources下的mapper包

首先實(shí)現(xiàn)UserService和對應(yīng)的實(shí)現(xiàn)類,并在內(nèi)部進(jìn)行對應(yīng)邏輯代碼實(shí)現(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)的實(shí)現(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ù)自身需求進(jìn)行對應(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)的是你的實(shí)體類 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í)行分頁方法時(shí)會報(bào)錯(cuò),具體如下

java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for org.wyq.studyone.mapper.UserMapper.selectPage_COUNT

 可以看到,在沒有selectPage_COUNT的情況下,會報(bào)找不到這個(gè)方法,可我們明明沒有調(diào)用過該函數(shù)啊,其實(shí)這個(gè)是分頁的內(nèi)部會去進(jìn)行的一個(gè)調(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" 這嗎一個(gè)變量,這個(gè)變量其實(shí)就是selectPage + _COUNT 也就是 selectPage_COUNT,所以我們以后要寫分頁代碼的話就需要加個(gè)對應(yīng)的 dataSelectId + "_COUNT" 用來實(shí)現(xiàn)其分頁內(nèi)部的變量

這個(gè)雖然麻煩但一定意義上實(shí)現(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í)是獲取前面五條數(shù)據(jù)

到此這篇關(guān)于MyBatis-Flex實(shí)現(xiàn)分頁查詢的示例代碼的文章就介紹到這了,更多相關(guān)MyBatis-Flex 分頁查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

罗定市| 蛟河市| 连州市| 麻城市| 原平市| 天峨县| 白河县| 肥西县| 蓬莱市| 宜兰县| 涡阳县| 新泰市| 石屏县| 四川省| 木兰县| 青神县| 张家港市| 兴仁县| 华容县| 呼伦贝尔市| 乡宁县| 凤翔县| 武清区| 章丘市| 郸城县| 乐亭县| 罗城| 莒南县| 山阳县| 昌都县| 德庆县| 南昌市| 葫芦岛市| 金湖县| 东宁县| 黄骅市| 五家渠市| 那曲县| 襄垣县| 玉山县| 周宁县|