后台文章管理功能设计(个人博客)

后台文章管理功能设计(个人博客)

在这里插入图片描述
在这里插入图片描述

1、编写dao层

@Mapper
public interface ArticleMapper {
    int deleteById(Integer id);

    int insert(Article record);

    Article selectByPrimaryKey(Long id);

    List<Article> selectAll();
    
    List<Article> queryArticlesByUserId(Long id);

    int  updateByCondition(Article article);

    List<Article> selectArticleByCid(int id);

    List<Article> queryByMany(Map map);

    @Select("select count(id) from article")
    int countArt();
}

2、编写绑定配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.pang.dao.ArticleMapper" >
  <resultMap id="BaseResultMap" type="article" >
    <id column="id" property="id"/>
    <result column="user_id" property="userId"/>
    <result column="cover_image" property="coverImage"/>
    <result column="category_id" property="categoryId"/>
    <result column="status" property="status"/>
    <result column="title" property="title"/>
    <result column="content" property="content"/>
    <result column="status_name" property="statusName"/>
    <result column="view_count" property="viewCount"/>
    <result column="created_at" property="createdAt"/>
    <result column="updated_at" property="updatedAt"/>
    <result column="comment_count" property="commentCount"/>
    <association property="author" resultMap="com.pang.dao.UserMapper.BaseResultMap">
      <id column="user_id" property="id"/>
    </association>
  </resultMap>
  <delete id="deleteById" parameterType="java.lang.Integer" >
    delete from article
    where id = #{id}
  </delete>
  <insert id="insert" parameterType="article" >
    <selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" >
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into article (user_id, cover_image, category_id, 
      status, title, content, 
      view_count, created_at, updated_at
      )
    values (#{userId}, #{coverImage}, #{categoryId}, 
      #{status}, #{title}, #{content}, 
      #{viewCount}, #{createdAt}, #{updatedAt}
      )
  </insert>
  <update id="updateByPrimaryKey" parameterType="article" >
    update article
    set user_id = #{userId},
      cover_image = #{coverImage},
      category_id = #{categoryId},
      status = #{status},
      title = #{title},
      content = #{content},
      view_count = #{viewCount},
      created_at = #{createdAt},
      updated_at = #{updatedAt}
    where id = #{id}
  </update>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
    select a.id,
           a.user_id,
           a.cover_image,
           a.category_id,
           a.status,
           a.title,
           a.content,
           a.view_count,
           a.created_at,
           a.updated_at,
           u.avatar,
           u.nickname,
           count(c.article_id) comment_count
        from article a
         join user u on a.user_id = u.id
         left join comment c on a.id = c.article_id
    where a.id = #{id}
  </select>
    <select id="queryByMany" parameterType="map" resultMap="BaseResultMap">
        select a.id,
           a.user_id,
           a.cover_image,
           a.category_id,
           a.status,
           a.title,
           a.content,
           a.view_count,
           a.created_at,
           a.updated_at,
           u.avatar,
           u.nickname,
           count(c.article_id) comment_count
        from article a
         join user u on a.user_id = u.id
         left join comment c on a.id = c.article_id
         where a.category_id = #{categoryId} and a.status = #{status}
    group by a.id
    order by a.id
    </select>
    <select id="selectArticleByCid" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select a.id,
           a.user_id,
           a.cover_image,
           a.category_id,
           a.status,
           a.title,
           a.content,
           a.view_count,
           a.created_at,
           a.updated_at,
           u.avatar,
           u.nickname,
           count(c.article_id) comment_count
		 from article a
         join user u on a.user_id = u.id
         left join comment c on a.id = c.article_id
         where a.category_id = #{id}
   		 group by a.id
  	     order by a.id
  </select>
  <select id="selectAll" resultMap="BaseResultMap" >
    select a.id,
           a.user_id,
           a.cover_image,
           a.category_id,
           a.status,
           a.title,
           a.content,
           a.view_count,
           a.created_at,
           a.updated_at,
           u.avatar,
           u.nickname,
           count(c.article_id) comment_count
		 from article a
         join user u on a.user_id = u.id
         left join comment c on a.id = c.article_id
		 group by a.id
		 order by a.id
  </select>
    <select id="queryArticlesByUserId" resultMap="BaseResultMap" parameterType="java.lang.Long" >
    select a.id,
           a.user_id,
           a.cover_image,
           a.category_id,
           a.status,
           a.title,
           a.content,
           a.view_count,
           a.created_at,
           a.updated_at,
           u.avatar,
           u.nickname,
           count(c.article_id) comment_count
    from article a
    join user u on a.user_id = u.id
    left join comment c on a.id = c.article_id
    where a.user_id = #{id}
    group by a.id
    order by a.id
  </select>
  <update id="updateByCondition" parameterType="article" >
    update article
    set
          cover_image = #{coverImage},
          category_id = #{categoryId},
          title = #{title},
          content = #{content},
          updated_at = #{updatedAt}
    where id = #{id}
  </update>
