7.29第九次实训

实训感受

1、今天实现了新闻的新增,编辑。感觉有点难的,可能是刚接触,感觉很陌生,但也是挺实用的。
2、搜索难度比较大
课堂项目编码

controller层

package com.zr.controller;

import com.zr.po.*;
import com.zr.service.NewsService;
import com.zr.service.TagService;
import com.zr.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin/news")
public class NewsController {
    @Autowired
    private NewsService newsService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;

    @RequestMapping
    public String list(@PageableDefault(size = 5,sort={"updateTime"},direction = Sort.Direction.DESC)Pageable pageable, Model model){
        Page<News> page=newsService.findByPageable(pageable);
        model.addAttribute("page",page);
        model.addAttribute("types",typeService.listType());
        return "admin/news";
    }

    @RequestMapping("input/{id}")
    public String toInput(@PathVariable Long id,Model model){
        if(id==-1){
            model.addAttribute("news",new News());
        }else{
            News news=newsService.findNewsById(id);
            String tagIds=tagService.getTagIds(news.getTags());
            news.setTagIds(tagIds);
            model.addAttribute("news",news);
        }
        List<Type> types=typeService.listType();
        model.addAttribute("types",types);
        model.addAttribute("tags",tagService.listTag());
        return "admin/news-input";
    }

    @RequestMapping("input")
    public String input(News news, HttpSession session){
        User user=(User)session.getAttribute("user");
        news.setUser(user);
        List<Tag>tags=tagService.findTagByTagId(news.getTagIds());
        news.setTags(tags);
        newsService.input(news);
        return "redirect:/admin/news";
    }

    @RequestMapping("search")
    public String search(@PageableDefault(size = 5,sort={"updateTime"},direction = Sort.Direction.DESC)Pageable pageable, Model model,
                         NewsQuery newsQuery){
        Page<News> page=newsService.searchNews(pageable,newsQuery);
        model.addAttribute("page",page);
        return "admin/news :: newsList";
    }
}


dao层

package com.zr.dao;

import com.zr.po.News;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public interface NewsDao extends JpaRepository<News,Long> , JpaSpecificationExecutor<News> {


}


po层

package com.zr.po;

public class NewsQuery {
    private String title;
    private String typeId;
    private Boolean recommend;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTypeId() {
        return typeId;
    }

    public void setTypeId(String typeId) {
        this.typeId = typeId;
    }

    public Boolean getRecommend() {
        return recommend;
    }

    public void setRecommend(Boolean recommend) {
        this.recommend = recommend;
    }

    public NewsQuery() {
    }

    public NewsQuery(String title, String typeId, Boolean recommend) {
        this.title = title;
        this.typeId = typeId;
        this.recommend = recommend;
    }

    @Override
    public String toString() {
        return "NewsQuery{" +
                "title='" + title + '\'' +
                ", typeId='" + typeId + '\'' +
                ", recommend=" + recommend +
                '}';
    }
}


service层
接口:

package com.zr.service;

import com.zr.po.News;
import com.zr.po.NewsQuery;
import com.zr.po.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

public interface NewsService {
    Page<News> findByPageable(Pageable pageable);

    void input(News news);

    News findNewsById(Long id);


    Page<News> searchNews(Pageable pageable, NewsQuery newsQuery);
}


课堂项目成果展示
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值