spring+PageHelper+mybatis(三)
来源:互联网 发布:陕西大数据集团怎么样 编辑:程序博客网 时间:2024/06/10 15:18
利用PageHelper可以很方便地对数据库查询结果进行分页
pom.xml加入对PageHelper的引用
<!--page--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>3.5.0</version> </dependency>
在spring-db.xml中加入pagehelper插件的引用
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:mapper/*.xml"/> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageHelper"> <property name="properties"> <value> dialect=mysql offsetAsPageNum=true<!-- 该参数默认为false,设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用,和startPage中的pageNum效果一样 --> rowBoundsWithCount=true<!-- 默认为false,设置为true时,使用RowBounds分页会进行count查询 --> pageSizeZero=true<!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果,(相当于没有执行分页查询,但是返回结果仍然是Page类型) --> reasonable=true<!-- 3.3.0版本可用,分页参数合理化,默认false禁用,启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页,禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 --> </value> </property> </bean> </array> </property> </bean>对于前台传来的页面请求,我们需要将其中的分页信息转换成我们需要的pageNum和pageSize
我们可以这么做,新建一个PageVo类,用来记录页码,每页条数相关信息,所有的model都要继承这个类
PageVo.java(dojo.gird传来的分页信息为start,count,我们可以这么转化下)
public class PageVo { /** 当前页码.**/ private int pageNum; /** 每页条数.**/ private int pageSize; /** 起始标志.**/ private int startRow; /**对接dojo的grid**/ private int start; private int count; /** * @return the pageNum */ public int getPageNum() { return count>0?start/count+1:1; } /** * @param pageNum the pageNum to set */ public void setPageNum(int pageNum) { this.pageNum = pageNum; } /** * @return the pageSize */ public int getPageSize() { return pageSize; } /** * @param pageSize the pageSize to set */ public void setPageSize(int pageSize) { this.pageSize = pageSize; } /** * @param startRow the startRow to set */ public void setStartRow(int startRow) { this.startRow = startRow; } /** * @return the startRow */ public int getStartRow() { return this.pageNum > 0 ? (this.pageNum - 1) * this.pageSize : 0; } /** * @return the endRow */ public int getEndRow() { return this.startRow + this.pageSize * (this.pageNum > 0 ? 1 : 0); } public int getStart() { return start; } public void setStart(int start) {this.start = start; } public int getCount() { return count; } public void setCount(int count) { this.pageSize=count; this.count = count; }}User需要继承PageVo
public class User extends PageVo{相应Controller测试接口
@ResponseBody @RequestMapping(value="/selectUser",method = RequestMethod.POST) public Map<String,Object> selectUser(@RequestBody User user){ Map<String, Object> resultMap = new HashMap<String, Object>(); try { Map<String,Object> paramMap= BeanUtil.transBean2Map(user); PageHelper.startPage(10, 3); Page<User> page = userService.selectUserByCon(paramMap); PageUtil.makePage(resultMap, page.getResult(), page.getPageNum(), page.getPageSize(), page.getTotal()); } catch (Exception e) { PageUtil.makePage(resultMap, null, 0, 0, 0); resultMap.put("msg", "查询用户信息失败"); resultMap.put("code", 1); } return resultMap; }BeanUtil是一个class和map互转的工具类,可见
http://blog.csdn.net/bornonew/article/details/54575550
PageUtil是把结果转成前台需要的工具类。
UserDao
Page<User> selectUserByCon(Map<String,Object> paraMap);UserMapper.xml
<select id="selectUserByCon" resultMap="BaseResultMap" parameterType="java.util.Map"> select <include refid="Base_Column_List"/> from user <where> 1=1 <if test="id != null and id != 0"> and id = #{id} </if> <if test="username != null and username != ''"> and username = #{username} </if> <if test="status != -1"> and status = #{status} </if> </where> </select>Test
@Test public void testSelectUser() throws Exception { User user=new User(); user.setPageNum(2); user.setPageSize(4); userController.selectUser(user); }修改PageNum和PageSize会有不同的结果返回
0 0
- spring+PageHelper+mybatis(三)
- Spring-Mybatis分页--PageHelper
- (三)spring boot + mybatis +pagehelper 添加Dao工具类
- Spring Boot+Mybatis+Pagehelper分页
- spring+Mybatis+ PageHelper实现分页
- Spring Boot+Mybatis+Pagehelper分页
- spring利用mybatis PageHelper分页
- Mybatis三剑客(mybatis-generator、mybatis-plugin、mybatis-pagehelper)介绍、配置及破解
- Mybatis三剑客(mybatis-generator、mybatis-plugin、mybatis-pagehelper)介绍、配置及破解
- 关于Spring+mybatis+PageHelper分页插件PageHelper的使用策略
- Spring+mybatis+PageHelper分页插件PageHelper的使用
- Spring + Mybatis 使用 PageHelper 插件分页
- spring+mybatis利用pageHelper分页插件分页
- Maven+Spring + Mybatis 使用 PageHelper 插件分页
- Spring + Mybatis 使用 PageHelper 插件分页
- Spring + Mybatis 使用 PageHelper 插件分页
- Spring + Mybatis 使用 PageHelper 插件分页
- Spring中集成Mybatis分页插件PageHelper
- android developer tiny share-20170217
- OkhttpUtils的Cookie存储问题
- 文章标题
- binder初始篇
- IP组播技术介绍及实现例子
- spring+PageHelper+mybatis(三)
- Activity和Fragment之间的数据通信
- GIF图像格式介绍
- Java EE 导入Maven项目报错
- 二进制、十进制、十六进制转换的算法
- 17.视图
- Java面试笔试题大汇总(最全+详细答案)
- iOS清除缓存机制
- 结构体指针