首页&分类&标签
1、查找前n条type/tag&显示出每条type/tag下新闻的条数
dao层
(1)在dao下的TypeDao接口中,创建findTop方法,查找前n条type
public interface TypeDao extends JpaRepository<Type,Long> {
@Query("select t from Type t")
List<Type> findTop(Pageable pageable);
}
(2)在dao下的TagDao接口中,创建findTop方法,查找前n条tag
public interface TagDao extends JpaRepository<Tag,Long> {
@Query("SELECT t from Tag t")
List<Tag> findTop(Pageable pageable);
}
service层
(1)在service下的
TypeService接口中,声明findTop方法
List<Type> findTop(int i);
TagService接口中,声明findTop方法
List<Tag> findTop(int i);
(2)在service下的impl中
实现TypeService接口中声明的findTop方法:
将type,按某条type下新闻的多少进行降序排列,再调用typeDao的findTop方法取前n条type
@Override
public List<Type> findTop(int i) {
Sort sort=Sort.by(Sort.Direction.DESC,"newsList.size");
Pageable pageable = PageRequest.of(0, i, sort);
return typeDao.findTop(pageable);
}
实现TagService接口中声明的findTop方法:
将tag,按某条tag下新闻的多少进行降序排列,再调用tagDao的findTop方法取前n条tag
@Override
public List<Tag> findTop(int i) {
Sort sort=Sort.by(Sort.Direction.DESC,"newsList.size");
Pageable pageable = PageRequest.of(0, i, sort);
return tagDao.findTop(pageable);
}
controller层
首页
使用内置的Pageable来实现分页,将news,按news的更新时间进行降序排列,再取前5条news
调用typeService的findTop方法,取前5条type
调用tagService的findTop方法,取前5条tag
将相关信息通过model传到前台
@Controller
@RequestMapping
public class IndexController {
@Autowired
private NewsServcie newsServcie;
@Autowired
private TypeService typeService;
@Autowired
private TagService tagService;
@RequestMapping
public String index(@PageableDefault(size = 5, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,Model model){
Page<News> page = newsServcie.findByPageable(pageable);
List<Type> types=typeService.findTop(5);
List<Tag> tags=tagService.findTop(5);
model.addAttribute("page", page);
model.addAttribute("types",types);
model.addAttribute("tags",tags);
return "index";
}
}
界面
index.html
遍历types对象,显示前n条type的name,并显示出每条type下新闻的条数:
<a href="#" th:href="@{/types/{id}(id=${type.id})}" target="_blank" class="item" th:each="type : ${types}" >
<span th:text="${type.name}" >学习日志</span>
<div class="ui teal basic left pointing label" th:text="${#arrays.length(type.newsList)}" >13</div>
</a>
遍历tags对象,显示前n条tag的name,并显示出每条tag下新闻的条数:
<a href="#" target="_blank" th:href="@{/tags/{id}(id=${tag.id})}" class="ui teal basic left pointing label m-margin-tb-tiny" th:each="tag:${tags}" >
<span th:text="${tag.name}" >方法论</span> <div class="detail" th:text="${#arrays.length(tag.newsList)}">23</div>
</a>
2、搜索type/tag下对应的新闻页面
service层
(1)在service下的NewsService接口中
声明searchNews(Pageable pageable, NewsQuery newsQuery)方法
Page<News> searchNews(Pageable pageable, NewsQuery newsQuery);
声明searchNews(Pageable pageable, Long id)方法
Page<News> searchNews(Pageable pageable, Long id);
(2)在service下的impl中
实现NewsService接口中声明的searchNews(Pageable pageable, NewsQuery newsQuery)方法:
根据title、typeId、recommend进行查询并以此new一个Specification
再以new出来的Specification以及pageable作为参数调用dao层的findAll方法完成搜索
@Override
public Page<News> searchNews(Pageable pageable, NewsQuery newsQuery) {
Page<News> news=newsDao.findAll(new Specification<News>() {
@Override
public Predicate toPredicate(Root<News> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
List<Predicate> predicates=new ArrayList<>();
if(!StringUtils.isEmpty(newsQuery.getTitle())){
predicates.add(criteriaBuilder.like(root.<String>get("title"),"%"+newsQuery.getTitle()+"%"));
}
if(!StringUtils.isEmpty(newsQuery.getTypeId())){
predicates.add(criteriaBuilder.equal(root.<Type>get("type").get("id"),newsQuery.getTypeId()));
}
if(!StringUtils.isEmpty(newsQuery.getRecommend())){
predicates.add(criteriaBuilder.equal(root.<Boolean>get("recommend"),newsQuery.getRecommend()));
}
query.where(predicates.toArray(new Predicate[predicates.size()]));
return null;
}
},pageable);
return news;
}
实现NewsService接口中声明的searchNews(Pageable pageable, Long id)方法:
根据tag的id进行查询并以此new一个Specification
再以new出来的Specification以及pageable作为参数调用dao层的findAll方法完成搜索
@Override
public Page<News> searchNews(Pageable pageable, Long id) {
return newsDao.findAll(new Specification<News>() {
@Override
public Predicate toPredicate(Root<News> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
Join join=root.join("tags");
return criteriaBuilder.equal(join.get("id"),id);
}
}, pageable);
}
controller层
分类
使用内置的Pageable来实现分页,将news,按news的更新时间进行降序排列,再取前5条news
调用typeService的findTop方法,取前7条type
如果id为-1,调用type的getId方法,默认将id赋值为排序后第一条type的id
调用newsServcie的searchNews方法,把type的id传入newsQuery,再将newsQuery作为参数之一进行搜索,得到某种type下对应的新闻页面
将相关信息通过model传到前台
@Controller
public class TypeShowController {
@Autowired
private TypeService typeService;
@Autowired
private NewsServcie newsServcie;
@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 = newsServcie.searchNews(pageable, newsQuery);
model.addAttribute("page",page);
model.addAttribute("types",types);
return "types";
}
}
标签
使用内置的Pageable来实现分页,将news,按news的更新时间进行降序排列,再取前5条news
调用tagService的findTop方法,取前7条tag
如果id为-1,调用tag的getId方法,默认将id赋值为排序后第一条tag的id
调用newsServcie的searchNews方法,根据tag的id进行搜索,得到某种tag下对应的新闻页面
将相关信息通过model传到前台
@Controller
public class TagShowController {
@Autowired
private TagService tagService;
@Autowired
private NewsServcie newsServcie;
@RequestMapping("/tags/{id}")
public String tags(@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=newsServcie.searchNews(pageable,id);
model.addAttribute("page",page);
model.addAttribute("tags",tags);
return "tags";
}
}
归档
dao层
在dao中下的NewsDao接口中
创建findGroupYear方法:给新闻的年份分组,并降序排列,返回分组排序后的年份列表
创建findByYear方法:查找某年份对应的新闻
public interface NewsDao extends JpaRepository<News,Long> , JpaSpecificationExecutor<News> {
@Query("select function('date_format',n.updateTime,'%Y') as year from News n group by function('date_format',n.updateTime,'%Y') order by year desc ")
List<String> findGroupYear();
@Query("select n from News n where function('date_format',n.updateTime,'%Y') = ?1")
List<News> findByYear(String y);
}
service层
(1)在service中下的NewsService接口中,声明countNews方法、archiveNews方法
Long countNews();
HashMap<String, List<News>> archiveNews();
(2)在service下的impl
实现NewsService接口中声明的countNews方法,得到新闻总条数
@Override
public Long countNews() {
return newsDao.count();
}
实现NewsService接口中声明的archiveNews方法,根据年份给新闻归档
调用newsDao的findGroupYear方法,得到新闻的年份
遍历所有的年份,调用newsDao的findByYear方法,得到该年份的新闻列表,再把该年份与对应新闻列表放进map中
@Override
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;
}
controller层
调用newsServcie的countNews方法,得到新闻总条数
调用newsServcie的archiveNews方法,根据年份给新闻归档
将相关信息通过model传到前台
@Controller
public class ArchivesController {
@Autowired
private NewsServcie newsServcie;
@RequestMapping("archives")
public String arvhives(Model model){
HashMap<String, List<News>> map=newsServcie.archiveNews();
Long count=newsServcie.countNews();
model.addAttribute("newsCount",count);
model.addAttribute("archiveMap",map);
return "archives";
}
}
运行结果
首页
分类
标签
归档