SSM框架——实现分页和搜索分页
来源:互联网 发布:外贸大数据分析 编辑:程序博客网 时间:2024/05/16 06:58
分页是JAVA WEB项目常用的功能,昨天在Spring MVC中实现了简单的分页操作和搜索分页,在此记录一下。使用的框架为(MyBatis+SpringMVC+Spring)。
首先我们需要一个分页的工具类:
1.分页
有了这个工具类后,首先编写MyBatis的XxxxMapper.xml配置文件中的SQL语句,如下:-
- <select id="selectProductsByPage" resultMap="返回值类型">
- select
- *
- from 表名 WHERE user_id = #{userId,jdbcType=INTEGER} limit #{startPos},#{pageSize}
- </select>
-
- <select id="getProductsCount" resultType="long">
- SELECT COUNT(*) FROM 表名 WHERE user_id = #{userId,jdbcType=INTEGER}
- </select>
此处我们可以看到,2个<select>需要分别传入3个和1个参数,此时在对应的DAO文件IXxxxDao中编写接口来编写对应的方法,方法名和mapper.xml中的id属性值一致:-
-
-
-
-
-
- public List<Products> selectProductsByPage(@Param(value="startPos") Integer startPos,@Param(value="pageSize") Integer pageSize,@Param(value="userId") Integer userId);
-
-
-
-
-
-
- public long getProductsCount(@Param(value="userId") Integer userId);
接口定义完成之后需要编写相应的业务接口和实现方法,在接口中定义这样一个方法,然后实现类中覆写一下:
-
-
-
-
-
-
- void showProductsByPage(HttpServletRequest request,Model model,int loginUserId);
接下来实现类中的方法就是要调用DAO层和接受Controller传入的参数,进行业务逻辑的处理,request用来获取前端传入的参数,model用来向JSP页面返回处理结果。
- @Override
- public void showProductsByPage(HttpServletRequest request, Model model,int loginUserId) {
- String pageNow = request.getParameter("pageNow");
-
- Page page = null;
-
- List<ProductWithBLOBs> products = new ArrayList<ProductWithBLOBs>();
-
- int totalCount = (int) productDao.getProductsCount(loginUserId);
-
- if (pageNow != null) {
- page = new Page(totalCount, Integer.parseInt(pageNow));
- allProducts = this.productDao.selectProductsByPage(page.getStartPos(), page.getPageSize(), loginUserId);
- } else {
- page = new Page(totalCount, 1);
- allProducts = this.productDao.selectProductsByPage(page.getStartPos(), page.getPageSize(), loginUserId);
- }
-
- model.addAttribute("products", products);
- model.addAttribute("page", page);
- }
接下来是控制器的编写,当用户需要跳转到这个现实产品的页面时,就需要经过这个控制器中相应方法的处理,这个处理过程就是调用业务层的方法来完成,然后返回结果到JSP动态显示,服务器端生成好页面后传给客户端(浏览器)现实,这就是一个MVC过程。
-
-
-
-
-
-
-
- @RequestMapping(value = "映射路径", method = RequestMethod.GET)
- public String showMyProduct(HttpServletRequest request, Model model) {
-
- User loginUser = (User) request.getSession().getAttribute("loginUser");
-
- if (loginUser == null || "".equals(loginUser)) {
- return "redirect:/";
- }
-
- int loginUserId = loginUser.getUserId();
-
- this.productService.showProductsByPage(request, model, loginUserId);
-
- return "跳转到的JSP路径";
- }
JSP页面接受部分我就不写了,每个人都一样,也就是结合JSTL和EL来写,(在循环输出的时候也做了判断,如果接受的参数为空,那么输出暂无商品,只有接受的参数不为空的时候,才循环输出,使用<<c:when test="${}">结合<c:otherwise>),这里只给出分页的相关代码:
-
- <div align="center">
- <font size="2">共 ${page.totalPageCount} 页</font> <font size="2">第
- ${page.pageNow} 页</font> <a href="myProductPage?pageNow=1">首页</a>
- <c:choose>
- <c:when test="${page.pageNow - 1 > 0}">
- <a href="myProductPage?pageNow=${page.pageNow - 1}">上一页</a>
- </c:when>
- <c:when test="${page.pageNow - 1 <= 0}">
- <a href="myProductPage?pageNow=1">上一页</a>
- </c:when>
- </c:choose>
- <c:choose>
- <c:when test="${page.totalPageCount==0}">
- <a href="myProductPage?pageNow=${page.pageNow}">下一页</a>
- </c:when>
- <c:when test="${page.pageNow + 1 < page.totalPageCount}">
- <a href="myProductPage?pageNow=${page.pageNow + 1}">下一页</a>
- </c:when>
- <c:when test="${page.pageNow + 1 >= page.totalPageCount}">
- <a href="myProductPage?pageNow=${page.totalPageCount}">下一页</a>
- </c:when>
- </c:choose>
- <c:choose>
- <c:when test="${page.totalPageCount==0}">
- <a href="myProductPage?pageNow=${page.pageNow}">尾页</a>
- </c:when>
- <c:otherwise>
- <a href="myProductPage?pageNow=${page.totalPageCount}">尾页</a>
- </c:otherwise>
- </c:choose>
- </div>
-
2.查询分页
关于查询分页,大致过程完全一样,只是第三个参数(上面是loginUserId)需要接受用户输入的参数,这样的话我们需要在控制器中接受用户输入的这个参数(页面中的<input>使用GET方式传参),然后将其加入到SESSION中,即可完成查询分页(此处由于“下一页”这中超链接的原因,使用了不同的JSP页面处理分页和搜索分页,暂时没找到在一个JSP页面中完成的方法,出现了重复代码,这里的重复代码就是输出内容的那段代码,可以单独拿出去,然后用一个<include>标签加载到需要的JSP页面就可以了,这样可以避免代码重复): 这里给出控制器的代码作为参考:
-
-
-
-
-
-
- @RequestMapping(value = "映射地址", method = RequestMethod.GET)
- public String searchForProducts(HttpServletRequest request, Model model) {
- HttpSession session = request.getSession();
-
- String param = request.getParameter("param");
-
- String condition = (String) session.getAttribute("condition");
-
-
- if (condition == null) {
- condition = new String();
- session.setAttribute("condition", condition);
-
- if (param == null || "".equals(param)) {
- return "private/space/ProductSearchResult";
- }
- }
-
- if (param != null && !("".equals(param))) {
- condition = param;
- session.setAttribute("condition", condition);
- }
-
- this.productService.showSearchedProductsByPage(request, model, condition);
-
- return "跳转的页面";
- }
文章出处:http://blog.csdn.net/zhshulin/article/details/26447713