源码获取:俺的博客首页 "资源" 里下载!
本系统是一个个人博客的管理系统,用户可以注册账号并登陆系统查看发布的文章并进行阅读评论等
本系统主要有三种用户:
1、游客角色主要功能:
没有登陆的用户,可以查询文章,但是无法进行评论,系统中的所有文章可以通过内容搜索,可以通过分类搜索也可以通过标签就行搜索
2、注册用户主要功能
可以对所有的文章进行阅读以及评论等功能,可以对文章进行赞赏(支付宝或者微信付钱)
3、管理员主要功能:
拥有注册用户的所有权限,管理分类以及发布文章和管理标签等,友链管理,时间轴管理等
1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。
2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;
3.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS;
4.数据库:MySql 5.7版本;
5.是否Maven项目:是;
SpringBoot+SpringSecurity+Mybatis+Mysql+Redis+Thymeleaf+JQuery
1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse导入项目,导入成功后请执行maven clean;maven install命令,然后运行;
3. 将项目中application.yml配置文件中的数据库配置改为自己的配置;
4. 运行项目,项目运行成功后,访问地址localhost:8080
管理员账号密码:[email protected]/mt2680324
用户账号密码:[email protected]/mt2680324
@Controller
@RequestMapping("/admin/blog")
public class AdminBlogController {
@Autowired
private BlogService blogService;
@Autowired
private TypeService typeService;
@Autowired
private TagService tagService;
@RequestMapping("/showAll")
public ModelAndView showAll(@RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize,
@RequestParam(name = "title", required = false, defaultValue = "") String title,
@RequestParam(name = "typeId", required = false, defaultValue = "0") Integer typeId,
@RequestParam(name = "recommend", required = false, defaultValue = "false") Boolean recommend) {
if (recommend == false) {
recommend = null;
}
ModelAndView mv = new ModelAndView ();
//展示所有博客前查询所有类别
List types = typeService.selectAll ();
mv.addObject ("types", types);
//调用业务层方法
List blogs = blogService.selectAll (pageNum, pageSize, title, typeId, recommend);
PageInfo pageInfo = new PageInfo<> (blogs, 5);
mv.addObject ("pageInfo", pageInfo);
mv.addObject ("title", title);
mv.addObject ("typeId", typeId);
mv.addObject ("recommend", recommend);
mv.setViewName ("admin/blogs");
return mv;
}
@RequestMapping("/showAll/search")
public String search(@RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize,
@RequestParam(name = "title", required = false, defaultValue = "") String title,
@RequestParam(name = "typeId", required = false, defaultValue = "0") Integer typeId,
@RequestParam(name = "recommend", required = false, defaultValue = "false") Boolean recommend,
Model model) {
if (recommend == false) {
recommend = null;
}
//调用业务层方法
List blogs = blogService.selectAll (pageNum, pageSize, title, typeId, recommend);
PageInfo pageInfo = new PageInfo<> (blogs, 5);
model.addAttribute ("pageInfo", pageInfo);
return "admin/blogs::blogList";
}
@RequestMapping("/toAdd")
public String toAdd(Model model) {
//添加博客页前展示所有类别
List types = typeService.selectAll ();
model.addAttribute ("types", types);
//添加博客页前展示所有标签
List tags = tagService.selectAll ();
model.addAttribute ("tags", tags);
return "admin/blogs-input";
}
@RequestMapping("/add")
public String add(Blog blog, @RequestParam(name = "tagIds", required = false) List tagIds, RedirectAttributes attributes) {
blog.setViews (0);
blog.setCreateTime (new Date ());
blog.setUpdateTime (new Date ());
blog.setCommentCount (0);
blog.setTags (getTags (tagIds));
//获取用户id
UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext ().getAuthentication ().getPrincipal ();
blog.setUserInfo (new UserInfo (userInfo.getId ()));
//封装完成,调用service层保存
Boolean save = blogService.save (blog);
if (save) {
attributes.addFlashAttribute ("message", "添加成功!");
} else {
attributes.addFlashAttribute ("message", "添加失败!");
}
return "redirect:showAll";
}
public static List getTags(List tagIds) {
List tags = new ArrayList<> ();
for (Integer tagId : tagIds) {
tags.add (new Tag (tagId));
}
return tags;
}
@RequestMapping("/toUpdate")
public String toUpdate(Long id, Model model) {
Blog blog = blogService.selectById (id);
model.addAttribute ("blog", blog);
List types = typeService.selectAll ();
model.addAttribute ("types", types);
List tags = tagService.selectAll ();
model.addAttribute ("tags", tags);
//用于展示标签
String tagIds = "";
List tagList = blog.getTags ();
if (tagList.size () == 0 || tagList == null) {
tagIds = tagIds;
} else {
for (Tag tag : tagList) {
tagIds += "," + tag.getId ();
}
tagIds = tagIds.substring (1);
}
model.addAttribute ("tagIds", tagIds);
return "admin/blogs-update";
}
@RequestMapping("/update")
public String update(Blog blog, @RequestParam(name = "tagIds", required = false) List tagIds, RedirectAttributes attributes) {
blog.setUpdateTime (new Date ());
blog.setTags (getTags (tagIds));
//获取用户id
UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext ().getAuthentication ().getPrincipal ();
blog.setUserInfo (new UserInfo (userInfo.getId ()));
Boolean update = blogService.update (blog);
if (update) {
attributes.addFlashAttribute ("message", "修改成功!");
} else {
attributes.addFlashAttribute ("message", "修改失败!");
}
return "redirect:showAll";
}
@RequestMapping("/deleteById")
public String deleteById(Long id, RedirectAttributes attributes) {
Boolean aBoolean = blogService.deleteById (id);
if (aBoolean) {
attributes.addFlashAttribute ("message", "删除成功!");
} else {
attributes.addFlashAttribute ("message", "删除失败!");
}
return "redirect:showAll";
}
}
@Controller
@RequestMapping("/front/comment")
public class FrontCommentController {
@Autowired
private CommentService commentService;
@RequestMapping("/addComment")
public String addComment(Comment comment){
//获取登录的用户
UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext ().getAuthentication ().getPrincipal ();
comment.setUserInfo (userInfo);
comment.setCreateTime (new Date ());
if (comment.getParentComment ().getId () == -1){
comment.getParentComment ().setId (null);
}
commentService.saveComment (comment);
return "redirect:showAllCommentByBlogId?id="+comment.getBlog ().getId ();
}
@RequestMapping("/showAllCommentByBlogId")
public String showAllCommentByBlogId(@RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(name = "pageSize", required = false, defaultValue = "6") Integer pageSize,
@RequestParam(name = "id") Long id, Model model){
List comments = commentService.selectByBlogIdAndParentCommentIsNull (pageNum, pageSize, id);
PageInfo pageInfo = new PageInfo<> (comments,8);
CommentServiceImpl commentServiceImpl = (CommentServiceImpl) commentService;
List commentList = commentServiceImpl.getAll (pageInfo.getList ()); //获取后代评论
pageInfo.setList (commentList); //封装好的顶级评论和后代评论设置给pageInfo的list
model.addAttribute ("pageInfo",pageInfo);
return "blog :: commentList";
}
}
@Controller
@RequestMapping("/front/message")
public class FrontMessageController {
@Autowired
private MessageService messageService;
@RequestMapping("/messages")
public String messages(@RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(name = "pageSize", required = false, defaultValue = "6") Integer pageSize,
Model model) {
showAll (pageNum, pageSize, model);
return "messages";
}
public void showAll(Integer pageNum, Integer pageSize, Model model){
List messages = messageService.selectAll (pageNum, pageSize);
PageInfo pageInfo = new PageInfo<> (messages, 8);
MessageServiceImpl messageServiceImpl = (MessageServiceImpl) messageService;
pageInfo.setList (messageServiceImpl.getAll (pageInfo.getList ()));
model.addAttribute ("pageInfo", pageInfo);
}
@RequestMapping("/addMessage")
public String addMessage(Message message) {
UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext ().getAuthentication ().getPrincipal ();
message.setUserInfo (userInfo);
message.setCreateTime (new Date ());
if (message.getParentMessage ().getId () == -1) {
message.getParentMessage ().setId (null);
}
messageService.saveMessage (message);
return "redirect:showAllMessages";
}
@RequestMapping("/showAllMessages")
public String showAllMessages(@RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(name = "pageSize", required = false, defaultValue = "6") Integer pageSize,
Model model) {
showAll (pageNum, pageSize, model);
return "messages :: messageList";
}
}
源码获取:俺的博客首页 "资源" 里下载!