springboot新闻首页+搜索+详情页

本文介绍了如何使用Spring Boot构建新闻应用的三个主要功能:新闻首页,搜索功能和详情页。在新闻首页部分,通过NewRepository查询推荐新闻,TypeRepository和TagRepository获取最多类型的新闻和最热门标签。Service和Impl层实现了对应的业务逻辑。前端页面进行了相应的调整。搜索功能在NewRepository中添加了按标题和简介查询的接口,并在IndexController中实现。详情页通过工具类处理新闻内容的格式,提供完整新闻展示。

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

一、新闻首页

在NewRepository中添加接口(按照是否推荐进行查询):

    @Query("select n from News n where n.recommend = true")
    List<News> findTop(Pageable p);

1.在NewService中添加接口,并在Impl中实现
1.1接口:

    //主页显示新闻列表
    Page<News> listNew(Pageable pageable);
    //主页推荐最新新闻列表
    List<News> listRecommendNewTop(Integer size);

1.2实现:

    @Override
    public Page<News> listNew(Pageable pageable) {
   
        return newRepository.findAll(pageable);
    }
    
    @Override
    public List<News> listRecommendNewTop(Integer size) {
   
        Sort sort = Sort.by(Sort.Direction.DESC,"updateTime");
        Pageable pageable = PageRequest.of(0,size,sort);
        return newRepository.findTop(pageable);
    }

2.在TypeRepository类中添加接口(查询类型最多的新闻种类),在TagRepository类中添加接口(查询标签数目最多的新闻)
TypeRepository:

    @Query("select t from Type t")
    List<Type> findTop(Pageable pageable);

TagRepository:

    @Query("select t from Tag t")
    List<Tag> findTop(Pageable p);

3.在Service和Impl中写入与上面数据库查询相对应的逻辑实现
TypeService接口

 List<Type> listTypeTop(Integer size);

TypeServiceImpl实现

    @Override
    public List<Type> listTypeTop(Integer size) {
   
        Sort sort = Sort.by(Sort.Direction.DESC,"news.size");
        Pageable pageable = PageRequest.of(0,size,sort)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值