分析
- 业务流程分析
- 表分析
- 界面分析
代码实现
- Servlet
/**
* 收藏线路分页
* @param request 请求
* @param response 响应
* @throws IOException IO异常
*/
public void myFavoritePageQuery(HttpServletRequest request, HttpServletResponse response) throws IOException {
//接收参数
String currentPageStr = request.getParameter("currentPage");
String pageSizeStr = request.getParameter("pageSize");
//获取当前用户
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
//判空
int uid;
if (user != null) {
//用户已经登陆
uid = user.getUid();
} else {
//用户未登陆或恶意访问
return;
}
//处理参数,并设置默认值
int pageSize = 5;
if (pageSizeStr != null && pageSizeStr.length() > 0) {
pageSize = Integer.parseInt(pageSizeStr);
}
int currentPage = 1;
if (currentPageStr != null && currentPageStr.length() > 0) {
currentPage = Integer.parseInt(currentPageStr);
}
//调用service方法
PageBean<Route> myFavoritePageBean= favoriteService.myFavoritePageQuery(uid,pageSize,currentPage);
//回写客户端
writeValue(myFavoritePageBean, response);
}
进行参数的获取,与用户是否登陆的校验,并设置分页默认值.
- Service
@Override
public PageBean<Route> myFavoritePageQuery(int uid, int pageSize, int currentPage) {
//创建PageBean
PageBean<Route> pageBean = new PageBean<>();
List<Route> routes = null;
//查询rid集合
List<Integer> rids = favoriteDao.findRidByUid(uid);
//查询收藏的route信息集合
//判空
if (rids != null && rids.size() > 0) {
//计算总记录数
int count = rids.size();
//注入总记录数
pageBean.setTotalCount(count);
//查询数据
//计算开始索引
int start = (currentPage - 1) * pageSize;
routes = new ArrayList<>();
for (Integer rid : rids) {
//根据rid查询线路信息
Route route = routeDao.findPageByRid(rid,start,pageSize);
routes.add(route);
}
//设置数据
pageBean.setList(routes);
//计算总页数
int totalPage = count % pageSize != 0 ? count / pageSize + 1 : count / pageSize;
//设置总页码
pageBean.setTotalPage(totalPage);
//设置当前页码
pageBean.setCurrentPage(currentPage);
//设置显示条数
pageBean.setPageSize(pageSize);
}
return pageBean;
}
根据用户的id在收藏表中查询出线路信息id的列表,根据线路id查询线路信息,然后添加进PageBean中,进行返回.
- Dao
@Override
public List<Integer> findRidByUid(int uid) {
String sql = "select rid from tab_favorite where uid= ?";
return template.queryForList(sql, Integer.class, uid);
}
@Override
public Route findPageByRid(int rid, int start, int pageSize) {
String sql = "select * from tab_route where rid= ? limit ?,? ";
return template.queryForObject(sql, new BeanPropertyRowMapper<>(Route.class), rid, start, pageSize);
}
在根据rid在tab_route查询线路信息.