您的位置:首页 > 其它

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

2017-10-30 19:13 232 查看
    
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 configuration
PUBLIC "-//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层

/**
* 查询商品列表并分页
*/
@Override
public 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")
@ResponseBody
public EUDataGridResult getItemList(Integer page,Integer rows){
EUDataGridResult itemList = itemService.getItemList(page, rows);
return itemList;
}


         5、效果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: