基于spring的博客系统(一)

打印 上一主题 下一主题

主题 1772|帖子 1772|积分 5316

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
         通过前⾯课程的学习, 我们把握了Spring框架和MyBatis的基本使⽤, 并完成了图书管理系统的常规功能 开发, 接下来我们系统的从0到1完成⼀个项⽬的开发
1. 项⽬介绍

使⽤SSM框架实现⼀个简单的博客系统 共5个⻚⾯
           1. 用户登录
          2. 博客发表⻚
          3. 博客编辑⻚
          4. 博客列表⻚
          5. 博客详情⻚
  1.1 功能描述

        ⽤⼾登录成功后, 可以检察所有⼈的博客. 点击 《检察原文》 可以检察该博客的正⽂内容;
         如果该博客作者为当前登录⽤⼾, 可以完成博客的修改和删除操作, 以及发表新博客 ;
1.2 ⻚⾯预览

        登录页面:

        博客列表和博客详情⻚:

 博客发表和更新页:

2.  预备工作

2.1 数据预备

创建user表和blog表;
  1. create database if not exists spring_blog_240908 charset utf8mb4;
  2. use spring_blog_240908;
  3. drop table if exists user;
  4. create table user (
  5.     `id` INT NOT NULL AUTO_INCREMENT,
  6.      `user_name` VARCHAR ( 128 ) NOT NULL,
  7.      `password` VARCHAR ( 128 ) NOT NULL,
  8.      `github_url` VARCHAR ( 128 ) NULL,
  9.      `delete_flag` TINYINT ( 4 ) NULL DEFAULT 0,
  10.      `create_time` DATETIME DEFAULT now(),
  11.      `update_time` DATETIME DEFAULT now(),
  12.      PRIMARY KEY ( id ),
  13.     UNIQUE INDEX user_name_UNIQUE ( user_name ASC ))
  14.     ENGINE = INNODB DEFAULT
  15.     CHARACTER set = utf8mb4 comment = '用户表';
  16. drop table if exists blog;
  17. CREATE TABLE blog (
  18. `id` INT NOT NULL AUTO_INCREMENT,
  19. `title` VARCHAR(200) NULL,
  20. `content` TEXT NULL,
  21. `user_id` INT(11) NULL,
  22. `delete_flag` TINYINT(4) NULL DEFAULT 0,
  23. `create_time` DATETIME DEFAULT now(),
  24. `update_time` DATETIME DEFAULT now(),
  25. PRIMARY KEY (id))
  26. ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT = '博客表';
  27. insert into user (user_name,password,github_url)values("shenmengyao","111111","https://gitee.com/smollye/projects");
  28. insert into user (user_name,password,github_url)values("yuanyiqi","222222","https://gitee.com/smollye/projects");
  29. insert into blog (title,content,user_id) values("第⼀篇博客","我是神喵我是神喵我是神喵",1);
  30. insert into blog (title,content,user_id) values("第⼆篇博客","我是小黑我是小黑我是小黑",2);
复制代码
数据库信息如下所示:

2.2 创建项⽬


        创建SpringBoot项⽬, 添加Spring MVC 和MyBatis对应依赖
2.3 预备前端页面

        把课博客系统静态⻚⾯拷⻉到static⽬录下

