MyBatis-Flex实现分页查询的示例代码

来自:互联网
时间:2024-10-14
阅读:

实现flex的分页查询需要去维护一个对应的获取数据库总数的方法,下面会对有无该方法进行一个比较

实现文件主要以下几个类,注意UserMapper.xml的位置,默认是扫描resources下的mapper包

MyBatis-Flex实现分页查询的示例代码

首先实现UserService和对应的实现类,并在内部进行对应逻辑代码实现

UserService

public interface IUserService {
    List<User> getAll();

    /**
     * 
     * @param page 当前页数
     * @param pageSize  每页的总条数
     * @return
     */
    Page<User> getPage(int page, int pageSize);
}

UserServiceImpl 对应的实现类

@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) {
        // 查询数据库的条件 这边可以根据自身需求进行对应条件添加
        // 可以自行查看源码,这边不加以阐述
        QueryWrapper queryWrapper = QueryWrapper.create();
        // selectPage 对应的指定mapper的方法
        // page.of()内的page 和 pageSize就对应我们的参数 即页数和行数
        // 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根据自己需要创建的的mapper的路径和名称填写-->
<mapper namespace="org.wyq.studyone.mapper.UserMapper">
    <!--    分页查询-->
    <!--    resultType对应的是你的实体类 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该方法没有的话在执行分页方法时会报错,具体如下

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

 可以看到,在没有selectPage_COUNT的情况下,会报找不到这个方法,可我们明明没有调用过该函数啊,其实这个是分页的内部会去进行的一个调用,对此我们可以看一下xmlPaginate相关的源码

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);
}

可以看到,其内部的组装了 dataSelectId + "_COUNT" 这吗一个变量,这个变量其实就是selectPage + _COUNT 也就是 selectPage_COUNT,所以我们以后要写分页代码的话就需要加个对应的 dataSelectId + "_COUNT" 用来实现其分页内部的变量

这个虽然麻烦但一定意义上实现了代码的更加灵活性

好了,开始代码测试

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();
    }

}

MyBatis-Flex实现分页查询的示例代码

MyBatis-Flex实现分页查询的示例代码

可以看到确实是获取前面五条数据

返回顶部
顶部