springMvc+Mybatis下使用pagehelper 插件
2017-11-06 14:16
513 查看
废话不多说,直接说流程,我的项目是Maven项目。先进行配置
1、pom.xml配置,添加jar
2、applicationContext.xml和mybatis-config.xml的配置
applicationContext.xml配置:
mybatis-config.xml配置:
3.UserController的写法:
4、showUser.jsp的写法:
好了,service,serviceImpl,和mapper的都跟一般的Mybatis写法都一样的。
效果如下:
1、pom.xml配置,添加jar
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>3.7.5</version> </dependency>
2、applicationContext.xml和mybatis-config.xml的配置
applicationContext.xml配置:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描mappers.xml文件 --> <property name="mapperLocations" value="classpath:com/web/mapper/*.xml"></property> <!-- mybatis配置文件 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean>
mybatis-config.xml配置:
<!-- Mybatis分页配置 --> <plugins> <!-- com.github.pagehelper为PageHelper类所在包名 --> <plugin interceptor="com.github.pagehelper.PageHelper"> <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库--> <property name="dialect" value="mysql"/> <!-- 该参数默认为false --> <!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 --> <!-- 和startPage中的pageNum效果一样--> <property name="offsetAsPageNum" value="true"/> <!-- 该参数默认为false --> <!-- 设置为true时,使用RowBounds分页会进行count查询 --> <property name="rowBoundsWithCount" value="true"/> <!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 --> <!-- (相当于没有执行分页查询,但是返回结果仍然是Page类型)--> <property name="pageSizeZero" value="true"/> <!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 --> <!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 --> <!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 --> <property name="reasonable" value="true"/> <!-- 3.5.0版本可用 - 为了支持startPage(Object params)方法 --> <!-- 增加了一个`params`参数来配置参数映射,用于从Map或ServletRequest中取值 --> <!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,不配置映射的用默认值 --> <!-- 不理解该含义的前提下,不要随便复制该配置 --> <property name="params" value="pageNum=start;pageSize=limit;"/> </plugin> </plugins>
3.UserController的写法:
/** * 分页查询方法 * * @author wangguan, * @date 2017年11月3日 下午2:55:02, * @version argType */ @RequestMapping("/findByPage") public String findByPage(@RequestParam(required=true,defaultValue="1")Integer page,User user,HttpServletRequest request,Model model,Map<String,Object> map){ PageHelper.startPage(page, 3); List<User> userList = userService.findByPage(""); PageInfo<User> p=new PageInfo<User>(userList); model.addAttribute("page", p); model.addAttribute("userList",userList); return "showUser"; }
4、showUser.jsp的写法:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ include file="/common/jsp/taglibs.jsp"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript" src="common/js/jquery.min.js"></script> <title>Insert title here</title> </head> <body> <center> <table width="200" border="1"> <tr> <th scope="col">ID</th> <th scope="col">姓名</th> <th scope="col">密码</th> </tr> <c:forEach items="${userList}" var="list" > <tr> <td>${list.id}</td> <td>${list.userName}</td> <td>${list.password}</td> </tr> </c:forEach> </table> <p>一共${page.pages}页</p> <a href="findByPage?page=${page.firstPage}">第一页</a> <a href="findByPage?page=${page.nextPage}">下一页</a> <a href="findByPage?page=${page.prePage}">上一页</a> <a href="findByPage?page=${page.lastPage}">最后页</a> </center> </body> </html>
好了,service,serviceImpl,和mapper的都跟一般的Mybatis写法都一样的。
效果如下:
相关文章推荐
- easyui 分页 + mybatis 分页插件 PageHelper 完成分页显示功能(SpringMVC + mybatis 框架)
- Maven+Spring + Mybatis 使用 PageHelper 插件分页
- Spring + Mybatis 使用 PageHelper 插件分页
- Spring + Mybatis 使用 PageHelper 插件分页
- springboot使用之二:整合mybatis(xml方式)并添加PageHelper插件
- Spring Boot系列教程十一: Mybatis使用分页插件PageHelper
- Spring + Mybatis 使用 PageHelper 插件分页
- 记录问题:springboot中使用Mybatis-pagehelper分页插件遇到的版本兼容问题
- Spring + Mybatis 使用 PageHelper 插件分页
- Spring + Mybatis 使用 PageHelper 插件分页
- Spring + Mybatis 使用 PageHelper 插件分页
- Spring + Mybatis 使用 PageHelper 插件分页
- MyBatis分页插件PageHelper使用练习
- Mybatis的分页插件PageHelper的使用方法
- 【MyBatis】MyBatis分页插件PageHelper的使用
- 【MyBatis】MyBatis分页插件PageHelper的使用
- Mybatis 的分页插件PageHelper-4.1.1的使用
- Mybatis分页插件PageHelper正确的使用方法(网上有2篇不够科学的文章)
- Mybatis 的分页插件PageHelper-4.1.1的使用
- Mybatis 使用 PageHelper 插件分页