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<User> selectAll(Map<String,Object> map);

//查询总行数
public int selTotalRows(Map<String,Object> map);

}

<select id="selectAll" resultType="com.entity.User">
	select * from user 
	<where>
		<if test="name != null and name != ''">
			and name like #{name}
		</if>
		<if test="sex != null and sex != ''">
			and sex = #{sex}
		</if>
	</where>
	limit #{page.beginRownum},5
</select>

<!-- 查询总行数 -->
<select id="selTotalRows" resultType="int">
	select count(id) from user
	<where>
		<if test="name != null and name != ''">
			and name like #{name}
		</if>
		<if test="sex != null and sex != ''">
			and sex = #{sex}
		</if>
	</where>
</select>

控制层

public class UserController {

	@Autowired
	private UserService userService;

	@RequestMapping("/showAll.action")
	public String selAll(HttpServletRequest request){
		Map<String,Object> map = new HashMap<String, Object>();	
		map.put("name", null);
		map.put("sex", null);
		Page page = new Page(1,5,userService.selTotalRows(map));
		map.put("page", page);
		List<User> 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<String,Object> map = new HashMap<String, Object>();
		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<User> 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 }页
</div>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值