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、效果
相关文章推荐
- spring-boot 集成mybatis的分页插件PageHelper和Generator (番外)
- springboot如何集成mybatis的pagehelper分页插件
- [置顶] MyBatis基于Spring-boot集成通用Mapper以及pagehelper分页插件(含源码下载)
- Spring Boot系列六 Spring boot集成mybatis、分页插件pagehelper
- Spring Boot : 集成mybatis、pagehelper插件、开启事务、druid数据源配置、mybatis-generator(十一)
- mybatis 分页插件pagehelper集成及使用
- SpringBoot集成MyBatis的分页插件PageHelper(回头草)
- spring-boot 集成mybatis的分页插件PageHelper和Generator
- spring-boot 集成mybatis的分页插件PageHelper和Generator (番外)
- SpringBoot集成MyBatis的分页插件PageHelper(回头草)
- Mybatis学习(4):Mybatis及PageHelper插件和easyUI实现分页
- Spring + Mybatis 使用 PageHelper 插件分页
- 2017.12.14 Mybatis物理分页插件PageHelper的使用(一)
- Mybatis --- PageHelper分页插件使用
- mybatis 分页插件PageHelper3.7.4试用
- Mybatis 的分页插件PageHelper-4.1.1的使用
- Mybatis的分页插件PageHelper
- SpringBoot中集成mybatis和PageHelper
- Mybatis分页插件PageHelper正确的使用方法(网上有2篇不够科学的文章)
- mybatis的分页插件:mybatis-paginator、mybatis-pageHelper