利用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会有不同的结果返回