【SpringBoot搭建个人博客】- 博客首页显示(十,看完你还觉得算法不重要

本文介绍了一个博客管理系统的数据库设计与业务逻辑实现,包括SQL查询、分页显示、搜索功能及统计数据等核心模块。通过具体示例展示了如何进行博客信息的检索与展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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. 运行访问

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值