编写归档
按照博客的年份来分类博客
IndexController
@GetMapping("archives")
public String archives(Model model){
Long count=blogService.countBlog();//获取总数
Map<String,List<Blog>> blogs=blogService.archivesBlog();//查询博客
model.addAttribute("blogCount",count);
model.addAttribute("archiveMap",blogs);
return "archives";
}
IBlogService
Long countBlog();
Map<String, List<Blog>> archivesBlog();
BlogServiceImpl
@Override
public Long countBlog() {
return blogDao.count();
}
@Override
public Map<String, List<Blog>> archivesBlog() {
List<String> year = blogDao.findGroupYear();
Map<String, List<Blog>> map=new HashMap<>();
for(String y:year){
List<Blog> blogs=blogDao.findByYear(y);
map.put(y,blogs);
}
return map;
}
BlogDao
在BlogDao中写 findGroupYear()、findByYear(String y)根据年份查找、根据年份分组的方法。
@Query("select function('date_format',b.updateTime,'%Y') as year from Blog b group by year order by year desc")
List<String> findGroupYear();
@Query("select b from Blog b where function('date_format',b.updateTime,'%Y') = ?1")
List<Blog> findByYear(String y);
以新增19年test为例
关于我
IndexController
@RequestMapping("about")
public String about(){
return "about";
}