Idea分页查询

idea的分页查询!
分页查询具体操作:
用户实体类

public class User {

private int id;
private String name;
private String sex;

public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
public String getSex() {
	return sex;
}
public void setSex(String sex) {
	this.sex = sex;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}

}

分页的实体类

public class Page {
/**
* 页码
/
private int pageNum;
/
*
* 每页显示的行数
/
private int pageRows;
/
*
* 总行数
/
private int totalRows;
/
*
* 总页数
/
private int totalPages;
/
*
* 起始行号
/
private int beginRownum;
/
*
* 结束行号
*/
private int endRownum;

public Page(int pageNum, int pageRows, int totalRows) {
	this.pageNum = pageNum;
	this.pageRows = pageRows;
	this.totalRows = totalRows;
	// 计算总页数:总行数%每页行数==0?总行数/每页行数:总行数/每页行数+1
	this.totalPages = totalRows % pageRows == 0 ? totalRows / pageRows : (totalRows / pageRows) + 1;
	beginRownum = (pageNum - 1) * pageRows;
	endRownum = pageNum * pageRows;
}

public int getPageNum() {
	return pageNum;
}

public void setPageNum(int pageNum) {
	this.pageNum = pageNum;
}

public int getPageRows() {
	return pageRows;
}

public void setPageRows(int pageRows) {
	this.pageRows = pageRows;
}

public int getTotalRows() {
	return totalRows;
}

public void setTotalRows(int totalRows) {
	this.totalRows = totalRows;
}

public int getTotalPages() {
	return totalPages;
}

public void setTotalPages(int totalPages) {
	this.totalPages = totalPages;
}

public int getBeginRownum() {
	return beginRownum;
}

public void setBeginRownum(int beginRownum) {
	this.beginRownum = beginRownum;
}

public int getEndRownum() {
	return endRownum;
}

public void setEndRownum(int endRownum) {
	this.endRownum = endRownum;
}

}

mapper 和 映射文件

public interface UserMapper {

public List selectAll(Map map);

//查询总行数
public int selTotalRows(Map map);

}





控制层

public class UserController {

	@Autowired
	private UserService userService;

	@RequestMapping("/showAll.action")
	public String selAll(HttpServletRequest request){
		Map map = new HashMap();	
		map.put("name", null);
		map.put("sex", null);
		Page page = new Page(1,5,userService.selTotalRows(map));
		map.put("page", page);
		List userList = userService.selectAll(map);
		request.setAttribute("userList", userList);
		request.setAttribute("page", page);
		return "two.jsp";
	}
	
	
	//查询所有用户(带模糊查询、分页)
	@RequestMapping("/showPage.action")
	public String selAllLikePage(int pageNum,String name,String sex,HttpServletRequest request){
		Map map = new HashMap();
		if("".equals(name)){
			map.put("name", null);
		}else{
			map.put("name", "%"+name+"%");
		}
		if("".equals(sex)){
			map.put("sex", null);
		}else{
			map.put("sex", sex);
		}
		Page page = new Page(pageNum,5,userService.selTotalRows(map));
		map.put("page", page);
		List userList = userService.selectAll(map);
		request.setAttribute("userList",userList);
		request.setAttribute("page", page);
		request.setAttribute("name", name);
		request.setAttribute("sex", sex);
		return "two.jsp";
	}

}

jsp页面

姓名: 性别:


序号 姓名 性别
${user.id } ${user.name } ${user.sex }
首页 上一页 ${num } ${num } 下一页 尾页 总${page.pageNum}/${page.totalPages }页

你可能感兴趣的:(Idea分页查询)