栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot开源项目个人博客——(7)博客管理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot开源项目个人博客——(7)博客管理

有了分类后,就可以做博客管理,博客新增、查询,删除,编辑修改,搜索博客功能,重点是mybatis的多表查询。

这里引用oneStar的内容,不详细描述,具体可看大佬的内容。

并不是全部按顺序步骤写的记录,只是以这种顺序写,内容结构比较好排版。

一、搭建好对应的基础结构:
  • dao包下的BlogDao接口,BlogDao.xml
  • service包下的BlogService 接口和impl包下的BlogServiceImpl接口实现类
  • admin包下的BlogController控制器

如果对下面的Controller觉得不理解,可以再编写之前,先细看一遍前端页面,对了解建立哪些接口,哪些功能有帮助。


二、博客列表查询

在查询文章列表的时候,前端页面需要显示分类名称,但博客数据表没有分类字段,这时候需要构建DTO/VO实体类。

  1. entity 里的每一个字段,与数据库相对应,

  2. vo 里的每一个字段,是和你前台 html 页面相对应,

  3. dto 这是用来转换从 entity 到 vo,或者从 vo 到 entity 的中间的东西 。(DTO中拥有的字段应该是entity中或者是vo中的一个子集)

https://blog.csdn.net/qq_40871196/article/details/107575188

在com.xx包下创建queryvo包,创建BlogQuery查询列表实体类,根据前端需要查询的内容来定义变量,有:主键(id)、标题(title)、更新时间(updateTime)、是否推荐(recommend)、是否发布(published)、分类id(typeId)、分类(type)

代码如下:(省去get、set、toString、构造)

public class BlogQuery {
    //根据前端 需要查询的内容来定义变量
    
    private Long id;
    private String title;
    private Date updateTime;
    private boolean recommend;
    private boolean published;
    private Long typeId; //选分类要用的
    private Type type;
}
  1. 在BlogDao下添加接口

    //查询文章管理列表
    List getAllBlogQuery();
    
  2. (重点)mapper文件的多表查询

    
    

    
    
        
        
        
        
        
        
        
        
        
            
            
        
    

在mybatis配置了驼峰转换(而且BlogQuery中的字段除了驼峰和同名外,没有与数据库又差别),实际上resultMap 只需要配association多对一映射,一个复杂类型的关联。(mybatis多表查询知识,不在此讲述。)

复杂的属性,需要单独处理
    对象 : association  (多个学生 关联 一个老师)--》多/一个博客对应着一个分类
    集合 : collection (老师有多个学生)--》一个分类又多篇博客

- List 集合用collection,集合中的泛型信息, 我们使用ofType获取。
- javaType=" " 指定实体类中属性的类型。
  1. 在BlogService接口下添加

    //查询文章管理列表
    List getAllBlog();
    
  2. 在BlogServiceImpl接口实现类添加

    @Autowired
    private BlogDao blogDao;
    
    //查询文章管理列表
    @Override
    public List getAllBlogQuery() {
        return blogDao.getAllBlogQuery();
    }
    
  3. 在admin包下的BlogController类中添加
    (都是使用pageHelper分页查询,此后不再强调)

    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    
    //博客列表
    @RequestMapping("/blogs")
    public String blogs(Model model, @RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum){
        //按照排序字段, 倒序
        String orderBy = " update_time desc";
        PageHelper.startPage(pageNum, 5,orderBy);
        List list = blogService.getAllBlogQuery();
        PageInfo pageInfo = new PageInfo<>(list);//封装 查询信息
    
        model.addAttribute("types",typeService.getAllType());
        model.addAttribute("pageInfo",pageInfo);
    
        return "admin/blogs";
    }
    
三、搜索博客列表

搜索是使用的MySQL的模糊查询,根据博客标题和博客分类查询出想要搜索的文章,需要创建有标题和分类属性的实体类做vo查询

在queryvo包下创建SearchBlog实体类(省去get、set、toString、构造)

public class SearchBlog {
    private String title;
    private  Long typeId;
}
  1. 在BlogDao下添加接口:

        //查询文章(传searchBlog , 返回的是 BlogQuery 列表)
        List searchByTitleAndType(SearchBlog searchBlog);
    
  2. mapper文件

    
        
            select id,
                   appreciation,
                   commentabled,
                   content,
                   description,
                   first_picture,
                   flag,
                   published,
                   recommend,
                   share_statement,
                   title,
                   type_id
            from t_blog
            where id = #{id};
        
    
        
            
            
            
            
            
    
        
    

    当时还没配置驼峰转换,所以写了resultMap,实际配置后可以不需要写

    1. 在BlogService接口下添加

       	//编辑博客
          int updateBlog(ShowBlog showBlog);
      
          //查询编辑修改的文章
          ShowBlog getBlogById(Long id);
      
    2. 在BlogServiceImpl接口实现类添加

      	@Override
          public ShowBlog getBlogById(Long id) {
              return blogDao.getBlogById(id);
          }
      
          @Override
          public int updateBlog(ShowBlog showBlog) {
              showBlog.setUpdateTime( new Date());
              return blogDao.updateBlog(showBlog);
          }
      
    3. 在admin包下的BlogController类中添加

      	//跳转到 修改博客
          @GetMapping("/blogs/{id}/input")
          public String editInput(@PathVariable Long id,Model model){
              ShowBlog blogById = blogService.getBlogById(id);
              System.out.println(blogById.toString());
      
              List allType = typeService.getAllType();
      
      
              model.addAttribute("blog",blogById);
              model.addAttribute("types",allType); //分类列表选择
      
      
              return "admin/blogs-input";
          }
      
          //提交编辑修改的为文章
          @PostMapping("/blogs/{id}")
          public String editPost(ShowBlog showBlog,RedirectAttributes attributes){
      
              int k = blogService.updateBlog(showBlog);
              if(k>0){
                  attributes.addFlashAttribute("message","修改成功");
              }else{
                  attributes.addFlashAttribute("message","修改失败");
              }
      
              return "redirect:/admin/blogs";
          }
      

    六、博客删除

    删除只需要根据博客Id来访问对应的控制器即可

    1. 在BlogDao下添加接口

          //删除博客
          void deleteBlog(Long id );
      
    2. mapper文件

          
          
              delete
              from t_blog
              where id = #{id};
          
      
    3. 在BlogService接口下添加

          //删除博客
          void deleteBlog(Long id);
      
    4. 在BlogServiceImpl接口实现类添加

          @Override
          public void deleteBlog(Long id) {
              blogDao.deleteBlog(id);
          }
      
    5. 在admin包下的BlogController类中添加

          //删除博客
          @GetMapping("/blogs/{id}/delete")
          public String delete(@PathVariable Long id,RedirectAttributes attributes){
              blogService.deleteBlog(id);
              attributes.addFlashAttribute("message","删除成功");
              return "redirect:/admin/blogs";
          }
      

    至此博客管理功能完成,这部分创建了3个VO实体类, BlogQuery(博客列表),SearchBlog(博客搜索),ShowBlog(编辑博客)。

    接下来的友链、相册就比较简单了,业务不算复杂和分类很像。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/936892.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号