基于 Spring Boot 博客系统开发(八)
本系统是简易的个人博客系统开发,为了更加熟练地掌握 SprIng Boot 框架及相关技术的使用。🌿🌿🌿
基于 Spring Boot 博客系统开发(七)👈👈
仪表盘实现效果
显示文章总数、评论总数、最新文章和最新留言。实现步骤,首先后端获取文章评论相关数据,然后前端使用thymeleaf获取后端model中的数据进行渲染。
后台首页 AdminController
获取最新文章列表、最新评论列表和page对象
@Controller @RequestMapping("/admin") public class AdminController { @Autowired private IArticleService articleService; @Autowired private ICommentService commentService; /** * 后台首页 */ @RequestMapping("/") public String home(Model model){ //int articleTotal = articleService.count(); //int commentTotal = commentService.count(); PageHelper.startPage(1,5); ListlatestArticleVOList = articleService.selectLatestArticle(); PageInfo articlePage = new PageInfo (latestArticleVOList); PageHelper.startPage(1,5,"created desc"); List commentList = commentService.list(); PageInfo commentPage = new PageInfo<>(commentList); model.addAttribute("articlePage",articlePage); model.addAttribute("commentPage",commentPage); return "admin/index"; } @RequestMapping("/list") public String list(){ return "admin/list"; } @RequestMapping("/edit") public String edit(){ return "admin/edit"; } }
创建VO对象,LatestArticleVO。实体对象不满足所需渲染属性的情况下,创建自定义属性视图对象,
@Data public class LatestArticleVO { private Long id; private String title; private Integer hits; }
编写最新文章列表的SQL、Mapper、service。首先,在Mapper中自定义查询SQL
ArticleMapper.xml
ArticleMapper,方法名selectLatestArticle与上面select标签id属性的名一致
@Mapper public interface ArticleMapper extends BaseMapper{ List selectHotArticle(); List selectLatestArticle(); }
IArticleService,创建service接口类
public interface IArticleService extends IService{ public List selectHotArticle(); List selectLatestArticle(); }
ArticleServiceImpl,调用mapper层方法
@Service public class ArticleServiceImpl extends ServiceImplimplements IArticleService { @Autowired private ArticleMapper articleMapper; @Override public List selectHotArticle() { return articleMapper.selectHotArticle(); } @Override public List selectLatestArticle() { return articleMapper.selectLatestArticle(); } }
后台首页内容前端代码实现
使用thymeleaf模板引擎渲染
仪表盘
发表了12篇文章收到了10条留言最新文章
最新留言
- [[${comment.author}]]于 [[${#dates.format(comment.created,'YYYY-MM-dd')}]]:
151235
还没有评论,来说两句吧...