【中软国际实训】——Day9
程序员文章站
2024-01-15 12:47:22
新闻管理——条件查询、新增、编辑1.项目结构2.新闻管理2.1 实体设计2.2 service接口与实现2.3 web控制3 功能测试1.项目结构2.新闻管理2.1 实体设计Newspackage com.zr0726.news.po;import javax.persistence.*;import java.util.ArrayList;import java.util.Date;import java.util.List;@Entity@Table(name = "t_...
1.项目结构
2.新闻管理
2.1 实体设计
- News
package com.zr0726.news.po;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Entity
@Table(name = "t_news")
public class News {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
@Basic(fetch = FetchType.LAZY)
@Lob
private String content;
private String firstPicture;
private String flag;
private String views;
private boolean appreciation;
private boolean shareStatement;
private boolean commentabled;
private boolean published;
private boolean recommend;
@Temporal(TemporalType.TIMESTAMP)
private Date createTime;
@Temporal(TemporalType.TIMESTAMP)
private Date updateTime;
@ManyToOne
private Type type;
@ManyToOne
private User user;
@ManyToMany(cascade = CascadeType.PERSIST)
private List<Tag> tags = new ArrayList<>();
@Transient
private String tagIds;
private String description;
public News(){
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getFirstPicture() {
return firstPicture;
}
public void setFirstPicture(String firstPicture) {
this.firstPicture = firstPicture;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
public String getViews() {
return views;
}
public void setViews(String views) {
this.views = views;
}
public boolean isAppreciation() {
return appreciation;
}
public void setAppreciation(boolean appreciation) {
this.appreciation = appreciation;
}
public boolean isShareStatement() {
return shareStatement;
}
public void setShareStatement(boolean shareStatement) {
this.shareStatement = shareStatement;
}
public boolean isCommentabled() {
return commentabled;
}
public void setCommentabled(boolean commentabled) {
this.commentabled = commentabled;
}
public boolean isPublished() {
return published;
}
public void setPublished(boolean published) {
this.published = published;
}
public boolean isRecommend() {
return recommend;
}
public void setRecommend(boolean recommend) {
this.recommend = recommend;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List<Tag> getTags() {
return tags;
}
public void setTags(List<Tag> tags) {
this.tags = tags;
}
public String getTagIds() {
return tagIds;
}
public void setTagIds(String tagId) {
this.tagIds = tagIds;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void init(){
this.tagIds = tagsToIds(this.getTags());
}
//1,2,3
private String tagsToIds(List<Tag> tags){
if (!tags.isEmpty()){
StringBuffer ids = new StringBuffer();
boolean flag = false;
for (Tag tag:tags){
if (flag){
ids.append(",");
}else {
flag=true;
}
ids.append(tag.getId());
}
return ids.toString();
}else {
return tagIds;
}
}
@Override
public String toString() {
return "News{" +
"id=" + id +
", title='" + title + '\'' +
", content='" + content + '\'' +
", firstPicture='" + firstPicture + '\'' +
", flag='" + flag + '\'' +
", views='" + views + '\'' +
", appreciation=" + appreciation +
", shareStatement=" + shareStatement +
", commentabled=" + commentabled +
", published=" + published +
", recommend=" + recommend +
", createTime=" + createTime +
", updateTime=" + updateTime +
", type=" + type +
", user=" + user +
", tags=" + tags +
", tagIds='" + tagIds + '\'' +
", description='" + description + '\'' +
'}';
}
}
- NewQuery
package com.zr0726.news.vo;
public class NewQuery {
private String title;
private Long typeId;
private boolean recommend;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Long getTypeId() {
return typeId;
}
public void setTypeId(Long typeId) {
this.typeId = typeId;
}
public boolean isRecommend() {
return recommend;
}
public void setRecommend(boolean recommend) {
this.recommend = recommend;
}
@Override
public String toString() {
return "NewQuery{" +
"title='" + title + '\'' +
", typeId=" + typeId +
", recommend=" + recommend +
'}';
}
}
- NewsRepository
package com.zr0726.news.dao;
import com.zr0726.news.po.News;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
public interface NewRepository extends JpaRepository<News,Long> , JpaSpecificationExecutor<News> {
@Query("select n from News n where n.title like ?1 or n.content like ?1")
Page<News> findByQuery(String query, Pageable pageable);
}
2.2 service接口与实现
- NewService
package com.zr0726.news.service;
import com.zr0726.news.po.News;
import com.zr0726.news.vo.NewQuery;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
public interface NewService {
Page<News> listNew(Pageable pageable, NewQuery newQuery);
News saveNew(News news);
News getNew(Long id);
News updateNew(Long id,News news);
}
- NewServiceImpl
package com.zr0726.news.service.impl;
import com.zr0726.news.dao.NewRepository;
import com.zr0726.news.po.News;
import com.zr0726.news.service.NewService;
import com.zr0726.news.vo.NewQuery;
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 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 NewServiceImpl implements NewService {
@Autowired
private NewRepository newRepository;
//新闻管理中的新闻列表(包含了查询)
@Override
public Page<News> listNew(Pageable pageable, NewQuery newQuery) {
return newRepository.findAll(new Specification<News>() {
@Override
public Predicate toPredicate(Root<News> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
List<Predicate> predicates = new ArrayList<>();
if (!"".equals(newQuery.getTitle())&&newQuery.getTitle()!=null){
predicates.add(cb.like(root.<String>get("title"),"%"+newQuery.getTitle()+"&"));
}
if (newQuery.getTypeId()!=null){
predicates.add(cb.equal(root.get("type").get("id"),newQuery.getTypeId()));
}
if (newQuery.isRecommend()){
predicates.add(cb.equal(root.get("recommend"),newQuery.isRecommend()));
}
cq.where(predicates.toArray(new Predicate[predicates.size()]));
return null;
}
},pageable);
}
@Override
public News saveNew(News news) {
if (news.getId()==null){
news.setCreateTime(new Date());
news.setUpdateTime(new Date());
}
return newRepository.save(news);
}
@Override
public News getNew(Long id) {
return newRepository.findById(id).orElse(null);
}
@Override
public News updateNew(Long id, News news) {
News news1 = newRepository.findById(id).orElse(null);
if (news1 == null){
System.out.println("未获得更新对象");
}
BeanUtils.copyProperties(news,news1);
news1.setUpdateTime(new Date());
return newRepository.save(news1);
}
}
2.3 web控制
- NewController
package com.zr0726.news.web.admin;
import com.zr0726.news.po.News;
import com.zr0726.news.po.User;
import com.zr0726.news.service.NewService;
import com.zr0726.news.service.TagService;
import com.zr0726.news.service.TypeService;
import com.zr0726.news.vo.NewQuery;
import org.springframework.beans.factory.annotation.Autowired;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/admin")
public class NewController {
private static final String INPUT = "admin/news-input";
private static final String LIST = "admin/news";
private static final String REDIRECT_LIST = "redirect:/admin/news";
@Autowired
private NewService newService;
@Autowired
private TypeService typeService;
@Autowired
private TagService tagService;
@GetMapping("/news")
public String news(@PageableDefault(size = 3,sort = {"updateTime"},direction = Sort.Direction.DESC)
Pageable pageable , NewQuery newQuery, Model model){
model.addAttribute("types",typeService.listType());
model.addAttribute("page",newService.listNew(pageable, newQuery));
return LIST;
}
@PostMapping("/news/search")
public String search(@PageableDefault(size = 3,sort = {"updateTime"},direction = Sort.Direction.DESC)
Pageable pageable , NewQuery newQuery, Model model){
model.addAttribute("page",newService.listNew(pageable,newQuery));
return "admin/news :: newsList";
}
public void setTypeAndTag(Model model){
model.addAttribute("type",typeService.listType());
model.addAttribute("tags",tagService.listTag());
}
@GetMapping("/news/input")
public String input(Model model){
// model.addAttribute("type",typeService.listType());
// model.addAttribute("tags",tagService.listTag());
setTypeAndTag(model);
model.addAttribute("news",new News());
return INPUT;
}
@GetMapping("/news/{id}/toUpdate")
public String toUpdate(@PathVariable Long id,Model model){
setTypeAndTag(model);
News news = newService.getNew(id);
news.init();
model.addAttribute("news",news);
return INPUT;
}
@PostMapping("/news/add")
public String post(News news , RedirectAttributes attributes, HttpSession session){
System.out.println("controller接收的news为:"+news);
news.setUser((User) session.getAttribute("user"));
news.setType(typeService.getType(news.getType().getId()));
news.setTags(tagService.listTag(news.getTagIds()));
News news1;
if (news.getId()==null){
news1 = newService.saveNew(news);
}else {
news1 = newService.updateNew(news.getId(),news);
}
if (news1==null){
attributes.addFlashAttribute("message","操作失败");
}else {
attributes.addFlashAttribute("message","操作成功");
}
return REDIRECT_LIST;
}
}
3 功能测试
本文地址:https://blog.csdn.net/qq_43662718/article/details/107677394
下一篇: C语言实现单链表的翻转