pagehelper分页插件的用法
插件简介
PageHelper是一款好用的开源免费的Mybatis第三方物理分页插件,原本以为分页插件,应该是很简单的,然而PageHelper比我想象的要复杂许多,它做的很强大,也很彻底,强大到使用者可能并不需要这么多功能,但是里面给我们封装了很多很多的变量,便于我们的开发,从而更加简单的加快了我们使用的效率
分页插件的使用
1.在pom.xml中导入相对于的坐标
pagehelper会依赖jsqlparser,在maven工程中也就是导入其坐标
复制代码
1
2
3
4
5
6
7
8
9
10
11
12<!--后端分页插件的使用的相关的jar包--> <dependency> <groupId>com.github.jsqlparser</groupId> <artifactId>jsqlparser</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency>
2.在mybatis的配置文件中配置方言
显然是指定要使用数据库的类型,便于数据的读取功能
复制代码
1
2
3
4
5
6
7
8<!--后端分页的插件,使用其插件的用法,能够大大的减少其关于分页的写法--> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!--配置连接的相关的数据库的方言--> <property name="helperDialect" value="mysql"/> </plugin> </plugins>
3.在dao层配置查询所有的接口(这里以多表查询的注解开发为例)
在dao层配置其分页查询的条件,这里的配置是多表查询的注解开发,故细节不再讲解,会在另一篇博客中详细给大家讲解到的
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15//分页查询的接口 @Select("select * from classes") @Results(id = "classesMap",value = { @Result(id = true,column = "id",property = "id"), @Result(column = "name",property = "name"), @Result(column = "type",property = "type"), @Result(column = "status",property = "status"), @Result(column = "majid",property = "majid"), @Result(column = "pre1",property = "pre1"), @Result(column = "pre2",property = "pre2"), @Result(column = "majid",property = "major", one = @One(select = "cn.ujiuye.dao.MajorDao.findById",fetchType = FetchType.EAGER)), }) List<Classes> getByPage();
4.在serice层配置其相对的参数的配置
- 配置分页的参数
- 调用查询所有的数据的接口
- 通过PageInfo对第二步中的sql语句的分页的关键字的以及参数(返回页码,每页的参数,总条数,当前的总页数,当前的页的集合)
复制代码
1
2
3
4
5
6
7
8/** * 利用后端分页实现的数据的分页展示的信息 * @param pageNO * @return */ public PageInfo<Classes> getClist(int pageNO);
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22/** * 通过后端的分页插件查询全部的数据的信息 * 1.设置分页参数 * 2.调用查询所有的数据的接口 * 3.通过PageInfo对第二步的sql语句拼接的关键字和分页的参数,返回PageInfo * (页码,每页的条数,总页数,当前的页面的集合等) * @param pageNO * @return */ @Override public PageInfo<Classes> getClist(int pageNO) { //设置分页的参数 PageHelper.startPage(pageNO,4); //调用查询所有的数据的接口 List<Classes> clist = classesDao.getByPage(); //通过PageInfo对第二步中的Sql语句拼接的分页的关键字以及分页参数, //返回PageInfo(页码,每页条数,总条数,当前页的集合等) PageInfo<Classes> info = new PageInfo<Classes>(clist); return info; }
5.在controller层配置其前后端的页面的交互
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14/** * 利用后端框架来实现对从后台数据的读取工作 * 初始化默认是第一页 * 该类对象中有所需的各种业务 * @return */ @RequestMapping public String getList(Model model, @RequestParam(defaultValue = "1")int pageNO){ //从后台获取参数,并将其结果存入到域对象中,转发到相对应的页面上 PageInfo<Classes> info = classesService.getClist(pageNO); model.addAttribute("info",info); return "class/cla-list"; }
6.在jsp页面其进行展示
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<c:forEach items="${info.list}" var="c"> <tr class="text-c va-m"> <td><input type="checkbox" name="id" value="${c.id}"></td> <td class="text-l">${c.id}</td> <td class="text-l">${c.name}</td> <td class="text-l">${c.type}</td> <td class="text-l">${c.major.name}</td> <td> <a href="" class="btn btn-danger radius">删除</a> <a class="btn btn-primary radius" onClick="maj_edit('学生编辑','cla/showadd','${c.id}')" href="javascript:;" title="编辑"><i class="Hui-iconfont">编辑</i></a> <a href="" class="btn btn-danger radius">排课</a> </td> </tr> </c:forEach>
运行结果
源码分享
通过对源码的分析更能让我们明确自己进步的过程和目标,奠定更好的前进动力
最后
以上就是缓慢衬衫最近收集整理的关于pagehelper分页插件的用法的全部内容,更多相关pagehelper分页插件内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复