2.4 配置配置⽂件

  1. server:
  2.   port: 8087
  3. spring:
  4.   datasource:
  5.     url: jdbc:mysql://127.0.0.1:3306/spring_blog_240908?characterEncoding=utf8&useSSL=false
  6.     username: root
  7.     password: ******
  8.     driver-class-name: com.mysql.cj.jdbc.Driver
  9. mybatis:
  10.   configuration:
  11.     map-underscore-to-camel-case: true #配置驼峰自动转换
  12.     log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印Sql语句
  13.   mapper-locations: classpath:mapper/**Mapper.xml
  14. # 设置日志路径
  15. logging:
  16.   file:
  17.     name: spring_blog_240908.log
复制代码
2.5 测试

        访问前端⻚⾯:

        前端⻚⾯可以精确显⽰, 说明项⽬初始化成功;
3. 项⽬公共模块

        项⽬分为控制层(Controller), 服务层(Service), 持久层(Mapper). 各层之间的调⽤关系如下: 

        先根据需求完成实体类和公共层代码的编写;
3.1 实体类

  1. @Data
  2. public class Blog {
  3.     private Integer id;
  4.     private String title;
  5.     private String content;
  6.     private Integer userId;
  7.     private String deleteFlag;
  8.     private Date createTime;
  9.     private Date updateTime;
  10. }
复制代码
  1. @Data
  2. public class User {
  3.     private Integer id;
  4.     private String userName;
  5.     private String password;
  6.     private String githubUrl;
  7.     private Byte deleteFlag;
  8.     private Date createTime;
  9.     private Date updateTime;
  10. }
复制代码
3.2 公共层

3.2.1  统⼀返回结果实体类

    a. code: 业务状态码 
            200: 业务处理成功
           -1 : 业务处理失败
           -2 : ⽤户未登录
           后续有其他非常信息, 可以再补充.
  b. msg: 业务处理失败时, 返回的错误信息         
  c. data: 业务返回数据
    业务状态码设置代码如下:
  1. public class Constant {
  2.     //返回业务的状态码设置
  3.     public  final  static  Integer SUCCESS_CODE = 200;
  4.     public final static Integer FAIL_CODE = -1;
  5.     public final static Integer UNLOGIN_CODE = -2;
  6. }
复制代码
   返回结果实体类设置代码:
  1. package com.example.spring_blog_24_9_8.model;
  2. import com.example.spring_blog_24_9_8.constants.Constant;
  3. import lombok.Data;
  4. @Data
  5. public class Result {
  6.     private int code;
  7.     private String msg;
  8.     private Object data;
  9.     /**
  10.      * 业务成功时执行的方法
  11.      */
  12.     public static Result success(Object data){
  13.         Result result = new Result();
  14.         result.setCode(Constant.SUCCESS_CODE);
  15.         result.setMsg("");
  16.         result.setData(data);
  17.         return result;
  18.     }
  19.     /**
  20.      *  业务执⾏失败时返回的⽅法
  21.      */
  22.     public  static  Result fail(int code, String msg){
  23.         Result result = new Result();
  24.         result.setCode(Constant.FAIL_CODE);
  25.         result.setMsg(msg);
  26.         result.setData("");
  27.         return result;
  28.     }
  29.     /**
  30.      * ⽤⼾未登录时返回的⽅法
  31.      */
  32.     public static Result unlogin(int code,String msg,Object data){
  33.         Result result = new Result();
  34.         result.setCode(Constant.UNLOGIN_CODE);
  35.         result.setMsg("用户未登录");
  36.         result.setData(data);
  37.         return result;
  38.     }
  39. }
复制代码
  3.2.2. 统⼀返回结果

  1. package com.example.spring_blog_24_9_8.config;
  2. import com.example.spring_blog_24_9_8.model.Result;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import lombok.SneakyThrows;
  5. import org.springframework.core.MethodParameter;
  6. import org.springframework.http.MediaType;
  7. import org.springframework.http.server.ServerHttpRequest;
  8. import org.springframework.http.server.ServerHttpResponse;
  9. import org.springframework.web.bind.annotation.ControllerAdvice;
  10. import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
  11.     @ControllerAdvice
  12.     public class ResponseAdvice implements ResponseBodyAdvice {
  13.        //哪个接口执行统一结果返回
  14.         @Override
  15.         public boolean supports(MethodParameter returnType, Class converterType) {
  16.             return true;
  17.       }
  18.         @SneakyThrows
  19.         @Override
  20.         //统一结果返回的具体逻辑
  21.         public Object beforeBodyWrite(Object body, MethodParameter returnType,
  22.                       MediaType selectedContentType, Class selectedConverterType,
  23.                        ServerHttpRequest request, ServerHttpResponse response) {
  24.                if (body instanceof Result){
  25.                        return body;
  26.                 }
  27.                  //对String 类型单独处理
  28.                 if (body instanceof String){
  29.                     ObjectMapper mapper = new ObjectMapper();
  30.                     return mapper.writeValueAsString(Result.success(body));
  31.                 }
  32.                 return Result.success(body);
  33.             }
  34.     }
复制代码
  3.3.3. 统⼀非常处理
  1. @ControllerAdvice
  2. @ResponseBody
  3. public class ExceptionAdvice {
  4.     @ExceptionHandler(Exception.class)
  5.     public Result  exceptionAdvice(Exception e){
  6.         return Result.fail(-1,e.getMessage());
  7.     }
  8. }
复制代码
ps:本文就到这里结束了,该项目未完待续,谢谢观看;

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

天津储鑫盛钢材现货供应商

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表