PageQuery
public class PageQuery {
@Getter
@Setter
@Min(value = 1, message = "当前页码不合法")
private int pageNo = 1;
@Getter
@Setter
@Min(value = 1, message = "每页展示数量不合法")
private int pageSize = 10;
@Setter
private int offset;
public int getOffset() {
return (pageNo - 1) * pageSize;
}
}
@Getter
@Setter
@ToString
@Builder
public class PageResult<T> {
private List<T> data = Lists.newArrayList();
private int total = 0;
}
action
@RequestMapping("/page.json")
@ResponseBody
public JsonData page(@RequestParam("deptId") int deptId, PageQuery pageQuery) {
PageResult<SysUser> result = sysUserService.getPageByDeptId(deptId, pageQuery);
return JsonData.success(result);
}
service
public PageResult<SysUser> getPageByDeptId(int deptId, PageQuery page) {
BeanValidator.check(page);
int count = sysUserMapper.countByDeptId(deptId);
if (count > 0) {
List<SysUser> list = sysUserMapper.getPageByDeptId(deptId, page);
return PageResult.<SysUser>builder().total(count).data(list).build();
}
return PageResult.<SysUser>builder().build();
}
sql
<select id="countByDeptId" parameterType="int" resultType="int">
SELECT count(1)
FROM sys_user
WHERE dept_id = #{deptId}
</select>
List<SysUser> getPageByDeptId(@Param("deptId") int deptId, @Param("page") PageQuery page);
<select id="getPageByDeptId" parameterType="map" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List" />
FROM sys_user
WHERE dept_id = #{deptId}
ORDER BY username ASC
LIMIT #{page.offset}, #{page.pageSize}
</select>