select b.id,b.title,b.first_picture, b.views, b.comment_count,b.create_time,b.update_time,b.description,
t.name ,
u.nickname, u.avatar
from myblog.t_blog b, myblog.t_type t,myblog.t_user u
where b.type_id = t.id and u.id = b.user_id order by b.create_time desc
select * from myblog.t_blog where t_blog.recommend = true order by t_blog.create_time desc limit 4;
select b.id,b.title,b.first_picture, b.views,b.comment_count,b.update_time,b.description,
t.name ,
u.nickname, u.avatar
from myblog.t_blog b, myblog.t_type t,myblog.t_user u
where b.type_id = t.id and u.id = b.user_id and (b.title like #{pattern} or b.content like #{pattern})
order by b.update_time desc
select count(*) from myblog.t_blog
select coalesce (sum(views),0) from myblog.t_blog
select count(*) from myblog.t_comment
select count(*) from myblog.t_message
讲解:
查询首页最新博客列表信息和查询推荐文章都是前面提到过的知识点,搜索文章也在博客管理里面有讲解过,用的是模糊查询,这里说一统计访问总数的SQL,在上一版的代码中,用的是:select sum(views) from myblog.t_blog,这里用的是:select coalesce (sum(views),0) from myblog.t_blog,在上一版中,当sum求和返回为null时,是会报空指针异常的,这里用coalesce (sum(views),0),打当sum求和为null时赋值为0,就能解决这个问题
4. 业务层
业务层接口
在BlogService接口中定义以下接口
//查询首页最新博客列表信息
List getAllFirstPageBlog();
//查询首页最新推荐信息
List getRecommendedBlog();
//搜索博客列表
List getSearchBlog(String query);
//统计博客总数
Integer getBlogTotal();
//统计访问总数
Integer getBlogViewTotal();
//统计评论总数
Integer getBlogCommentTotal();
//统计留言总数
Integer getBlogMessageTotal();
接口实现类
在BlogServiceImpl接口实现类中添加:
//查询首页最新博客列表信息
@Override
public List getAllFirstPageBlog() {
return blogDao.getFirstPageBlog();
}
//查询首页最新推荐信息
@Override
public List getRecommendedBlog() {
List allRecommendBlog = blogDao.getAllRecommendBlog();
return allRecommendBlog;
}
//搜索博客列表
@Override
public List getSearchBlog(String query) {
return blogDao.getSearchBlog(query);
}
//统计博客总数
@Override
public Integer getBlogTotal() {
return blogDao.getBlogTotal();
}
//统计访问总数
@Override
public Integer getBlogViewTotal() {
return blogDao.getBlogViewTotal();
}
//统计评论总数
@Override
public Integer getBlogCommentTotal() {
return blogDao.getBlogCommentTotal();
}
//统计留言总数
@Override
public Integer getBlogMessageTotal() {
return blogDao.getBlogMessageTotal();
}
5. 控制器
在controller包下创建IndexController类,根据前面的功能分析,编写如下代码:
package com.star.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.star.queryvo.FirstPageBlog;
import com.star.queryvo.RecommendBlog;
import com.star.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.util.List;
/**
-
@Description: 首页控制器
-
@Date: Created in 21:01 2020/5/20
-
@Author: ONESTAR
-
@QQ群: 530311074
-
@URL: https://onestar.newstar.net.cn/
*/
@Controller
public class IndexController {
@Autowired
private BlogService blogService;
//分页查询博客列表
@GetMapping("/")
public String index(Model model, @RequestParam(defaultValue = “1”,value = “pageNum”) Integer pageNum, RedirectAttributes attributes){
PageHelper.startPage(pageNum,10);
List allFirstPageBlog = blogService.getAllFirstPageBlog();
List recommendedBlog = blogService.getRecommendedBlog();
PageInfo pageInfo = new PageInfo<>(allFirstPageBlog);
System.out.println(“pageInfo:” +pageInfo);
model.addAttribute(“pageInfo”,pageInfo);
model.addAttribute(“recommendedBlogs”, recommendedBlog);
return “index”;
}
//搜索博客
@PostMapping("/search")
public String search(Model model,
@RequestParam(defaultValue = “1”, value = “pageNum”) Integer pageNum,
@RequestParam String query) {
PageHelper.startPage(pageNum, 1000);
List searchBlog = blogService.getSearchBlog(query);
PageInfo pageInfo = new PageInfo<>(searchBlog);
model.addAttribute(“pageInfo”, pageInfo);
model.addAttribute(“query”, query);
return “search”;
}
//博客信息统计
@GetMapping("/footer/blogmessage")
public String blogMessage(Model model){
int blogTotal = blogService.getBlogTotal();
int blogViewTotal = blogService.getBlogViewTotal();
int blogCommentTotal = blogService.getBlogCommentTotal();
int blogMessageTotal = blogService.getBlogMessageTotal();
model.addAttribute(“blogTotal”,blogTotal);
model.addAttribute(“blogViewTotal”,blogViewTotal);
model.addAttribute(“blogCommentTotal”,blogCommentTotal);
model.addAttribute(“blogMessageTotal”,blogMessageTotal);
return “index :: blogMessage”;
}
}
6. 前后端交互
- 最新推荐:

大圣,此去欲何?
- 文章列表
大圣,此去欲何?
戴上金箍,没法爱你;放下金箍,没法保护你。我知道上天不会给我第二次机会,曾经我们说好的永远,原来也仅仅只有,十二画,而已。“大圣,此去欲何?”“踏南天,碎凌霄。”“若一去不回……”“便一去不回” 其实很多时候,我们都是有机会的,最后真正放弃的,是我们自己。......

2020-01-01
2222
2222

- 分页显示文章列
/
- 搜索博客
- 统计博客信息
HTML:
文章总数:
14
篇访问总数:
14
次评论总数:
14
条留言总数:
14
条JS:
$(’#blog-message’).load(/*[[@{/fo
【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
oter/blogmessage}]]*/"/footer/blogmessage");
7. 运行访问