1.文件结构
2.公共类:
PageParam.java
package util;
public class PageParam {
private int page = 1;
private int rows = 10;
public void setPage(int page) {
this.page = page;
}
public void setRows(int rows) {
this.rows = rows;
}
public int getFirst() {
return (page - 1) * rows;
}
public int getMax() {
return rows;
}
}
PageResult.java
package util;
import java.util.List;
public class PageResult {
private Number total;
private List rows;
public PageResult(Number total, List rows) {
this.total = total;
this.rows = rows;
}
public Number getTotal() {
return total;
}
public List getRows() {
return rows;
}
}
3.示例:
UserDao.java
package dao;
import java.util.List;
import org.springframework.stereotype.Component;
import entity.User;
import util.PageParam;
import util.PageResult;
@Component
public class UserDao extends BaseDao {
public PageResult page(PageParam param) {
String sql = "select count(*) from spring_orm_user";
Number total = queryForNumber(sql);
String sql2 = "select id,name from spring_orm_user limit ?,?";
List rows = queryForList(sql2, param.getFirst(), param.getMax());
return new PageResult(total, rows);
}
//其它代码
}
UserService.java
package service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import dao.UserDao;
import entity.User;
import util.PageParam;
import util.PageResult;
@Component
public class UserService {
@Autowired
private UserDao userDao;
public PageResult page(PageParam param) {
return userDao.page(param);
}
//其它代码
}
UserController.java
package controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import entity.User;
import service.UserService;
import util.PageParam;
import util.PageResult;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/page.do")
@ResponseBody
public PageResult page(PageParam param) {
return userService.page(param);
}
//其它代码
}
over.