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. jquery多种方式实现输入框input输入时的onput,onpropertychange,onchange触发事件及区别

    有关inputs输入内容的事件监听,一般我们会想到下面几个关键词:onput,onpropertychange,onchange onput与onchange的一个区分 onput:该事件在 < ...

  2. C#内部关于绑定事件Event的线程安全

    private EventHandler _FieldsChanged;public event EventHandler FieldsChanged{    add    {        Even ...

  3. MyBatis学习笔记1--初识MyBatis

    我也是初学者,写博客只是想把自己的整个思路整理一下,有不对或者不好的地方,请大家多多指正. 1.MyBatis简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射. ...

  4. css基础-选择器

    CSS选择符(选择器) 一.各类选择器 选择符表示要定义样式的对象,可以是元素本身,也可以是一类元素或者制定名称的元素. 常用的选择符有十种左右 类型选择符,id选择符,class选择符,通配符,群组 ...

  5. shell ping一个IP,延时大于5,输出延时大于5s,打印输出

    # ping一个IP,延时大于5,输出延时大于5s,打印输出 #!/bin/bash ip=$* echo $ip num=`ping  -c 10 ${ip}|grep icmp_seq|awk ' ...

  6. ubuntu16.04安装交叉编译链

    我使用的是arm-linux-gcc 4.3.2版本,其他版本类似,附上下载链接: https://pan.baidu.com/s/1geUOfab 密码: frzy 首先我的安装包是tar.bz2的 ...

  7. Struts2-052验证脚本

    下面分享一下Struts2-052验证的python脚本 #-*- coding:utf-8 -*- import requests url_list_path ="/home/d0ll4r ...

  8. Centos下mongodb的安装与配置

    安装MongoDB的方法有很多种,可以源代码安装,在Centos也可以用yum源安装的方法. 1.准备工作 运行yum命令查看MongoDB的包信息 yum info mongodb-org (提示没 ...

  9. 《java.util.concurrent 包源码阅读》15 线程池系列之ScheduledThreadPoolExecutor 第二部分

    这篇文章主要说说DelayedWorkQueue. 在ScheduledThreadPoolExecutor使用DelayedWorkQueue来存放要执行的任务,因为这些任务是带有延迟的,而每次执行 ...

  10. RAC(ReactiveCocoa)使用方法(一)

    RAC(ReactiveCocoa)使用方法(一) RAC(ReactiveCocoa)使用方法(二) 什么是RAC? 最近回顾了一下ReactiveCocoa的方法,也看了一些人的文章,现写篇文章总 ...