项目模块4--对商品查询实现分页查询功能

简介:

  为查询商品得动作,增加分页查询的功能。

步骤一:引入分页插件

  使用Maven得方式引入分页插件,在pom.xml中添加下面的依赖:

  

1   ·
2       com.github.pagehelper
3       pagehelper
4       5.1.2
5     

步骤二:配置拦截器插件

  在Spring配置文件中的sqlSessionFactory中配置分页插件。

   

 1 class="org.mybatis.spring.SqlSessionFactoryBean">
 2         
 3         
 4             
 5                 class="com.github.pagehelper.PageInterceptor">
 6                     
 7                         
 8                             helperDialect=mysql
 9                         
10                     
11                 
12             
13         

使用plugins属性对分页插件进行配置,

helpDialect=mysql:分页插件使用mysql语言,分页插件会自动检测当前的数据库连接,自动选择合适的分页方式。

步骤三:在代码中使用分页插件

1      PageHelper.startPage(pageCur,10);   //pageCur表示当前页码,10表示每页包含10个数据。
2         GoodsExample.Criteria criteria1 = example.createCriteria();
3         criteria1.andIdBetween(0,1000);
4         allGoods = goodsDao.selectByExample(example);    //PageHelper.startPage方法将对该句起作用。
5         PageInfo pi = new PageInfo<>(allGoods);   //记录分页的相关信息。

PageHelper.startPage(pageCur,10):

1.只有在PageHelper.startPage方法下面的第一个mybatis查询语句会被执行分页。

2.不支持带有for update语句的分页,会抛出运行时异常。

3.不支持嵌套结果映射。

 

  

你可能感兴趣的:(项目模块4--对商品查询实现分页查询功能)