java web(SSM框架)实现博客的上一篇、下一篇功能

运用了mybatis
1、Service层

 public Blog getAfterBlog(Integer blogId) {
        BlogExample blogExample = new BlogExample();
        BlogExample.Criteria criteria = blogExample.createCriteria();
        criteria.andBlogIdGreaterThan(blogId);
        Blog blog = new Blog();
        if(blogMapper.selectByExample(blogExample).size() > 0 ){
            blog = blogMapper.selectByExample(blogExample).get(0);
        }else{
            blog.setTitle("这是最后一篇文章");
            blog.setBlogId(blogId);
        }
        return blog;
    }

2、Service层

@Override
    public Blog getPreBlog(Integer blogId) {
        BlogExample blogExample = new BlogExample();
        BlogExample.Criteria criteria = blogExample.createCriteria();
        criteria.andBlogIdLessThan(blogId);
        Blog blog = new Blog();
        if(blogMapper.selectByExample(blogExample).size() > 0 ){
            blog = blogMapper.selectByExample(blogExample).get(0);
        }else{
            blog.setTitle("这是第一篇文章");
            blog.setBlogId(blogId);
        }
        return blog;
    }

3、Controller层

@RequestMapping(value="/view")
    public ModelAndView viewBlog(@RequestParam("blogId") Integer blogId){
//        log.debug("In viewBlog, blogID={}", blogId);
        Blog blog = blogServiceImpl.getBlogById(blogId);
        Blog blogPre = blogServiceImpl.getPreBlog(blogId);
        Blog blogAfter = blogServiceImpl.getAfterBlog(blogId);
        ModelAndView mav = new ModelAndView("blog_view");
        mav.addObject("blog",blog);
        mav.addObject("blogPre",blogPre);
        mav.addObject("blogAfter",blogAfter);
        return mav;

    }

4、前端

<div class="last-next">
            <div>
                <a href="${pageContext.request.contextPath}/blog/view?blogId=${blogPre.blogId}" title="上一篇">
                    <i class="icon-double-angle-left">i>${blogPre.title}
                a>
            div>
            <div>
                <a href="${pageContext.request.contextPath}/blog/view?blogId=${blogAfter.blogId}" title="下一篇">
                    <i class="icon-double-angle-right">i>${blogAfter.title}
                a>
            div>
        div>

你可能感兴趣的:(java web(SSM框架)实现博客的上一篇、下一篇功能)