博客系统 - 博客发布
第1关:前端参数获取(方法一)
Step1Controller.java
package net.educoder.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
@Controller
public class Step1Controller {
/**
* 基本类型参数绑定
*
* @param blogId 博客ID(long)
* @param blogTitle 博客标题(String)
* @param blogContent 博客正文(String)
* @param userId 用户ID(long)
* @param typeId 类型ID(long)
* @param blogStatus 博客状态(long)
* @param createTime 创建时间(String)
* @param updateTime 更新时间(String)
* @param coverImage 封面图片路径(String)
* @return JSON
*/
@RequestMapping("/step1")
@ResponseBody
/********** Begin **********/
public Map<String, Object> blogValueToJson(long blogId, String blogTitle, String blogContent, long userId, long typeId, long blogStatus, String createTime, String updateTime, String coverImage) {
Map<String, Object> map = new HashMap<>();
map.put("blogId", blogId);
map.put("blogTitle", blogTitle);
map.put("blogContent", blogContent);
map.put("userId", userId);
map.put("typeId", typeId);
map.put("blogStatus", blogStatus);
map.put("createTime", createTime);
map.put("updateTime", updateTime);
map.put("coverImage", coverImage);
return map;
}
/********** End **********/
}
第2关:前端参数获取(方法二)
Step2Controller.java
package net.educoder.controller;
import net.educoder.entity.TBlog;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
@Controller
public class Step2Controller {
/**
* @param blog 博客对象
* @return 返回JSON对象
* <p>
* 案例如下:
* {"data":{"blogId":1,"blogTitle":"博客系统","blogContent":null,"userId":1,"typeId":1,"blogStatus":1,"createTime":"2018-04-28 08:55:55","updateTime":"2018-04-28 08:55:55","coverImage":"/pic/sad1wda3ddsac.jpg"}}
*/
@RequestMapping("/step2")
@ResponseBody
/********** Begin **********/
public Map<String, Object> blogValueToJson(TBlog blog) {
Map<String, Object> map = new HashMap<>();
map.put("data", blog);
return map;
}
/********** End **********/
}
第3关:创建博客
BlogMapper.java
package net.educoder.mapper;
import net.educoder.entity.TBlog;
import net.educoder.entity.TUser;
import org.apache.ibatis.annotations.*;
@Mapper
public interface BlogMapper {
/********** Begin **********/
/**
* 创建博客
* <p>
* 表名:t_blog
* <p>
* blogId:博客ID(主键自增)
* blogTitle:博客标题
* blogContent:博客内容
* userId:用户ID
* typeId:类型ID
* blogStatus:博客状态
* createTime:创建时间
* updateTime:更新时间
*
* @param blog 博客对象
*/
@Insert("insert into t_blog (blogTitle, blogContent, userId, typeId, blogStatus, createTime, updateTime) values(#{blogTitle}, #{blogContent}, #{userId}, #{typeId}, #{blogStatus}, #{createTime}, #{updateTime})")
@Options(useGeneratedKeys = true, keyProperty = "blogId")
void addBlog(TBlog blog);
/**
* 博客与标签绑定
* <p>
* 表名:t_tag_blog
* <p>
* tagId:标签ID
* blogId:标签ID
*
* @param blogId 博客ID
* @param tagId 标签ID
*/
@Insert("insert into t_tag_blog values (#{tagId}, #{blogId})")
void addBlogAndTag(@Param("blogId") long blogId, @Param("tagId") long tagId);
/********** End **********/
@Select("select * from t_blog where blogId = #{blogId} ")
TBlog findBlogDetail(String blogId);
@Select("select * from t_user where userId = #{userId}")
TUser findUserInfoByUserId(long userId);
}
BlogServiceImpl.java
package net.educoder.service.impl;
import net.educoder.entity.TBlog;
import net.educoder.entity.TUser;
import net.educoder.mapper.BlogMapper;
import net.educoder.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Service
@Transactional
public class BlogServiceImpl implements BlogService {
/**
* BlogMapper 对象注入
*/
@Autowired
private BlogMapper mapper;
/**
* 创建博客
*
* @param blog 博客对象
* @param tagId 标签ID
* @return 返回博客ID
*/
@Override
public long addBlog(TBlog blog, String tagId) {
/********** Begin **********/
//1.设置创建时间与更新时间
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = format.format(date);
blog.setCreateTime(dateStr);
blog.setUpdateTime(dateStr);
//2.设置为发布状态为 1 ,状态为已发布状态
blog.setBlogStatus(1);
//3.调用 BlogMapper 的创建博客方法
mapper.addBlog(blog);
//4.将当前博客与标签进行绑定
long blogId = blog.getBlogId();
mapper.addBlogAndTag(blogId, Long.parseLong(tagId));
return blogId;
/********** End **********/
}
@Override
public Map<String, Object> findBlogDetail(String BlogId) {
Map<String, Object> map = new HashMap<>();
TBlog blog = mapper.findBlogDetail(BlogId);
long userId = blog.getUserId();
TUser user = mapper.findUserInfoByUserId(userId);
map.put("blogData", blog);
map.put("userInfo", user);
return map;
}
}
Step3Controller.java
package net.educoder.controller;
import net.educoder.entity.TBlog;
import net.educoder.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("blog")
public class Step3Controller {
/**
* BlogService 对象注入
*/
@Autowired
BlogService blogService;
/**
* 创建博客
*
* @param blog 博客对象
* @param tagId 标签ID
* @return 返回JSON对象
* <p>
* 案例如下:
* {"code":200,"blogId":"xxx"}
* <p>
* code:200 --> 200 表示创建成功
* blogId:xxx --> xxx 表示当前博客ID
*/
@RequestMapping("/createBlog")
@ResponseBody
public Map<String, Object> createBlog(TBlog blog, String tagId) {
Map<String, Object> map = new HashMap<>();
/********** Begin **********/
long blogId = blogService.addBlog(blog, tagId);
map.put("code", 200);
map.put("blogId", blogId);
/********** End **********/
return map;
}
}
第4关:封面上传
Step4Controller.java
package net.educoder.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("blog")
public class Step4Controller {
/**
* 封面上传
*
* @param file 文件对象
* @return 返回JSON对象,图片上传成功,返回以下JSON
* <p>
* 例如:
* <p>
* {
* code:"1",
* url:"/picture/图片名称.jpg"
* }
*/
@PostMapping("/uploadImg")
@ResponseBody
public Map<String, Object> singleFileUpload(@RequestParam("file") MultipartFile file) throws Exception {
Map<String, Object> map = new HashMap<>();
if (file.isEmpty()) {
map.put("code", 0);
return map;
}
/********** Begin **********/
String fileName = file.getOriginalFilename();
file.transferTo(new File("/root/img/" + fileName));
map.put("code", 1);
map.put("url", "/picture/" + fileName);
/********** End **********/
return map;
}
}
WebConfig.java
package net.educoder.conf;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
/********** Begin **********/
registry.addResourceHandler("/picture/**").addResourceLocations("file:/root/img/");
/********** End **********/
}
}
第5关:MarkDown 图片上传
Step5Controller.java
package net.educoder.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("blog")
public class Step5Controller {
/**
* MarkDown 图片上传
*
* @param file 文件对象
* @return 返回JSON对象,格式如下:
* <p>
* {
* success : 0 | 1, // 0 表示上传失败,1 表示上传成功
* message : "上传成功",
* url : "图片地址" // 上传成功时才返回
* }
* <p>
* <p>
* 图片上传成功,返回以下JSON:
* <p>
* {
* success:1,
* message:"上传成功",
* url:"/picture/图片名称.jpg"
* }
*/
@RequestMapping("/uploadMdImg")
@ResponseBody
public Map<String, Object> editormdPic(@RequestParam(value = "editormd-image-file", required = true) MultipartFile file) throws Exception {
Map<String, Object> map = new HashMap<>();
if (file.isEmpty()) {
map.put("code", 0);
return map;
}
/********** Begin **********/
String fileName = file.getOriginalFilename();
file.transferTo(new File("/root/img/" + fileName));
map.put("success", 1);
map.put("message", "上传成功");
map.put("url", "/picture/" + fileName);
/********** End **********/
return map;
}
}