1.添加分页插件

在mybatis-generator-config.xml添加plugin节点:

<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>

2.在maven面板重新运行mybatis-generator:generate自动生成了分页相关的内容。

ProductMapper.java添加了分页查询方法:

List<Product> selectByExampleWithRowbounds(ProductExample example, RowBounds rowBounds);

ProductMapper.xml添加了SelectByExampleWithRowbounds节点:

<select id="selectByExampleWithRowbounds" parameterType="com.data.pojo.ProductExample" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from product
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>

3.测试

@ContextConfiguration(locations = "classpath:spring/applicationContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class ProductDaoTests {
@Resource
ProductMapper productMapper; @Test
public void test_selectByPrimaryKey(){
Product product=productMapper.selectByPrimaryKey(1);
System.out.println(product.getName());
} @Test
public void test_page(){
int id=2;
int pageIndex=1,pageSize=3;
RowBounds rowBounds=new RowBounds((pageIndex-1)*pageSize,pageSize); List<Product> products=productMapper.selectByExampleWithRowbounds(new ProductExample(),rowBounds);
if(products==null){
System.out.println("查询结果为空");
}else {
System.out.println("第"+pageIndex+"页,每页大小"+pageSize);
for(Product p:products){
System.out.println("id="+p.getId()+" name="+p.getName());
}
}
}
}

源码:http://pan.baidu.com/s/1bpJ2pJp

JAVA入门[10]-mybatis分页查询的更多相关文章

  1. JAVA入门[9]-mybatis多表关联查询

    概要 本节要实现的是多表关联查询的简单demo.场景是根据id查询某商品分类信息,并展示该分类下的商品列表. 一.Mysql测试数据 新建表Category(商品分类)和Product(商品),并插入 ...

  2. Mybatis分页查询与动态SQL

    一.Mybatis的分页查询 由于第一二节较为详细讲述了Mybatis的环境搭建,文件配置,SQL编写和Java代码实现,所以接下来的讲述都将只抽取关键代码和mapper文件中的关键sql,详细的流程 ...

  3. spring boot +mybatis分页查询

    这是spring boot集合mybatis的分页查询. pom依赖: <!-- 分页插件 --> <dependency> <groupId>com.github ...

  4. Mybatis 分页查询

    该篇博客记录采用pagehelper分页插件实现Mybatis分页功能 一.依赖 pom.xml <!-- pagehelper --> <dependency> <gr ...

  5. springmvc+spring+mybatis分页查询实例版本1,ver1.0

    无聊做做看看,几乎没有怎么仔细做过这方面的,总是以为很简单,想想就会,实际做起来结合工作经验感觉还是挺有收获的,可以用在自己的项目上 第一版本思路:框架使用ssm,这个无所谓,采用分页语句查询指定页面 ...

  6. 【mybatis】在mybatis分页查询时,主表对关联表 一对多 分页查询怎么实现

    现在有这样一个需求: 1.积分商品分页查询 2.一个积分商品会有多张商品图片在商品图片表  1:n的关系 这样在积分商品分页查询的时候,想要顺便把每个积分商品对应的商品图片信息也带出来 实现如下: 1 ...

  7. 使用Oracle实现的MyBatis分页查询效果

    1.mybatis.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configur ...

  8. mybatis分页查询的万能模板

    分页查询项目里太多了,而这种分页查询,在mybatis里面的配置几乎一模一样,今天就整理一个比较好和实用的模板,供以后直接Ctrl+C <select id="queryMember& ...

  9. mybatis分页查询,SqlServer 2008 查询速度很慢

    一个业务场景,需要进行union查询: 查询速度非常慢,大概要37秒: 直接复制sql在数据库客户端执行,速度很快,由此可知是mybatis的原因,在网上搜索,可以配置fetchSize=" ...

随机推荐

  1. Winform C# 简单实现子窗口显示进度条

    主窗口代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data ...

  2. [转载] ZooKeeper简介

    转载自http://blog.csdn.net/kobejayandy/article/details/17738435 一.      Paxos 基于消息传递通信模型的分布式系统,不可避免的会发生 ...

  3. APP后台API文档管理对接(APP后台框架五)

    这一章还是总结点API管理工具,也大都是组件来的,关于spring-rest-doc 以为很不错,其实还是有点局限性的: 1,每一个接口文档,都要自己手动设置文档的展示: 2,每一个接口就独立一个文档 ...

  4. javascript内存管理(堆和栈)和javascript运行机制

    内存基本概念 内存的生命周期: 1.分配所需的内存 2.内存的读与写 3.不需要时将其释放 所有语言的内存生命周期都基本一致,不同的是最后一步在低级语言中很清晰,但是在像JavaScript 等高级语 ...

  5. Linux笔记(固定USB摄像头硬件端口,绑定前后置摄像头)

    在Android的系统会有前置摄像头和后置摄像头的定义,摄像头分为SOC类型的摄像头和USB这一类的摄像头,接下要分析就是USB摄像头这一类 . 一般在android或者linux系统中分析一个模块, ...

  6. [拓扑排序]Ordering Tasks UVA - 10305

    拓扑排序模版题型: John has n tasks to do.Unfortunately, the tasks are not independent and the execution of o ...

  7. [动态规划]P1004 方格取数

    ---恢复内容开始--- 题目描述 设有N*N的方格图(N<=9),我们将其中的某些方格中填入正整数,而其他的方格中则放 人数字0.如下图所示(见样例): A 0 0 0 0 0 0 0 0 0 ...

  8. python学习笔记 改变字符串中的某一位

    a = ' a = list(a) a[2] = ' news = ''.join(a) print news,a 注意不能使用 news = '' news.join(a) 因为news.join只 ...

  9. Abp后台工作者类使用hangfire

    一.Abp中的后台工作及后台工作者类 请阅读这篇文章 二 .Abp官方实现的缺点 Abp官方实现方式很简单,也很容易上手,但缺点是工作者类依赖了具体的基类(PeriodicBackgroundWork ...

  10. ES7前端异步玩法:async/await理解

    在最新的ES7(ES2017)中提出的前端异步特性:async.await. 什么是async.await? async顾名思义是"异步"的意思,async用于声明一个函数是异步的 ...