实现flex的分页查询需要去维护一个对应的获取数据库总数的方法,下面会对有无该方法进行一个比较
实现文件主要以下几个类,注意usermapper.xml的位置,默认是扫描resources下的mapper包
首先实现userservice和对应的实现类,并在内部进行对应逻辑代码实现
userservice
public interface iuserservice { listgetall(); /** * * @param page 当前页数 * @param pagesize 每页的总条数 * @return */ page getpage(int page, int pagesize); }
userserviceimpl 对应的实现类
@service public class userserviceimpl extends serviceimplimplements iuserservice { @resource private usermapper usermapper; @override public list getall() { return this.getmapper().selectall(); } @override public page getpage(int page, int pagesize) { // 查询数据库的条件 这边可以根据自身需求进行对应条件添加 // 可以自行查看源码,这边不加以阐述 querywrapper querywrapper = querywrapper.create(); // selectpage 对应的指定mapper的方法 // page.of()内的page 和 pagesize就对应我们的参数 即页数和行数 // querywrapper 查询条件 page pageinfo = usermapper.xmlpaginate("selectpage", page.of(page, pagesize) , querywrapper); return pageinfo; } }
mapper
@mapper public interface usermapper extends basemapper{ /** * flex的分页配置 * @return */ long selectpage_count(); /** * 分页 * @return */ list selectpage(); }
mapper.xml
selectpage_count该方法没有的话在执行分页方法时会报错,具体如下
java.lang.illegalargumentexception: mapped statements collection does not contain value for org.wyq.studyone.mapper.usermapper.selectpage_count
可以看到,在没有selectpage_count的情况下,会报找不到这个方法,可我们明明没有调用过该函数啊,其实这个是分页的内部会去进行的一个调用,对此我们可以看一下xmlpaginate相关的源码
pagepageinfo = usermapper.xmlpaginate("selectpage", page.of(page, pagesize) , querywrapper);
defaultpage xmlpaginate(string dataselectid, page page, querywrapper querywrapper) { return this.xmlpaginate(dataselectid, dataselectid "_count", page, querywrapper, (map)null); }
可以看到,其内部的组装了 dataselectid "_count" 这吗一个变量,这个变量其实就是selectpage _count 也就是 selectpage_count,所以我们以后要写分页代码的话就需要加个对应的 dataselectid "_count" 用来实现其分页内部的变量
这个虽然麻烦但一定意义上实现了代码的更加灵活性
好了,开始代码测试
controller层:
/* * d88尊龙官网手机app 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 */ @controller public class usercontroller { @resource private iuserservice userservice; @requestmapping("/hello") @responsebody public string hello() { listall = userservice.getall(); return all.tostring(); } @requestmapping("/page") @responsebody public string page(int page, int pagesize) { page pageinfo = userservice.getpage(page, pagesize); return pageinfo.tostring(); } }
可以看到确实是获取前面五条数据