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. ContextLoaderListener - 运行原理

    基本概念: servletContext:http://blog.csdn.net/yjw757174266/article/details/45072975 1.  使用ContextLoaderL ...

  2. WPF中DataGrid垂直滚动条滚动后导致每行CheckBox选择错乱

    问题: WPF的DataGrid中出现选取或者多选以及单选的时候,出现滚动条的时候,如果发生了滚动,默认情况下就会出现已经选择的CheckBox错乱.这样的原因何在? 解决方案: 经过查阅资料,了解到 ...

  3. 线性回归,附tensorflow实现

    本文同步自:https://zhuanlan.zhihu.com/p/30738405 本文旨在通过介绍线性回归来引出一些基本概念:h(x),J(θ),梯度下降法 有一组数据: x=[1,2,3,4, ...

  4. Hue 之 SparkSql interpreters的配置及使用

    1.环境说明: HDP 2.4 V3 sandbox hue 4.0.0 2.hue 4.0.0 编译及安装 地址:https://github.com/cloudera/hue/releases/t ...

  5. java 之 原型模式(大话设计模式)

    原型模式,在笔者理解看来就是克隆,当我们在创建第一个对象时,已经给对象赋值完毕,此时我们需要一个当前对象的副本,如果没有原型模式,我们会再次创建一个对象,然后后二次赋值,保证两个对象完全一致, 这样我 ...

  6. C#学习笔记随笔(1)----C#中static关键字的作用

    静态分配的,有两种情况: 1. 用在类里的属性.方法前面,这样的静态属性与方法不需要创建实例就能访问, 通过类名或对象名都能访问它,静态属性.方法只有“一份”:即如果一个类新建有N个 对象,这N 个对 ...

  7. 开源API测试工具 Hitchhiker v0.5更新 - 完善细节

    Hitchhiker 是一款开源的支持多人协作的 Restful Api 测试工具,支持Schedule, 数据对比,压力测试,支持上传脚本定制请求,可以轻松部署到本地,和你的team成员一起管理Ap ...

  8. [搜索]ElasticSearch Java Api(一) -添加数据创建索引

    转载:http://blog.csdn.net/napoay/article/details/51707023 ElasticSearch JAVA API官网文档:https://www.elast ...

  9. spring cloud feign不支持@RequestBody+ RequestMethod.GET,报错

    1.问题梳理: 异常:org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not ...

  10. 用sort()按小到大排序的方法:

    例子:function compare(value1,value2){ if(value1<value2){ return -1; }else if(value1==value2){ retur ...