mybatis使用逆向工程时分页插件的使用

一、使用场景

查询的表:tb_item

单表查询sqlSELECT * from tb_itemLIMIT 0,10

需要实现分页,使用逆向工程,可以使用mybatis的分页插件。

二、原理 

 

 三、使用方法

插件叫做PageHelper如果你也在用Mybatis,建议尝试该分页插件,这个一定是最方便使用的分页插件。

该插件目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库分页。

使用方法:

第一步:添加jar包

		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>${pagehelper.version}</version>
		</dependency>

第二步:需要在SqlMapConfig.xml,配置一个plugin

                 <!-- 配置分页插件 -->
		<plugins>
			<plugin interceptor="com.github.pagehelper.PageHelper">
				<!-- 指定使用的数据库是什么 -->
				<property name="dialect" value="mysql"/>
			</plugin>
		</plugins>

第三步:在sql语句执行之前,添加一个PageHelper.startPage(page,rows);
第四步:取分页结果。创建一个PageInfo对象需要参数,查询结果返回的list。从PageInfo对象中取分页结果。

四、测试

public class TestPageHelper {
	
	@Test
	public void testPageHelper() throws Exception {
		//1、获得mapper代理对象
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
		TbItemMapper itemMapper = applicationContext.getBean(TbItemMapper.class);
		//2、设置分页
		PageHelper.startPage(1, 30);
		//3、执行查询
		TbItemExample example = new TbItemExample();
		List<TbItem> list = itemMapper.selectByExample(example);
		//4、取分页后结果
		PageInfo<TbItem> pageInfo = new PageInfo<>(list);
		long total = pageInfo.getTotal();
		System.out.println("total:" + total);
		int pages = pageInfo.getPages();
		System.out.println("pages:" + pages);
		int pageSize = pageInfo.getPageSize();
		System.out.println("pageSize:" + pageSize);
		
	}

}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值