查询操作

  1. 1. 数据库的查询操作
    1. 1.1. web层
    2. 1.2. service层
    3. 1.3. dao层
      1. 1.3.1. 国庆几天终于把前端的数据和分页搞定了,太艰难了

数据库的查询操作

区间查询可以把日程抽象成数据

web层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
protected void pageByYear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("经过了前台的byueservlet程序");
//1.获取请求的参数pageNo和pageSize
int pageNo = WebUtils.parseInt(req.getParameter("pageNo"),1);
int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
int min = WebUtils.parseInt(req.getParameter("min"),0);
int max = WebUtils.parseInt(req.getParameter("max"),Integer.MAX_VALUE);
//2.调用BuyeService.page(pageNo,pageSize);page对象
Page<Byue> page = byueService.pageByYear(pageNo,pageSize,min,max);

StringBuffer sb = new StringBuffer("clientByueServlet?action=pageByYear");
//如果有最小值参数,追加到分页条的地址参数中
if(req.getParameter("min")!=null){
sb.append("&min=").append(req.getParameter("min"));
}
//如果有最大值参数,追加到分页条的地址参数中
if(req.getParameter("max")!=null){
sb.append("&max=").append(req.getParameter("max"));
}
page.setUrl(sb.toString());
//3.保存到request域中
req.setAttribute("page",page);
//4.请求转发
req.getRequestDispatcher("/pages/home.jsp").forward(req,resp);
}

service层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public Page<Byue> pageByYear(int pageNo, int pageSize, int min, int max) {
Page<Byue> page = new Page<Byue>();

//设置每页显示的数量
page.setPageSize(pageSize);
//求总记录数
Integer pageTotalCount = byueDao.querForPageTotalCountByYear(min,max);
//设置总记录数
page.setPageTotalCount(pageTotalCount);
//求总页码
Integer pageTotal = pageTotalCount / pageSize;
if (pageTotalCount % pageSize > 0 ){
pageTotal+=1;
}
//设置总页码
page.setPageTotal(pageTotal);
/*数据边界的有效检查 在page.java中直接检查*/

//设置当前页码
page.setPageNo(pageNo);

int begin = (page.getPageNo()-1) * pageSize;
//求当前页数据
List<Byue> items = byueDao.querForPageItemsByYear(begin,pageSize,min,max);
//设置当前页数据
page.setItem(items);

return page;
}

dao层

1
2
3
4
5
6
7
8
9
10
11
12
13
@Override
public Integer querForPageTotalCountByYear(int min, int max) {
String sql = "select count(*) from s_by where year between ? and ?";
Number count = (Number)queryForSingleValue(sql,min,max);
return count.intValue();
}

@Override
public List<Byue> querForPageItemsByYear(int begin, int pageSize, int min, int max) {
String sql = "select `id`,`name`,`sclass`,`bm`, `zw`, `year`,`img_path` imgPath " +
"from s_by where year between ? and ? order by year limit ?,?";
return queryForyList(Byue.class,sql,min,max,begin,pageSize);
}

国庆几天终于把前端的数据和分页搞定了,太艰难了