zoukankan      html  css  js  c++  java
  • MP实战系列(十四)之分页使用

    MyBatis Plus的分页,有插件式的,也有其自带了,插件需要配置,说麻烦也不是特别麻烦,不过觉得现有的MyBatis Plus足以解决,就懒得配置插件了。

    MyBatis Plus的资料不算是太多,与MyBatis相比。所以将可能用到的记录下来。分页及其搜索对于web开发是非常常用的。

    使用MyBatis Plus已经有5个多月,开发的效率,的确提高不少。虽然前面有MyBatis的逆向工程,可以生成单表的增删改查,但是呢?看起来繁杂,用起来不爽,因为还得看一大堆sql和一些queryvo,看起来就不爽,何况用呢。

    MyBatis Plus Github开源地址如下:https://github.com/baomidou/mybatis-plus

    至于maven依赖,前面实战系列贴的都有,这里不再重复贴。

    来个单元测试示例:

    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import com.baomidou.mybatisplus.plugins.Page;
    import com.entity.SysDictData;
    import com.service.SysDictDataService;
    
    
    
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:spring/spring.xml")
    public class JunitTest {
    
    
        
        @Autowired
        private SysDictDataService sysDictDataService;
        
        @Test
        public void testName() throws Exception { 
    
        List<SysDictData> list = sysDictDataService.selectList(null);
        int tatalCount = sysDictDataService.selectCount(null);
        Page<SysDictData> page =new Page<SysDictData>();
        page.setRecords(list);
        page.setCurrent(1);
        page.setLimit(1);
        page.setSize(10);
        page.setTotal(tatalCount);
        System.out.println(page.getRecords())
    }   

    selectList(null)这个方法前面的关于封装方法详解有过说明,这里不再赘述。

    上面的page.setRecords()、 page.setCurrent()、 page.setLimit()、page.setSize()、 page.setTotal()等等方法,相信有一定编程编程经验或者英语水平一般的人都能看出来是什么意思。

    不过这里还是要稍微解释下:

    Records:相当于将集合数据放入其中,作为集合数据装载容器

    Current:当前页

    Limit()相当于 select id,user_name,password from `user` limit 0,5 从索引几开始

    Size():每页显示多少条数据

    Total():数据总数

    至于最后的page.getRecords()就是获取对应的数据。当然如果你要获取当前页或者数据总数可以对象.方法

    例如:

    page.getTotal(); page.getCurrent();

    关于MyBatis Plus封装方法讲解可以参考我的如下博客:

    MP实战系列(五)之封装方法讲解

    MP实战系列(十一)之封装方法详解(续一)

    MP实战系列(十二)之封装方法详解(续二)

    如果对上面的有疑惑,可以留言,说明疑惑,我必尽力详细解答。

    当然了,如果你只是使用MyBatis而不是MyBatis,分页可以使用MyBatis的插件,当然了,如果觉得插件配置麻烦了话,可以参考我的这篇文章实现简单分页:

    layui前端框架之分页

    尽管你不是使用layui,但里面的代码你可以参考。

  • 相关阅读:
    Linux vim 跳转到指定行
    Linux安装lamp过程中出的问题
    centos 7.4源码安装mysql5.5.20+apache 2.4.29+php5.3.28
    centos install vsftpd常见的错误:vsftpd: refusing to run with writable root inside chroot ()错误
    python join函数
    enumerate函数
    实战项目 1:5 行代码爬取国内所有上市公司信息
    python yield用法理解
    python time模块
    yolov5-OSError: [WinError 1455] 页面文件太小,无法完成操作。 Error loading "C:Anaconda3libsite-packages orchlibcaffe2_detectron_ops_gpu.dll"
  • 原文地址:https://www.cnblogs.com/youcong/p/9588365.html
Copyright © 2011-2022 走看看