实训感受
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);
}
课堂项目成果展示