中软国际实训日记第九天-7.29

目录

1.新闻界面

2.新增和编辑

3.搜索

正文

1.新闻界面

1.1控制层

 @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";
    }

1.2服务实现层

 @Autowired
      private NewsDao newsDao;

      @Override
      public Page<News> findByPageable(Pageable pageable) {
            return newsDao.findAll(pageable);
      }

1.3界面
在这里插入图片描述

2.新增和编辑

2.1控制层
如果id恒等于-1则跳转新增界面,否则跳转到编辑页面

 @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";
    }

2.2服务实现层

@Override
      public News findNewsById(Long id) {
            return newsDao.getOne(id);
      }

      @Override
      public void input(News news) {
            if(news.getId()==null){
                  news.setCreateTime(new Date());
                  newsDao.save(news);
            }else{
                  news.setUpdateTime(new Date());
                  News n=newsDao.getOne(news.getId());
                  BeanUtils.copyProperties(news,n, MyBeanUtils.getNullPropertyNames(news));
                  newsDao.save(n);
            }
      }

2.3界面
在这里插入图片描述
在这里插入图片描述

3.搜索

3.1控制层

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

3.2服务实现层


      @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(newsQuery.getRecommend()!=null){
                              predicates.add(criteriaBuilder.equal(root.<Boolean>get("recommend"),newsQuery.getRecommend()));
                        }
                        query.where(predicates.toArray(new Predicate[predicates.size()]));
                        return null;
                  }
            },pageable);
            return news;
      }

3.3界面
在这里插入图片描述
在这里插入图片描述

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页