</mapper>

3、编写service层

@Service
public class ArticleService {
    
    @Autowired
    private ArticleMapper articleMapper;

    public List<Article> queryArticles() {
        return articleMapper.selectAll();
    }

    public Article queryArticle(Long id) {
        return articleMapper.selectByPrimaryKey(id);
    }
    
    public int insert(Article article) {
        return articleMapper.insert(article);
    }

    public int updateByCondition(Article article) {
        return articleMapper.updateByCondition(article);
    }


    public int delete(Integer id) {
        return articleMapper.deleteById(id);
    }

    public List<Article> selectArticleByCid(int id) {
        return articleMapper.selectArticleByCid(id);
    }

    public List<Article> queryByMany(Map map){
        return articleMapper.queryByMany(map);
    }

    public int countArt(){
        return articleMapper.countArt();
    }

}

4、编写controller层


	@RequestMapping("/admin")
    public String admin(@RequestParam(value = "pageNum",defaultValue = "1") Integer 			pageNum,Model model){
        List<Category> categories = categoryService.allCategories();
        model.addAttribute("types",categories);
        PageHelper.startPage(pageNum,4);
        List<Article> articles = articleService.queryArticles();
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/blogs";
    }
    
        @RequestMapping("/admin/toWrite")
    public String toWrite(Model model){
        List<Category> categories = categoryService.allCategories();
        model.addAttribute("types",categories);
        model.addAttribute("article", new Article());
        return "admin/blogs-input";
    }
@RequestMapping("/admin/toUpdate/{id}")
    public String toUpdate(@PathVariable("id") Long id,Model model){
        List<Category> categories = categoryService.allCategories();
        model.addAttribute("types",categories);
        Article article = articleService.queryArticle(id);
        model.addAttribute("article",article);
        return "admin/blogs-update";
    }

    @RequestMapping("/admin/searMany")
    public String searchByUserId(Integer categoryId,Boolean recommend ,Model model) {
        List<Category> categories = categoryService.allCategories();
        model.addAttribute("types",categories);
        if(categoryId==null){
            List<Article> articles = articleService.queryArticles();
            model.addAttribute("articleList", articles);
            return "admin/blogs";
        }
        List<Article> articles = articleService.selectArticleByCid(categoryId);
        model.addAttribute("articleList", articles);
        return "admin/blogs";
    }
    
    @RequestMapping(value = "/admin/writer/article", method = RequestMethod.POST)
    public String publish(Article article, Principal principal) {
        article.setUpdatedAt(new Date());
        User user = userService.getByName(principal.getName());
        article.setUserId(user.getId());
        article.setCoverImage(article.getCoverImage());
        article.setCreatedAt(new Date());
        article.setStatus((byte) 0);
        article.setViewCount(0L);
        article.setCommentCount(0);
        int num = articleService.insert(article);
        return "redirect:/admin";
    }

    @RequestMapping("/admin/updateArt")
    public String updateArticle(Article article, HttpSession session) {
        article.setUpdatedAt(new Date());
        int num = articleService.updateByCondition(article);
        return "redirect:/admin";
    }

    @RequestMapping("/admin/deleteArt/{id}")
    public String deleteArt(@PathVariable("id") int id) {
        articleService.delete(id);
        return "redirect:/admin";
    }

5、绑定前端页面

 <form th:action="@{/admin/searMany}" method="post" class="ui segment form">
 <div class="item" th:each="type:${types}" th:data-value="${type.statusId}" th:text="${type.name}">错误日志</div>
 <tr th:each="article:${pageInfo.list}">
    <td th:text="${article.id}">1</td>
    <td th:text="${article.title}"></td>
    <td th:text="${article.categoryId}"></td>
    <td th:text="${article.status}==0?'':''"></td>
    <td th:text="${article.createdAt}"></td>
    <td th:text="${article.author.getNickname()}"></td>
    <td>
        <a th:href="@{/admin/toUpdate/{id}(id=${article.id})}" class="ui mini teal basic button">编辑</a>
        <a th:href="@{/admin/deleteArt/{id}(id=${article.id})}" class="ui mini red basic button">删除</a>
    </td>
 </tr>
 <a class="icon item" th:href="@{/admin(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}" th:unless="${pageInfo.isFirstPage}">上一页</a>
 <a class="icon item" th:href="@{/admin(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}" th:unless="${pageInfo.isLastPage}">下一页</a>
 <a th:href="@{/admin/toWrite}" class="ui right floated mini teal basic button">新增</a>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值