mybatis框架中分页的实现
2.分页的实现?
分页的时候考虑的问题:
分页的大小,分页的索引。
比如:分页的大小为10,分页的起始索引为1(索引从1开始)
第一页:1到10. 起始行号: (页的索引-1)*分页大小+1
结束行号: 页的索引*分页大小
实现方案:
1)比如针对UserInfo的实体对象做分页处理,那么先创建一个分页的实体类,该类继承自UserInfo。
package com.gxa.bj.model;
public class UserInfoPage extends UserInfo{
// 分页的索引
private int pageIndex;
// 分页的大小
private int pageSize;
// 分页的起始行
private int startNum;
// 分页的结束行
private int endNum;
public int getPageIndex() {
return pageIndex;
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getStartNum() {
return startNum;
}
public void setStartNum(int startNum) {
this.startNum = startNum;
}
public int getEndNum() {
return endNum;
}
public void setEndNum(int endNum) {
this.endNum = endNum;
}
}
2)在接口中定义一个方法,
public List<UserInfo> getListByPage(UserInfoPage upage);
3)在Mapper文件中编写SQL语句:
<select id="getListByPage" parameterType="com.gxa.bj.model.UserInfoPage" resultType="com.gxa.bj.model.UserInfo">
Select u.*
From (Select rownum as num, userinfo.*
from userinfo
<where>
<if test="userName!=null">
And userName like #{userName}
</if>
<if test="userId>0">
And userId =#{userId}
</if>
<if test="userPwd!=null and userPwd !='' ">
And userPwd like #{userPwd}
</if>
</where>
) u Where u.num between #{startNum} and #{endNum}
</select>
4)编写相应的测试代码:
UserInfoPage u= new UserInfoPage();
//
u.setUserName("zhangsan");
u.setPageIndex(2);
u.setPageSize(2);
int startNum = (u.getPageIndex()-1)*u.getPageSize()+1;
int endNum = u.getPageIndex()*u.getPageSize();
u.setStartNum(startNum);
u.setEndNum(endNum);
List<UserInfo> list = userMapper.getListByPage(u);
for(UserInfo t :list){
System.out.println("id:"+t.getUserId());
}
mybatis框架中分页的实现的更多相关文章
- Mybatis框架中实现双向一对多关系映射
学习过Hibernate框架的伙伴们很容易就能简单的配置各种映射关系(Hibernate框架的映射关系在我的blogs中也有详细的讲解),但是在Mybatis框架中我们又如何去实现 一对多的关系映射呢 ...
- 详解Java的MyBatis框架中SQL语句映射部分的编写
这篇文章主要介绍了Java的MyBatis框架中SQL语句映射部分的编写,文中分为resultMap和增删查改实现两个部分来讲解,需要的朋友可以参考下 1.resultMap SQL 映射XML 文件 ...
- drf框架中分页组件
drf框架中分页组件 普通分页(最常用) 自定制分页类 pagination.py from rest_framework.pagination import PageNumberPagination ...
- mybatis框架中XxxxMaper.xml的文件
我们知道在mybatis框架中,config.xml中会关联到许多的XxxxMapper的xml文件,这些文件又对应着一个个的接口,来观察下这些xml文件 从以下这个文件为例子: <?xml v ...
- DRF框架中分页功能接口
目录 DRF框架中分页功能接口 DRF框架中分页功能接口 一.在框架中提供来三个类来实现分页功能,PageNumberPagination.LimitOffsetPagination.CursorPa ...
- Spring+MyBatis框架中sql语句的书写,数据集的传递以及多表关联查询
在很多Java EE项目中,Spring+MyBatis框架经常被用到,项目搭建在这里不再赘述,现在要将的是如何在项目中书写,增删改查的语句,如何操作数据库,以及后台如何获取数据,如何进行关联查询,以 ...
- 【mybatis】使用mybatis框架中踩过的坑
好久没来记录一下自己的学习情况,最近都在学框架,今天来记录一下关于mybatis框架的学习过程中碰过的一些问题: 以下内容可能稍微有点凌乱,因为是把之前遇到过的错误或异常都集中一起了,不过我已经把问题 ...
- SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释(转)
原文:https://blog.csdn.net/yijiemamin/article/details/51156189# 这几天一直在整合SSM框架,虽然网上有很多已经整合好的,但是对于里面的配置文 ...
- 0927-转载:SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释
这篇文章暂时只对框架中所要用到的配置文件进行解释说明,而且是针对注解形式的,框架运转的具体流程过两天再进行总结. spring+springmvc+mybatis框架中用到了三个XML配置文件:web ...
随机推荐
- koa2+koa-views示例
app.js var Koa = require('koa') var fs = require('fs') var path = require('path') var koaStaticPlus ...
- HDOJ(2438)几何里的三分
Turn the corner http://acm.hdu.edu.cn/showproblem.php?pid=2438 题目:一辆车能否在一个路口拐弯,看图就很明白啦. 算法:见下图,只要求出图 ...
- toolkit:Accordion DataTemplate ListBox TextBlock Interaction.Triggers
困扰好几个小时的问题终于解决了,本人系菜鸟,使用MVVM设计模式,绑定DataTemplate的Command,需要使用 DataContent的资源,否则无法触发ICommand ClickChil ...
- VC++ 迭代器 iterator, const_iterator, const iterator
迭代器 iterator, const_iterator, const iterator 迭代器iterator的作用类似于指针. (1)iterator只有针对制定<类型>的容器才有效. ...
- CRM域用户误删恢复
记录一下: 不小心将CRM用户在域中删除了(CRM中未删除),直接新建一个同样账号的域用户然后尝试在CRM中登录报“invalid user”错误,一番检查发现从2011版本开始CRM中不单记录了用户 ...
- C#与Java的比较
C#与Java的比较 写完后得知维基百科里有更加全面得多的比较: http://en.wikipedia.org/wiki/Comparison_of_C_Sharp_and_Java NET(C#) ...
- gcc 使用 stdio.h
9876543210z@z:~/funnyC++$ cat main.cpp #include <stdio.h> int main() { ; ) { printf("%d&q ...
- 个人关于React的一些理解
##React背景 React是当前前端最火的框架,它的理念思想及构建方法比AngularJS更适合做webApp. 它是由facebook团队研发并开源到社区,所以它有很强大的技术背景,而且它的架构 ...
- SpringRMI远程方法调用
Spring为各种远程访问技术的集成提供了工具类. 该小段引用自 http://www.open-open.com/lib/view/open1408957290478.html Spring远程支持 ...
- ZOJ 1442 Dinner Is Ready 容斥原理 + java大数
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=442 求解 x1 + x2 + x3 + .... + xn = m 其中xi属 ...