PageHelper的使用(完全舍弃mybatis配置文件)
-
导入坐标
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>x.x.x</version><!-- 这里我用的版本是5.1.2 --> </dependency>
-
配置
-
方式一:完全整合到Spring配置文件中
分析org.mybatis.spring.SqlSessionFactoryBean类,发现其内plugins的类型为一个拦截器数组
private Interceptor[] plugins;
这个org.apache.ibatis.plugin.Interceptor接口的实现类有两个
- com.github.pagehelper.PageInterceptor(分页拦截器)
- com.github.pagehelper.QueryInterceptor(查询拦截器,里面没有实现属性设置方法)
故先要往spring容器中配置一个拦截器的实现类PageInterceptor
applicationContext.xml
<!-- 配置pageHelp的拦截器 --> <bean id="plugin" class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value>helperDialect=mysql</value> </property> </bean>
然后再sqlSessionFactory中注入属性
<!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据库连接 --> <property name="dataSource" ref="dataSource"></property> <!-- 设置映射文件地址 --> <property name="mapperLocations" value="classpath:com/zjweu/mapper/*.xml"></property> <!-- 设置实体类别名 --> <property name="typeAliasesPackage" value="com.zjweu.entity"></property> <!-- 配置蛇形转驼峰 --> <property name="configuration" ref="settings"></property> <!-- 配置分页插件 --> <property name="plugins"> <list> <ref bean="plugin"></ref> </list> </property> </bean>
在需要分页的地方使用,这里我在controller层使用分页
@RequestMapping("/tolist") public ModelAndView tolist(@RequestParam(defaultValue = "1") Integer page , @RequestParam(defaultValue="10") Integer pageSize){ PageHelper.startPage(page,pageSize);//开始分页 List<News> list = newsService.findAll(); PageInfo<News> pageInfo = new PageInfo<News>(list);//封装分页数据 ModelAndView mav = new ModelAndView(); mav.setViewName("newsList"); mav.addObject("pageInfo", pageInfo); return mav; }
页面参考代码
<p>当前${pageInfo.pageNum}页,共${pageInfo.pages}页,总共${pageInfo.total}条记录</p> <a href="tolist?page=${pageInfo.firstPage}">第一页</a> <c:if test="${pageInfo.hasPreviousPage}"> <a href="tolist.do?page=${pageInfo.pageNum-1}">上一页</a> </c:if> <c:if test="${pageInfo.hasNextPage}"> <a href="tolist?page=${pageInfo.pageNum+1}">下一页</a> </c:if> <a href="tolist?page=${pageInfo.lastPage}">最后一页</a>
-
方式二:在将配置放在mybatis的配置文件中
mybatis-config.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> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库--> <property name="helperDialect" value="mysql"/> </plugin> </plugins> </configuration>
然后在spring配置文件中加载mybatis的配置
applicationContext.xml
<!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据库连接 --> <property name="dataSource" ref="dataSource"></property> <!-- 设置映射文件地址 --> <property name="mapperLocations" value="classpath:com/zjweu/mapper/*.xml"></property> <!-- 加载mybatis配置文件 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <!-- 设置实体类别名 --> <property name="typeAliasesPackage" value="com.zjweu.entity"></property> </bean>
使用和第一种方式一样
-