7.29第九次实训笔记
程序员文章站
2022-09-16 21:13:04
7.29第九次实训笔记实训感受课堂项目编码课堂项目成果展示实训感受1、今天实现了新闻的新增,编辑。感觉有点难的,可能是刚接触,感觉很陌生,但应该也是挺实用的。2、尤其是在写搜索的时候,感觉很迷,不过很实用,学会了以后在其他场景都可以用得上。课堂项目编码controller层package com.zr.controller;import com.zr.po.*;import com.zr.service.NewsService;import com.zr.service.TagServi...
实训感受
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);
}
实现层
package com.zr.service.Impl;
import com.zr.dao.NewsDao;
import com.zr.po.News;
import com.zr.po.NewsQuery;
import com.zr.po.Tag;
import com.zr.po.Type;
import com.zr.service.NewsService;
import com.zr.util.MyBeanUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class NewsServiceImpl implements NewsService {
@Autowired
private NewsDao newsDao;
public Page<News> findByPageable(Pageable pageable) {
return newsDao.findAll(pageable);
}
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);
}
}
public News findNewsById(Long id) {
return newsDao.getOne(id);
}
public Page<News> searchNews(Pageable pageable, final NewsQuery newsQuery) {
Page<News> news=newsDao.findAll(new Specification<News>(){
public Predicate toPredicate(Root<News> root, CriteriaQuery<?> criteriaQuery, 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()));
}
criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()]));
return null;
}
},pageable);
return news;
}
}
课堂项目成果展示
新增(界面缩小了50%)
新闻界面
搜索功能
本文地址:https://blog.csdn.net/weixin_44043727/article/details/107678062