PageHelper5.0.0分页插件与mybatis的集成

来源:互联网 发布:网易云音乐推荐算法 编辑:程序博客网 时间:2024/06/05 05:11

     PageHelper5.0.0分页插件与mybatis的集成

               PageHelper作为使用最方便的分页插件,因为操作简单,深受广大编程爱好者的喜爱,那么它是怎么与mybatis进行集成的呢?
              1、maven集成      

    <!-- pagehelper分页插件 -->          <dependency>              <groupId>com.github.pagehelper</groupId>              <artifactId>pagehelper</artifactId>          <version>5.0.0</version>      </dependency>  

            2、mabatis集成,

           (1) 第一种写法,spring-mybatis.xml

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">              <!-- 数据库连接池 -->              <property name="dataSource" ref="dataSource" />              <!-- 加载mybatis的全局配置文件 -->              <!-- <property name="configLocation" value="classpath:sqlMapConfig.xml"                   /> -->                  <!--  为com.red.packet.po包下的所有实体类配置别名(mybatis 3.2.8以上版本) -->              <property name="typeAliasesPackage" value="com.red.packet.po" />              <property name="plugins">                  <array>                      <bean class="com.github.pagehelper.PageInterceptor">                          <property name="properties">                              <value>                                  helperDialect=mysql                              </value>                          </property>                      </bean>                  </array>              </property>          </bean>  
         (2)第二种写法 spring-mybatis.xml加SqlMapConfig.xml

           1、spring-mybatis.xml

<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 数据库连接池 --><property name="dataSource" ref="dataSource" /><!-- 加载mybatis的全局配置文件 --><property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" /></bean>

           2、 SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><plugins>    <!-- com.github.pagehelper为PageHelper类所在包名 -->    <plugin interceptor="com.github.pagehelper.PageInterceptor">        <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->                <property name="helperDialect" value="mysql"/>    </plugin></plugins></configuration>

         3、service层

/** * 查询商品列表并分页 */@Overridepublic EUDataGridResult getItemList(int page, int rows){TbItemExample example = new TbItemExample();//设置分页PageHelper.startPage(page, rows);//取到商品对象集合List<TbItem> list = itemMapper.selectByExample(example);//取分页信息PageInfo<TbItem> pageInfo = new PageInfo<>(list);long total = pageInfo.getTotal();//与eassyui进行集成EUDataGridResult result = new EUDataGridResult();result.setRows(list);result.setTotal(total);return result;}

        4、controller层,针对的是easyui表格,因为必须返回数据格式为 {total:2,rows:[{id:1,name,张三},{id:2,name,李四}]}

/** * 按照easyui datagrid格式返回数据进行分页 * @param page * @param rows * @return */@RequestMapping("/item/list")@ResponseBodypublic EUDataGridResult getItemList(Integer page,Integer rows){EUDataGridResult itemList = itemService.getItemList(page, rows);return itemList;}

         5、效果


阅读全文
1 0