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

简介:

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

步骤一:引入分页插件

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

  

1   ·<dependency>
2       <groupId>com.github.pagehelper</groupId>
3       <artifactId>pagehelper</artifactId>
4       <version>5.1.2</version>
5     </dependency>

步骤二:配置拦截器插件

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

   

 1 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 2         <!--配置分页插件-->
 3         <property name="plugins">
 4             <array>
 5                 <bean class="com.github.pagehelper.PageInterceptor">
 6                     <property name="properties">
 7                         <value>
 8                             helperDialect=mysql
 9                         </value>
10                     </property>
11                 </bean>
12             </array>
13         </property>

使用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<Goods> pi = new PageInfo<>(allGoods);   //记录分页的相关信息。

PageHelper.startPage(pageCur,10):

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

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

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

  

猜你喜欢

转载自www.cnblogs.com/deijiawoyu/p/12544303.html