实训感受
1、今天实现了新闻系统的前端部分:新闻系统首页、标签、分页。代码量不是很大,而且很多部分是相似的,实现成功的时候感觉很惊喜。
2、第一次做前后端分离的系统,对springboot的了解又加深了一些。尤其是userDao层的NewsDao让我印象很深刻,但是现在还不是很理解那些sql语句…
新闻系统项目编码
controller层
ArchivesController
@Controller
public class ArchivesController {
@Autowired
private NewsService newsService;
@RequestMapping("archives")
public String archives(Model model){
HashMap<String, List<News>> map=newsService.archiveNews();
Long count=newsService.countNews();
model.addAttribute("newsCount",count);
model.addAttribute("archiveMap",map);
return "archives";
}
}
TagShowController
@Controller
public class TagShowController {
@Autowired
private TagService tagService;
@Autowired
private NewsService newsService;
@RequestMapping("/tags/{id}")
public String tag(@PageableDefault(size = 5,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
@PathVariable Long id, Model model){
List<Tag> tags=tagService.findTop(7);
if(id==-1){
id=tags.get(0).getId();
}
Page<News> page=newsService.searchNews(pageable,id);
model.addAttribute("page",page);
model.addAttribute("tags",tags);
return "tags";
}
}
TypeShowController
@Controller
public class TypeShowController {
@Autowired
private TypeService typeService;
@Autowired
private NewsService newsService;
@RequestMapping("/types/{id}")
public String types(@PageableDefault(size = 5,sort = {"updateTime"},direction = Sort.Direction.DESC)Pageable pageable,
@PathVariable Long id, Model model){
List<Type> types=typeService.findTop(7);
if(id==-1){
id=types.get(0).getId();
}
NewsQuery newsQuery=new NewsQuery();
newsQuery.setTypeId(id.toString());
Page<News> page=newsService.searchNews(pageable,newsQuery);
model.addAttribute("page",page);
model.addAttribute("types",types);
return "types";
}
}
TagDao
public interface TagDao extends JpaRepository<Tag,Long> {
@Query("select t from Tag t")
List<Tag> findTop(Pageable pageable);
}
service层
NewsServiceImpl
public HashMap<String, List<News>> archiveNews() {
LinkedHashMap<String,List<News>> map=new LinkedHashMap<>();
List<String> years=newsDao.findGroupYear();
for(String y:years){
List<News> news = newsDao.findByYear(y);
map.put(y,news);
}
return map;
}
新闻项目前端部分实现效果
主页面